{% extends "templates/base.html" %}
{% from "www/macros/livecode.html" import LiveCodeEditorJS, LiveCodeEditor with context %}

{% block title %}

{% if lesson.title %}
{{ lesson.title }} - {{ course.title }}
{% else %}
{{ _("New Lesson") }}
{% endif %}

{% endblock %}


{% block head_include %}
{% include "public/icons/symbol-defs.svg" %}
<link rel="stylesheet" href="/assets/frappe/css/hljs-night-owl.css">

{% for ext in page_extensions %}
{{ ext.render_header() }}
{% endfor %}

{% endblock %}


{% block content %}
<div class="common-page-style lesson-page">
    <div class="container course-details-page">
        {{ BreadCrumb(course, lesson) }}
        <div class="course-content-parent">
            <div class="course-details-outline">
                {{ widgets.CourseOutline(course=course, membership=membership) }}
            </div>
            <div class="lesson-pagination-parent">
                {{ LessonContent(lesson) }}
                {% if not lesson.edit_mode and course.status == "Approved" and not course.upcoming %}
                {{ Discussions() }}
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}


<!-- BreadCrumb -->
{% macro BreadCrumb(course, lesson) %}
<div class="breadcrumb">
    <a class="dark-links" href="/courses">{{ _("All Courses") }}</a>
    <img class="ml-1 mr-1" src="/assets/lms/icons/chevron-right.svg">
    <a class="dark-links" href="/courses/{{ course.name }}">{{ course.title }}</a>
    <img class="ml-1 mr-1" src="/assets/lms/icons/chevron-right.svg">
    <span class="breadcrumb-destination">{{ lesson.title if lesson.title else _("New Lesson") }}</span>
</div>
{% endmacro %}


<!-- Lesson Details -->
{% macro LessonContent(lesson) %}
{% set instructors = get_instructors(course.name) %}
{% set is_instructor = is_instructor(course.name) %}

