from django import forms
from codelist.models import KonovaCode
from codelist.settings import CODELIST_COMPENSATION_ACTION_ID, CODELIST_BIOTOPES_ID


class DummyFilterInput(forms.HiddenInput):
    """ A dummy input widget

    Does not render anything. Can be used to keep filter logic using django_filter without having a pre defined
    filter widget being rendered to the template.

    """
    template_name = "konova/widgets/empty-dummy-input.html"


class TextToClipboardInput(forms.TextInput):
    template_name = "konova/widgets/text-to-clipboard-input.html"


class GenerateInput(forms.TextInput):
    """

    Provides a form group with a button at the end, which generates new content for the input.
    The url used to fetch new content can be added using the attrs like

    widget=GenerateInput(
        attrs={
            "url": reverse_lazy("app_name:view_name")
            ...
        }
    )

    """
    template_name = "konova/widgets/generate-content-input.html"


class TreeCheckboxSelectMultiple(forms.CheckboxSelectMultiple):
    """ Provides multiple selection of parent-child data

    """
    template_name = "konova/widgets/tree/checkbox/checkbox-tree-select.html"

    class meta:
        abstract = True


class TreeRadioSelect(forms.RadioSelect):
    """ Provides single selection of parent-child data

    """
    template_name = "konova/widgets/tree/radio/radio-tree-select.html"

    class meta:
        abstract = True


class KonovaCodeTreeCheckboxSelectMultiple(TreeCheckboxSelectMultiple):
    """ Provides multiple selection of KonovaCodes

    """
    filter = None

    def __init__(self, *args, **kwargs):
        self.code_list = kwargs.pop("code_list", None)
        self.filter = kwargs.pop("filter", {})
        super().__init__(*args, **kwargs)

    def get_context(self, name, value, attrs):
        context = super().get_context(name, value, attrs)
        codes = KonovaCode.objects.filter(
            **self.filter,
        )
        codes = [
            parent_code.add_children()
            for parent_code in codes
        ]
        context["codes"] = codes
        return context


class KonovaCodeTreeRadioSelect(TreeRadioSelect):
    """ Provides single selection of KonovaCode

    """
    filter = None

    def __init__(self, *args, **kwargs):
        self.code_list = kwargs.pop("code_list", None)
        self.filter = kwargs.pop("filter", {})
        super().__init__(*args, **kwargs)

    def get_context(self, name, value, attrs):
        context = super().get_context(name, value, attrs)
        codes = KonovaCode.objects.filter(
            **self.filter,
        )
        codes = [
            parent_code.add_children()
            for parent_code in codes
        ]
        context["codes"] = codes
        return context


class CompensationActionTreeCheckboxSelectMultiple(KonovaCodeTreeCheckboxSelectMultiple):
    """ Provides multiple selection of CompensationActions

    """
    filter = None

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.filter = {
            "code_lists__in": [CODELIST_COMPENSATION_ACTION_ID],
            "parent": None,
        }


class CompensationStateTreeRadioSelect(KonovaCodeTreeRadioSelect):
    """ Provides single selection of CompensationState

    """
    filter = None

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.filter = {
            "code_lists__in": [CODELIST_BIOTOPES_ID],
            "parent": None,
            "is_archived": False,
        }

    def get_context(self, name, value, attrs):
        context = super().get_context(name, value, attrs)
        codes = KonovaCode.objects.filter(
            **self.filter,
        )
        codes = [
            parent_code.add_children("short_name")
            for parent_code in codes
        ]
        context["codes"] = codes
        return context