from django import forms
from extras.models import CustomField
from .cf_types import ExtendedCustomFieldTypeChoices
from .models import TagidCustomField


class TagIdInput(forms.TextInput):
    template_name = "tagid_input.html"


_original_to_form_field = CustomField.to_form_field

def _tagid_to_form_field(self, *args, **kwargs):
    if getattr(self, "type", None) == ExtendedCustomFieldTypeChoices.TYPE_TAGID:
        return forms.CharField(
            label=self.label or self.name,
            required=self.required,
            initial=self.default,
            help_text=self.description,
            widget=TagIdInput(attrs={
                "class": "form-control",
                "data_cf_id": self.id,   
            }),
        )
    return _original_to_form_field(self, *args, **kwargs)

# Activation du patch
CustomField.to_form_field = _tagid_to_form_field