<div class="common-card-style lesson-content">
    <div class="lesson-title">

        <!-- Title -->
        <div class="course-home-headings title mb-0 {% if membership %} is-member {% endif %}
            {% if membership or is_instructor %} eligible-for-submission {% endif %}" id="title"
            {% if lesson.edit_mode %} data-placeholder="{{ _('Title') }}" contenteditable="true" {% endif %}
            data-index="{{ lesson_index }}" data-course="{{ course.name }}" data-chapter="{{ chapter }}"
            {% if lesson.name %} data-lesson="{{ lesson.name }}" {% endif %}
            >{% if lesson.title %}{{ lesson.title }}{% endif %}</div>

        {% if get_progress(course.name, lesson.name) == 'Complete' %}
        <span id="status-indicator" class="indicator-pill green">{{ _("COMPLETED") }}</span>
        {% endif %}

        <!-- Edit Button -->
        {% if (is_instructor or has_course_moderator_role()) and not lesson.edit_mode %}
        <button class="btn btn-secondary btn-sm ml-2 btn-edit"> {{ _("Edit") }} </button>
        {% endif %}
    </div>

    <!-- Instructors -->
    <div class="d-flex align-items-center">
        {% set ins_len = instructors | length %}
        {% for instructor in instructors %}
        {% if ins_len > 1 and loop.index == 1 %}
        <div class="avatar-group overlap">
        {% endif %}
        {{ widgets.Avatar(member=instructor, avatar_class="avatar-small") }}

        {% if ins_len > 1 and loop.index == ins_len %}
        </div>
        {% endif %}
        {% endfor %}
        <a class="button-links ml-1" href="{{ get_profile_url(instructors[0].username) }}">
        <span class="course-meta">
            {% if ins_len == 1 %}
                {{ instructors[0].full_name }}
            {% elif ins_len == 2 %}
                {{ instructors[0].full_name.split(" ")[0] }} and {{ instructors[1].full_name.split(" ")[0] }}
            {% else %}
                {% set suffix = "other" if ins_len - 1 == 1 else "others"  %}
                {{ instructors[0].full_name.split(" ")[0] }} and {{ ins_len - 1 }} {{ suffix }}
            {% endif %}
        </span>
        </a>
        <div class="ml-5 course-meta"> {{ frappe.utils.format_date(lesson.creation, "medium") }} </div>
    </div>

    <!-- Lesson Content -->
    <div class="markdown-source lesson-content-card {% if lesson.edit_mode %} mb-0 mt-2 {% endif %} ">
        {% if show_lesson %}

            {% if is_instructor and not lesson.include_in_preview and not lesson.edit_mode %}
            <div class="medium alert alert-info alert-dismissible mb-4">
                {{ _("This lesson is not available for preview. As you are the Instructor of the course only you can see it.") }}
                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
            </div>
            {% endif %}

            {% if lesson.edit_mode %}
            {{ EditLesson(lesson) }}
            {% else %}
            {{ render_html(lesson) }}
            {% endif %}

        {% else %}
            {% set course_link = "<a class='join-batch' data-course=" + course.name | urlencode + " href=''>" + _('here') +  "</a>" %}
            <div class="alert alert-info medium mb-0">
                {{ _("There is no preview available for this lesson.
                Please join the course to access it.
                Click {0} to enroll.").format(course_link) }}
            </div>
        {% endif %}
    </div>

    {% if not lesson.edit_mode %}
    {{ pagination(prev_url, next_url) }}
    {% endif %}

</div>
{% endmacro %}


<!-- Pagination -->
{% macro pagination(prev_url, next_url) %}
    {% if prev_url or next_url %}
    <div class="lesson-pagination">
        {% if prev_url %}
        <div>
            <a class="btn btn-secondary btn-sm prev" href="{{ prev_url }}">
                {{ _("Previous") }}
            </a>
        </div>
        {% endif %}

        {% if next_url %}
        <div>
            <a class="btn btn-primary btn-sm next ml-2 " href="{{ next_url }}">
                {{ _("Next") }}
            </a>
        </div>
        {% endif %}
    </div>
    {% endif %}
{% endmacro %}


<!-- Edit Lesson -->
{% macro EditLesson(lesson) %}

    <div class="d-flex mt-2 medium">
        <div class="flex-grow-1" contenteditable="true" data-placeholder="{{ _('YouTube Video ID') }}"
        id="youtube">{% if lesson.youtube %}{{ lesson.youtube }}{% endif %}</div>

        <div class="flex-grow-1 ml-2" contenteditable="true" data-placeholder="{{ _('Quiz ID') }}"
        id="quiz-id">{% if lesson.quiz_id %}{{ lesson.quiz_id }}{% endif %}</div>
    </div>

    <div id="body" {% if lesson.body %} data-body="{{ lesson.body }}" {% endif %}></div>

    <div class="d-flex medium mx-0 mb-4">
        <div class="flex-grow-1" contenteditable="true" data-placeholder="{{ _('Assignment Question') }}"
        id="assignment-question">{% if lesson.question %}{{ lesson.question }}{% endif %}</div>

        <select class="btn btn-default ml-2" id="file-type" data-type="{{ lesson.file_type }}">
            <option selected> {{ _("File Type") }} </option>
            <option value="Image"> {{ _("Image") }} </option>
            <option value="Document"> {{ _("Document") }} </option>
            <option value="PDF"> {{ _("PDF") }} </option>
        </select>
    </div>




    <label class="preview" for="preview">
        <input {% if lesson.include_in_preview %} checked {% endif %} type="checkbox" id="preview">
        {{ _("Show preview of this lesson to Guest users.") }}
    </label>

    <div class="mt-4">
        <button class="btn btn-primary btn-sm btn-lesson pull-right ml-2"> {{ _("Save") }} </button>
        {% if lesson.name %}
        <button class="btn btn-secondary btn-sm pull-right btn-back ml-2"> {{ _("Back to Lesson") }} </button>
        <a class="btn btn-secondary btn-sm pull-right" href="/quizzes"> {{ _("Create Quiz") }} </a>
        {% endif %}

        {{ UploadAttachments() }}

    </div>

    {{ HelpArticle() }}
{% endmacro %}


{% macro UploadAttachments() %}
    <div class="attachments-parent">
        <div class="attachment-controls">
            <div class="show-attachments" data-toggle="collapse" data-target="#collapse-attachments" aria-expanded="false">
                <svg class="icon icon-sm">
                    <use class="" href="#icon-attachment">
                </svg>
                <span class="attachment-count" data-count="0">0 {{ _("attachments") }}</span>
            </div>
            <div class="add-attachment">
                <span class="btn btn-sm btn-secondary">
                    <svg class="icon icon-sm">
                        <use class="" href="#icon-upload">
                    </svg>
                    {{ _("Upload Attachments") }}
                </span>
            </div>
        </div>
        <table class="attachments common-card-style collapse hide" id="collapse-attachments"></table>
    </div>
{% endmacro %}


<!-- Help Article -->
{% macro HelpArticle() %}
    <div class="medium">
        <h3> {{ _("Embed Components") }} </h3>
        <p>
            {{ _("You can add additional content to the lesson using a special syntax. The table below mentions
                all types of dynamic content that you can add to the lessons and the syntax for the same.") }}
        </p>
        <ol>
            <li>
                <b> {{ _("YouTube Video") }} </b>
                <p> To get the YouTube Video ID, follow the steps mentioned below. </p>
                <ul class="px-4">
                    <li>
                        {{ _("Upload the video on youtube.") }}
                    </li>
                    <li>
                        {{ _("When you share a youtube video, it shows a URL") }}
                    </li>
                    <li>
                        {{ _("Copy the last parameter of the URL and paste it here.") }}
                    </li>
                </ul>
            </li>
        </ol>
    </div>
{% endmacro %}


<!-- Discussions Component -->
{% macro Discussions() %}
    {% set topics_count = frappe.db.count("Discussion Topic", {
        "reference_doctype": "Course Lesson",
        "reference_docname": lesson.name
    }) %}
    {% set condition = is_instructor(course.name) or membership %}
    {% set doctype, docname = _("Course Lesson"), lesson.name %}
    {% set title = "Questions" if topics_count else "" %}
    {% set cta_title = "Ask a Question" %}
    {% set button_name = _("Start Learning") %}
    {% set redirect_to = "/courses/" + course.name %}
    {% set empty_state_title = _("Have a doubt?") %}
    {% set empty_state_subtitle = _("Post it here, our mentors will help you out.") %}
    {% include "frappe/templates/discussions/discussions_section.html" %}
{% endmacro %}


<!-- Scripts -->
{%- block script %}
    {{ super() }}
    <script type="text/javascript">
        var page_context = {{ page_context | tojson }};
    </script>
    {{ include_script('controls.bundle.js') }}
    {% for ext in page_extensions %}
    {{ ext.render_footer() }}
    {% endfor %}
{%- endblock %}
