from django.utils.translation import ugettext_lazy as _
from django_filters import CharFilter

from geotrek.common.filters import StructureRelatedFilterSet, YearFilter
from geotrek.maintenance.filters import InterventionYearSelect
from .models import INFRASTRUCTURE_TYPES, Infrastructure, Signage


class InfrastructureYearSelect(InterventionYearSelect):
    label = _(u"Intervention year")


class InfrastructureFilterSet(StructureRelatedFilterSet):
    name = CharFilter(label=_('Name'), lookup_type='icontains')
    comments = CharFilter(label=_('Comments'), lookup_type='icontains')
    intervention_year = YearFilter(name='interventions_set__date',
                                   widget=InfrastructureYearSelect)

    def __init__(self, *args, **kwargs):
        super(InfrastructureFilterSet, self).__init__(*args, **kwargs)
        field = self.form.fields['type']
        field.queryset = field.queryset.exclude(type=INFRASTRUCTURE_TYPES.SIGNAGE)

        field = self.form.fields['type__type']
        all_choices = field.widget.choices
        all_choices = [c for c in all_choices if c[0] != INFRASTRUCTURE_TYPES.SIGNAGE]
        field.widget.choices = [('', _(u"Category"))] + all_choices

    class Meta(StructureRelatedFilterSet.Meta):
        model = Infrastructure
        fields = StructureRelatedFilterSet.Meta.fields + ['type__type', 'type', 'condition']


class SignageFilterSet(StructureRelatedFilterSet):
    name = CharFilter(label=_('Name'), lookup_type='icontains')
    comments = CharFilter(label=_('Comments'), lookup_type='icontains')
    intervention_year = YearFilter(name='interventions_set__date',
                                   widget=InfrastructureYearSelect)

    def __init__(self, *args, **kwargs):
        super(SignageFilterSet, self).__init__(*args, **kwargs)
        field = self.form.fields['type']
        field.queryset = field.queryset.filter(type=INFRASTRUCTURE_TYPES.SIGNAGE)

    class Meta(StructureRelatedFilterSet.Meta):
        model = Signage
        fields = StructureRelatedFilterSet.Meta.fields + ['type', 'condition']

@@ -508,32 +508,35 @@
 tains')%0A    
-comments
+description
  = CharFilte
@@ -542,32 +542,35 @@
 er(label=_('
-Comments
+Description
 '), lookup_t
@@ -1502,16 +1502,19 @@
     
-comments
+description
  = C
@@ -1536,16 +1536,19 @@
 =_('
-Comments
+Description
 '), 

fix field name in infrastructure filters