{% extends "deliver_grant_funding/grant_base.html" %}
{% from "govuk_frontend_jinja/components/notification-banner/macro.html" import govukNotificationBanner %}
{% from "govuk_frontend_jinja/components/summary-list/macro.html" import govukSummaryList %}
{% from "govuk_frontend_jinja/components/button/macro.html" import govukButton %}
{% from "deliver_grant_funding/macros/dependency_banner.html" import dependency_banner %}
{% from "deliver_grant_funding/macros/question_table.html" import question_table %}
{% from "deliver_grant_funding/macros/group_nesting_error_banner.html" import group_nesting_error_banner %}
{% from "deliver_grant_funding/macros/deliver_grant_funding_collection_breadcrumb.html" import deliver_grant_funding_collection_breadcrumb %}

{% set page_title = group.name ~ " - " ~ db_form.title %}
{% set type_constants = db_form.collection.type.constants %}
{% set active_item_identifier = type_constants.active_nav %}
{% set grant_admin = authorisation_helper.is_deliver_grant_admin(grant.id, current_user) %}

{% block beforeContent %}
  {% set parent_item = None %}
  {% if group.parent %}


    {% set parent_html %}
      <span class="govuk-visually-hidden">Question group:</span>
      <span>{{ group.parent.name }}</span>
    {% endset %}
    {% set parent_item={"html": parent_html, "href": url_for("deliver_grant_funding.list_group_questions", grant_id=grant.id, group_id=group.parent_id)} %}
  {% endif %}
  {{ deliver_grant_funding_collection_breadcrumb(grant=grant, collection=db_form.collection, form=db_form, immediate_parent_breadcrumb_item=parent_item,this_page_breadcrumb_item={"text":group.name}) }}
{% endblock beforeContent %}

