from django import template
from django.utils.safestring import mark_safe

from agenda_culturel.models import ReferenceLocation

from .utils_extra import picto_from_name

register = template.Library()


@register.simple_tag
def show_suggested_positions(filter):
    filter.form.full_clean()

    if filter.is_filtered_by_position_radius():
        if filter.with_local_events():
            return mark_safe(
                ' <a class="small-location" role="button" href="'
                + filter.get_url_remove_local_events()
                + '">'
                + picto_from_name("minus-circle")
                + " cacher les événements de portée locale</a>"
            )
        else:
            return mark_safe(
                ' <a class="small-location" role="button" href="'
                + filter.get_url_add_local_events()
                + '">'
                + picto_from_name("plus-circle")
                + " voir les événements de portée locale</a>"
            )

    locations = (
        ReferenceLocation.objects.all()
        .filter(suggested_distance__isnull=False)
        .order_by("main", "name")
    )

    result = ""
    for loc in locations:
        result += (
            ' <a class="small-location" role="button" href="'
            + filter.get_url_add_suggested_position(loc)
            + '">'
            + picto_from_name("map-pin")
            + " "
            + loc.name
            + " "
            + str(loc.suggested_distance)
            + "km</a>"
        )

    return mark_safe(result)
