{% from "_macros/shared/vf_section_top_rule.jinja" import vf_section_top_rule %}

{#-
    Params
        - body
            - size ("small" | "medium" | "large")
            - text (str)
 -#}
{%- macro _quote_wrapper_body_block(body={}) -%}
  {%- set quote_size = body.get("size", "") | trim | lower -%}
  {%- set quote_text = body.get("text", "") | trim -%}

  {# Translate quote size param to quote heading level #}
  {% if quote_size == 'large' %}
    {% set quote_heading_level = 2 %}
  {% elif quote_size == 'small' %}
    {% set quote_heading_level = 6 %}
  {% else %}
    {% set quote_heading_level = 4 %}
  {% endif %}

  {%- if quote_text | length > 0 -%}
    <p class="p-heading--{{ quote_heading_level }}">
      <i>
        &#8220;{{ quote_text }}&#8221;
      </i>
    </p>
  {%- endif -%}
{%- endmacro %}

{#-
    Params
        - citation
            - name
            - organisation
            - title
 -#}
{%- macro _quote_wrapper_citation_block(citation={}) -%}
  {%- set citation_source_name = citation.get("name", "") | trim -%}
  {%- set citation_source_title = citation.get("title", "") | trim -%}
  {%- set citation_source_organisation = citation.get("organisation", "") | trim -%}

  {%- set has_citation_source_name = citation_source_name | length > 0 -%}
  {%- set has_citation_source_title = citation_source_title | length > 0 -%}
  {%- set has_citation_source_organisation = citation_source_organisation | length > 0 -%}
  {%- set has_citation = has_citation_source_name or has_citation_source_title or has_citation_source_organisation -%}

  {%- if has_citation -%}
    {#- Optional citation block -#}
    <p>
      {% if has_citation_source_name -%}
        {#- Optional citation source name -#}
        <strong>{{ citation_source_name }}</strong>
        {% if has_citation_source_title or has_citation_source_organisation -%}
          {#- If the citation name is followed by title and/or organisation, add a line break -#}
          <br>
        {% endif -%}
      {% endif -%}
      {% if has_citation_source_title or has_citation_source_organisation -%}
        {#- Optional citation source title and/or organisation -#}
        <span class="u-text--muted">
            {% if has_citation_source_title -%}
              {#- Optional citation source title -#}
              {{ citation_source_title }}
              {%- if has_citation_source_organisation %}
                {#- Add a line break between the title and org if both are present -#}
                <br>
              {%- endif %}
            {% endif %}
          {%- if has_citation_source_organisation -%}
            {#- Optional citation source organisation -#}
            {{ citation_source_organisation }}
          {%- endif %}
          </span>
      {%- endif %}
    </p>
  {% endif %}
{%- endmacro -%}

{#-
    Params
        - heading
            - title
                - text
            - link
                - html
 #}
{%- macro _quote_wrapper_heading_block(heading={}) %}
  {%- set heading_title_text = heading.get("title", {}).get("text", "") | trim -%}
  {%- set heading_link_html = heading.get("link", {}).get("html", "") | trim -%}
  {%- set has_heading_title = heading_title_text | length > 0 -%}
  {%- set has_heading_link = heading_link_html | length > 0 -%}
  {%- set has_heading_row = has_heading_title or has_heading_link -%}

  {% if has_heading_row -%}
    {#- Optional heading -#}
    <div class="p-section--shallow">
      <div class="grid-row">
        {%- if has_heading_title %}
          {#- Optional heading text -#}
          <div class="grid-col-2">
            <h2 class="p-muted-heading">{{ heading_title_text }}</h2>
          </div>
        {%- endif -%}

        {%- if has_heading_link %}
          {#- Optional heading link  -#}
          <div class="grid-col-2 grid-col-start-large-7">
            <p class="p-text--default">
              {{ heading_link_html }}
            </p>
          </div>
        {% endif -%}
      </div>
    </div>
  {% endif -%}
{%- endmacro %}

{#-
    Params
        - cta
            - html (String)
-#}
{%- macro _quote_wrapper_cta_block(cta={}) -%}
  {%- set cta_html = cta.get("html", "") | trim -%}
  {% if cta_html | length > 0 %}
    {#-  Optional CTA block -#}
    <div class="p-cta-block">
      {{- cta_html -}}
    </div>
  {%- endif -%}
{%- endmacro -%}

{#-
    This macro exposes the main contents of the quote wrapper pattern.
    this includes the citation, body (quote itself), CTA, and image.
    Params
        - contents
          - citation
              - name
              - organisation
              - title
          - body
              - size ("small" | "medium" | "large")
              - text (String)
          - cta
              - html (String)
          - image
              - html (String)
 #}
{%- macro _quote_wrapper_block_contents_column(contents={}) -%}
  {%- set citation = contents.get("citation", {}) -%}
  {%- set body = contents.get("body", {}) -%}
  {%- set cta = contents.get("cta", {}) -%}
  {%- set image = contents.get("image", {}) -%}
  {%- set has_citation = (citation.get("name") or citation.get("title") or citation.get("organisation")) | trim | length > 0 -%}
  {%- set has_cta = cta.get("html") | trim | length > 0 -%}
  {%- set has_image = image.get("html") | trim | length > 0 -%}
  {% if has_citation -%}
    {#-  When a citation is present, wrap the quote and citation in a nested grid to space them properly  -#}
    <div class="grid-row">
      <div class="grid-col-4">
        {{ _quote_wrapper_body_block(body=body) }}
      </div>
      <div class="grid-col-2">
        {{ _quote_wrapper_citation_block(citation=citation) }}
      </div>
    </div>
  {% else -%}
    {#-  When no citation is present, display quote body without a nested grid  -#}
    {{ _quote_wrapper_body_block(body=body) }}
  {% endif -%}

  {#-  Optional CTA and/or image block  -#}
  {{ _quote_wrapper_cta_block(cta=cta) }}

  {#-  Optional image block  -#}
  {{ image.get("html", "") }}
{%- endmacro -%}

{#-
    Params
        - image (required)
            - html (String)
-#}
{%- macro _quote_wrapper_block_signpost_column(signpost={}) -%}
  {%- set signpost_html = signpost.get("image", {}).get("html", "") | trim -%}
  {%- if signpost_html | length > 0 -%}
    <div class="p-section--shallow">
      {{ signpost_html | safe }}
    </div>
  {%- endif -%}
{%- endmacro -%}

{#-
    Params
        - signpost (optional)
            - image
                - html (String)
        - contents (required)
            - body
                - size ("small" | "medium" | "large")
                - text (str)
            - citation
                - name
                - organisation
                - title
            - cta
                - html (String)
            - image
                - html (String)
            - has_divider (boolean) (optional): Whether to show a divider above the contents column when it is stacked with preceding content. Defaults to false.
              - Note: passing a signpost will force this to True.
-#}
{%- macro vf_quote_block(signpost={}, contents={}) -%}
  {%- set signpost_html = signpost.get("image", {}).get("html", "") | trim -%}
  {%- set has_signpost = signpost_html | length > 0 -%}
  <div class="grid-row">
    {%- if has_signpost -%}
      <div class="grid-col-2 grid-col-medium-1">
        {%- if contents.get("has_divider", False) -%}
          <hr class="p-rule--muted u-hide--medium u-hide--large"/>
        {%- endif -%}
        {#- Optional signpost image -#}
        {{- _quote_wrapper_block_signpost_column(signpost=signpost) -}}
      </div>
    {%- endif -%}

    <div class="grid-col-6 grid-col-start-large-3 grid-col-medium-3 grid-col-start-medium-2">
      {%- if contents.get("has_divider", False) -%}
        <hr class="p-rule--muted u-hide--small"/>
      {%- endif -%}
      {{- _quote_wrapper_block_contents_column(contents=contents) -}}
    </div>
  </div>
{%- endmacro -%}

{#
  Params
  - title_text (string) (optional): The text to be displayed as the heading
  - quote_size (string) (required): The size of the quote. Possible values are 'small', 'medium', 'large'. Default is 'medium'.
  - quote_text (string) (required): The text of the quote. The macro will surround it with quotes, so there is no need to quote it yourself.
  - citation_source_name_text (string) (optional): The name of the source being quoted
  - citation_source_title_text (string) (optional): The title of the source being quoted
  - citation_source_organisation_text (string) (optional): The organisation associated with the source being quoted.
  - is_shallow (boolean) (optional): Whether the quote wrapper pattern should be displayed in a shallow section. Defaults to false.
  Slots
  - signpost_image (optional): The signpost_image of the source being quoted
  - heading_link (optional): Link to be displayed beside the heading text
  - cta (optional): Contents of the call to action block to be displayed below the quote
  - image (optional): An image to be displayed below the quote
#}
{%- macro vf_quote_wrapper(
  title_text="",
  quote_size="medium",
  quote_text="",
  citation_source_name_text="",
  citation_source_title_text="",
  citation_source_organisation_text="",
  is_shallow=False
) -%}
  {% set heading_link_content = caller('heading_link') %}
  {% set has_heading_link =  heading_link_content|trim|length > 0 %}
  {% set has_title = title_text|length > 0 %}
  {% set has_heading_row = has_title or has_heading_link %}
  {% set signpost_image_content = caller('signpost_image') %}
  {% set has_signpost_image = signpost_image_content|trim|length > 0 %}

  <div class="p-section{% if is_shallow %}--shallow{% endif %}">
    {#- pattern-level separator -#}
    {%- if has_heading_row -%}
      {{ vf_section_top_rule(top_rule_variant="default", is_fixed_width=True) }}
    {%- endif -%}
    {{- _quote_wrapper_heading_block(
        heading={
            "title": {
                "text": title_text
            },
            "link": {
                "html": heading_link_content
            }
        }
    ) -}}
    {#- TODO use `content.has_divider` only for non-first quote blocks once we support multiple quotes per pattern. -#}
    {#- TODO add shallow spacing between each quote block in the pattern once we support multiple quotes per pattern. -#}
    {{- vf_quote_block(
        signpost={
          "image": {
            "html": signpost_image_content
          }
        },
        contents={
            "body": {
                "size": quote_size,
                "text": quote_text
            },
            "citation": {
                "name": citation_source_name_text,
                "title": citation_source_title_text,
                "organisation": citation_source_organisation_text
            },
            "image": {
                "html": caller('image')
            },
            "cta": {
                "html": caller('cta')
            },
            "has_divider": not has_heading_row
        }
    ) -}}
  </div>
{% endmacro -%}