from django import template
from django.utils.html import format_html, mark_safe
from django.templatetags.static import static
from django.template.loader import render_to_string

register = template.Library()

@register.simple_tag
def br_stylesheets():
  return format_html(
      """
      <link rel="stylesheet" href="{}">
      <link rel="stylesheet" href="{}">
      """,
      static('dsgov/dist/css/styles.css'),
      static('dsgov/fonts/rawline/css/rawline.css')
  )

@register.simple_tag
def br_scripts():
  return format_html(
      """
      <script type="module" src="{}"></script>
      """, 
      static('dsgov/dist/js/bundle.js')
  )


@register.simple_tag
def br_button(text='', button_type='', style='', extra_classes='', icon_class=''):

  style_class = f'{style}' if style in ['primary', 'secondary'] else ''
  
  classes = f'br-button {style_class} {extra_classes}'.strip()
  
  icon_html = format_html('<i class="{}" aria-hidden="true"></i>', icon_class) if icon_class else ''
    
  return format_html('<button type="{}" class="{}">{}{}</button>', button_type, classes, icon_html, text)


@register.simple_tag
def br_tag(tag_text="", tag_id="", icon_class="", close_button="", close_icon_class="", button_extra_classes="", extra_classes="", density=""):
    
  icon_html = format_html('<i class="{}" aria-hidden="true"></i>', icon_class) if icon_class else ""

  close_button_html = ""
  if close_button:
    close_button_html = format_html( '<button class="br-button inverted circle {}" type="button" aria-label="Fechar" aria-describedby="{}" data-dismiss="{}">' '<i class="{}" aria-hidden="true"></i></button>', button_extra_classes, tag_id, tag_id, close_icon_class)

  interaction_class = "interaction" if close_button else ""

  tag_html = format_html('<span class="br-tag {} {} {}" id="{}" aria-describedby="{}">' '{}<span>{}</span>{}</span>', extra_classes, density, interaction_class, tag_id, tag_id, icon_html, tag_text, close_button_html)

  return format_html('<div class="d-flex align-items-center flex-wrap gap-2">{}</div>', tag_html)


@register.simple_tag
def br_input(input_type="", placeholder="", label="", input_id="", help_text="" ,extra_classes="", density="" , icon_class="", disabled=False, readonly=False, value=""):
    
  density_class = density if density in ['small', 'large'] else 'medium'
  classes = f'br-input {density_class} {extra_classes}'.strip()

  disabled_attr = 'disabled' if disabled else ''
  readonly_attr = 'readonly' if readonly else ''

  label_html = format_html('<label for="{}">{}</label>', input_id, label) if label else ''

  help_text_html = format_html('<p>{}</p>', help_text) if help_text else ''

  if icon_class:
      input_html = format_html('<div class="input-group">' '<div class="input-icon"><i class="{}" aria-hidden="true"></i></div>' '<input type="{}" id="{}" placeholder="{}" value="{}" class="{}" {} {}/>' '</div>', icon_class, input_type, input_id, placeholder, value, extra_classes, disabled_attr, readonly_attr)
  else:
      input_html = format_html('<input type="{}" id="{}" placeholder="{}" value="{}" class="{}" {} {}/>', input_type, input_id, placeholder, value, extra_classes, disabled_attr, readonly_attr)

  return format_html('<div class="{}">{label_html}{input_html}{help_text_html}</div>', classes, label_html=label_html, input_html=input_html, help_text_html=help_text_html)


@register.simple_tag
def br_header(title="", subtitle="", title_extra="", logo_url="", links="", search_placeholder="", show_login=True, extra_classes="", links_style="", label_button_login="", title_style="", items_style="", header_sign_style=""):

  context = {
        'title': title,
        'subtitle': subtitle,
        'title_extra' : title_extra,
        'logo_url': logo_url,
        'links': links,
        'search_placeholder': search_placeholder,
        'show_login': show_login,
        'extra_classes': extra_classes,
        'links_style': links_style,
        'label_button_login': label_button_login,
        'title_style': title_style,
        'items_style': items_style,
        'header_sign_style': header_sign_style
    }

  return render_to_string('components/header.html', context)

@register.simple_tag
def br_list(title, items):

    context = {
        "title": title,
        "items": items,
    }
    return render_to_string("components/list.html", context)

@register.simple_tag
def br_list_footer(title, items):

    context = {
        "title": title,
        "items": items,
    }
    return render_to_string("components/list_footer.html", context)

@register.simple_tag
def br_footer(copyright="", lists="", logo_url="", acesso_a_info_image="", emec_image=""):
   
   rendered_lists = [
      br_list_footer(list_data['title'], list_data['items']) for list_data in lists
   ]

   context = {
      'copyright': copyright,
      'rendered_lists': rendered_lists,
      "logo_url": logo_url,
      "emec_image": emec_image,
      "acesso_a_info_image": acesso_a_info_image
   }

   return render_to_string('components/footer.html', context)

@register.simple_tag
def br_carousel(imagens=""):

  context = {
      'imagens': imagens
  }

  return render_to_string('components/carousel.html', context)

@register.simple_tag
def br_menu():
  
  context = {
      
  }

  return render_to_string('components/menu.html', context)

@register.simple_tag
def br_breadcrumb():

  context = {
      
  }

  return render_to_string('components/breadcrumb.html', context)
