from django import template
from edc_constants.constants import CANCELLED, CLOSED, HIGH_PRIORITY, NEW, OPEN
from edc_dashboard.utils import get_bootstrap_version

from ..choices import ACTION_STATUS
from ..helpers import ActionItemHelper
from ..site_action_items import site_action_items

register = template.Library()


@register.inclusion_tag(
    f"edc_action_item/bootstrap{get_bootstrap_version()}/add_action_item_popover.html"
)
def add_action_item_popover(subject_identifier, subject_dashboard_url) -> dict[str, str]:
    action_item_add_url = "edc_action_item_admin:edc_action_item_actionitem_add"
    show_link_to_add_actions = site_action_items.get_show_link_to_add_actions(
        subject_identifier
    )
    return dict(
        action_item_add_url=action_item_add_url,
        subject_identifier=subject_identifier,
        subject_dashboard_url=subject_dashboard_url,
        show_link_to_add_actions=show_link_to_add_actions,
    )


@register.inclusion_tag(
    f"edc_action_item/bootstrap{get_bootstrap_version()}/action_item_with_popover.html"
)
def action_item_with_popover(action_item_model_wrapper, tabindex):
    helper = ActionItemHelper(
        action_item=action_item_model_wrapper.object,
        href=action_item_model_wrapper.href,
    )
    context = helper.get_context()
    context.update(
        CANCELLED=[c[1] for c in ACTION_STATUS if c[0] == CANCELLED][0],
        CLOSED=[c[1] for c in ACTION_STATUS if c[0] == CLOSED][0],
        HIGH_PRIORITY=HIGH_PRIORITY,
        NEW=[c[1] for c in ACTION_STATUS if c[0] == NEW][0],
        OPEN=[c[1] for c in ACTION_STATUS if c[0] == OPEN][0],
        tabindex=tabindex,
    )
    return context