{% block content %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      {% with flashes = get_flashed_messages(category_filter=[enum.flash_message_type.NESTED_GROUP_ERROR.value]) %}
        {% if flashes %}
          {% for error in flashes %}
            {{ group_nesting_error_banner(error, interpolate=interpolate) }}
          {% endfor %}
        {% endif %}
      {% endwith %}
      {% with flashes = get_flashed_messages(category_filter=[enum.flash_message_type.DEPENDENCY_ORDER_ERROR.value, enum.flash_message_type.DATA_SOURCE_ITEM_DEPENDENCY_ERROR.value]) %}
        {% if flashes %}
          {% for error in flashes %}
            {{ dependency_banner(error, interpolate=interpolate) }}
          {% endfor %}
        {% endif %}
      {% endwith %}

      {% if delete_form %}
        {% set banner_html %}
          <p class="govuk-body govuk-!-font-weight-bold">Are you sure you want to delete this question group?</p>
          <form method="post" novalidate>
            {{ delete_form.csrf_token }}

            <span class="govuk-button-group govuk-!-margin-bottom-0">
              {{ delete_form.confirm_deletion(params={"text": "Yes, delete this question group", "classes": "govuk-button--warning govuk-!-margin-bottom-0"}) }}
              <a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('deliver_grant_funding.list_group_questions', grant_id=grant.id, group_id=group.id) }}">Cancel</a>
            </span>
          </form>
        {% endset %}

        {{ govukNotificationBanner(params={"role": "alert", "titleText": "Warning", "classes": "", "html": banner_html}) }}
      {% endif %}

      <h1 class="govuk-heading-l">{{ group.name }}</h1>
    </div>
  </div>
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      <h2 class="govuk-heading-m govuk-!-margin-top-5">Question group settings</h2>

      {% set change_add_another_html %}
        <span>Change</span><span class="govuk-visually-hidden">add another setting</span>
      {% endset %}
      {% set change_display_html %}
        <span>Change</span><span class="govuk-visually-hidden">display setting</span>
      {% endset %}

      {# todo: business logic with these change links could quickly get out of sync with interfaces #}
      {#       only keep the ones that feel really valuable to see up front (likely an actionable buttons #}
      {{
        govukSummaryList({
          "rows": [
            {
              "key": {"text": "Display"},
              "value": {"text": "All questions on the same page" if group.same_page else "One question per page"},
              "actions": {
                "items": [{
                  "html": change_display_html,
                  "href": url_for("deliver_grant_funding.change_group_display_options", grant_id=grant.id, group_id=group.id)
                }]
              } if not group.has_nested_groups else {}
            },
            {
              "key": {"text": "Add another"},
              "value": {"text": "People can add answers for this group more than once" if group.add_another else "People can only answer the questions in this group once"},
              "actions": {
                "items": [{
                  "html": change_add_another_html,
                  "href": url_for("deliver_grant_funding.change_group_add_another_options", grant_id=grant.id, group_id=group.id)
                }]
              } if not group.contains_add_another_components and not group.contains_questions_depended_on_elsewhere and not group.parent.add_another_container else {}
            }
          ]
        })
      }}

      {# todo: we've agreed to remove these from these settings "Change" tables and only keep this pattern for #}
      {#       where theres an action button not shown (Like "Add question group") - remove these and rely on error summary #}
      {% if group.has_nested_groups %}
        <p class="govuk-body">You cannot change the display type because this group has one or more nested groups.</p>
      {% endif %}
      {% if group.contains_add_another_components %}
        <p class="govuk-body">You cannot allow this question group to be answered more than once as it already contains one or more questions that can be answered more than once.</p>
      {% endif %}
      {% if group.contains_questions_depended_on_elsewhere %}
        <p class="govuk-body">You cannot allow this question group to be answered more than once as it contains questions that are depended on by other questions in this form.</p>
      {% endif %}
      {% if group.parent and group.parent.add_another_container %}
        <p class="govuk-body">You cannot allow this question group to be answered more than once as it is already contained in a group that can be answered more than once.</p>
      {% endif %}
    </div>
  </div>
  {% if group.add_another %}
    <div class="govuk-grid-row">
      <div class="govuk-grid-column-two-thirds">
        <h2 class="govuk-heading-m govuk-!-margin-top-5">Add another settings</h2>
        <p class="govuk-body">Customise the add another summary page by selecting how each response to this question group should be summarised or providing guidance on answering this question group multiple times.</p>

        {% set answer_summary_list_html %}
          {% if group.questions_in_add_another_summary | length %}
            <p class="govuk-body">A comma separated list including the answers to:</p>
            <ul class="govuk-list govuk-list--bullet">
              {% for question in group.questions_in_add_another_summary %}
                <li>{{ question.name }}</li>
              {% endfor %}
            </ul>
          {% else %}
            There are no questions in this question group.
          {% endif %}
        {% endset %}


        {% set guidance_text %}
          {% if group.add_another_guidance_body %}
            <pre class="app-pre-markdown">{{ interpolate(group.add_another_guidance_body, with_interpolation_highlighting=True) }}</pre>
          {% else %}
            (None set)
          {% endif %}
        {% endset %}

        {{
          govukSummaryList({
            "rows": [{
              "key": {"text": "Answer summary"},
              "value": {"html": answer_summary_list_html},
              "actions": {
                "items": [{
                  "text": "Change",
                  "visuallyHiddenText": "add another response summary",
                  "href": url_for("deliver_grant_funding.change_group_add_another_summary", grant_id=grant.id, group_id=group.id)
                }] if group.questions_in_add_another_summary else []
              }
            }, {
              "key": {"text": "Guidance text"},
              "value": {"html": guidance_text},
              "actions": {
                "items": [{
                  "text": "Change",
                  "visuallyHiddenText": "add another guidance text",
                  "href": url_for("deliver_grant_funding.manage_add_another_guidance", grant_id=grant.id, group_id=group.id)
                }]
              }
            }]
          })
        }}
      </div>
    </div>
  {% endif %}
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-full">
      <div class="govuk-body govuk-form-group">
        <h2 class="govuk-heading-m govuk-!-margin-top-5">Questions</h2>
        {% if not group.components %}
          <p class="govuk-body">This question group has no questions.</p>
        {% else %}
          {{ question_table(group, interpolate, role_can_edit=grant_admin, enum=enum) }}
        {% endif %}

        {% if grant_admin %}
          <a class="govuk-button govuk-button--secondary" href="{{ url_for('deliver_grant_funding.choose_question_type', grant_id=grant.id, form_id=db_form.id, parent_id=group.id) }}"
            >{{ "Add another question" if group.cached_questions else "Add a question" }}</a
          >
          {% if group.can_have_child_group and not group.presentation_options.show_questions_on_the_same_page %}
            <a class="govuk-button govuk-button--secondary" href="{{ url_for('deliver_grant_funding.add_question_group_name', grant_id=grant.id, form_id=db_form.id, parent_id=group.id) }}"> {{ "Add a question group" }}</a>
          {% endif %}
          {% if not group.can_have_child_group %}
            <p class="govuk-body">You cannot add a nested group because this group is already nested.</p>
          {% elif group.same_page %}
            <p class="govuk-body">You cannot add a nested group when display is set to show all questions on the same page.</p>
          {% endif %}
        {% endif %}
      </div>
    </div>
  </div>
  <div class="govuk-grid-row">
    <div class="govuk-grid-column-two-thirds">
      {% if group.same_page %}
        <div class="govuk-body govuk-form-group">
          <h2 class="govuk-heading-m govuk-!-margin-top-5">Guidance</h2>
          <p class="govuk-body">Only add guidance if you need to give a longer explanation of how to answer the question group, or to format your content with paragraphs, headings, lists or links.</p>

          {% if group.guidance_body %}
            {% set guidance_text %}
              <pre class="app-pre-markdown">{{ interpolate(group.guidance_body, with_interpolation_highlighting=True) }}</pre>
            {% endset %}
            {{
              govukSummaryList({
                "rows": [
                  {"key": {"text": "Page heading"}, "value": {"text": group.guidance_heading}, "actions": {"items": [{"href": url_for('deliver_grant_funding.manage_guidance', grant_id=grant.id, question_id=group.id), "text": "Change", "visuallyHiddenText": "page heading"}] } },
                  {"key": {"text": "Guidance text"}, "value": {"html": guidance_text}, "actions": {"items": [{"href": url_for('deliver_grant_funding.manage_guidance', grant_id=grant.id, question_id=group.id), "text": "Change", "visuallyHiddenText": "guidance text"}] } },
                ]
              })
            }}
          {% else %}
            <a class="govuk-button govuk-button--secondary" href="{{ url_for('deliver_grant_funding.manage_guidance', grant_id=grant.id, question_id=group.id) }}"> Add guidance </a>
          {% endif %}
        </div>
      {% endif %}

      <div class="govuk-body govuk-form-group">
        <h2 class="govuk-heading-m govuk-!-margin-top-5">Conditions</h2>
        <p class="govuk-body">Add conditions to only show this question group if the grant recipient has answered other questions in a particular way.</p>

        {% with component=group %}
          {% include 'deliver_grant_funding/partials/component-conditions-operator-table.html' %}
        {% endwith %}

        {% set rows = [] %}
        {% with component=group %}
          {% include 'deliver_grant_funding/partials/_implicit-conditions.html' %}
        {% endwith %}

        {% with component=group %}
          {% include 'deliver_grant_funding/partials/_explicit-conditions.html' %}
        {% endwith %}

        {% if rows %}
          {{ govukSummaryList({ "rows": rows, "classes": "app-tasklist-builder" }) }}
        {% endif %}
        {{
          govukButton({
            "text": "Add condition",
            "href": url_for('deliver_grant_funding.add_question_condition_select_calculation', grant_id=grant.id, component_id=group.id),
            "classes": "govuk-button--secondary",
          })
        }}
      </div>

      <div class="govuk-body govuk-form-group">
        <h2 class="govuk-heading-m govuk-!-margin-top-5">Validations</h2>
        {% if group.same_page %}
          <p class="govuk-body">Add validations to show an error message if a calculation including at least one answer from the question group does not meet a requirement.</p>
          <p class="govuk-body">For example, validation to stop users from entering numbers that add up to more than a required amount.</p>
          {% set rows = [] %}
          {% with component=group %}
            {% include 'deliver_grant_funding/partials/_explicit-validations.html' %}
          {% endwith %}

          {% if rows %}
            {{ govukSummaryList({ "rows": rows, "classes": "app-tasklist-builder" }) }}
          {% endif %}

          {% if grant_admin %}
            {{
              govukButton({
                "text": "Add more validation" if group.validations else "Add validation",
                "href": url_for('deliver_grant_funding.add_group_validation', grant_id=grant.id, group_id=group.id),
                "classes": "govuk-button--secondary govuk-!-margin-bottom-3",
              })
            }}
          {% endif %}
        {% else %}
          {# TODO: content review #}
          <p class="govuk-body">You can only add validations to a question group when questions are displayed on the same page. To add a validation, change the display setting to 'All questions on the same page'.</p>
        {% endif %}
      </div>

      {% if grant_admin %}
        <div class="govuk-body govuk-form-group">
          <h2 class="govuk-heading-m govuk-!-margin-top-9">Manage question group</h2>
          <div class="govuk-body">
            <ul class="govuk-list govuk-list--spaced">
              <li><a class="govuk-link govuk-link--no-visited-state" href="{{ url_for('deliver_grant_funding.change_group_name', grant_id=grant.id, group_id=group.id) }}">Change question group name</a></li>
              <li><a class="govuk-link govuk-link--no-visited-state" href="?delete">Delete question group</a></li>
            </ul>
          </div>
        </div>
      {% endif %}
    </div>
  </div>
{% endblock content %}
