from django.core.validators import EMPTY_VALUES
from django_filters import rest_framework as filters

from location import models


class CityBackFilter(filters.FilterSet):
    """Employee filter set."""

    search = filters.CharFilter(method='search_by_name')

    class Meta:
        """Meta class."""

        model = models.City
        fields = (
            'search',
        )

    def search_by_name(self, queryset, name, value):
        """Search by name or last name."""
        if value not in EMPTY_VALUES:
            return queryset.search_by_name(value)
        return queryset


class RegionFilter(filters.FilterSet):
    """Region filter set."""

    country_id = filters.CharFilter()
    sub_regions_by_region_id = filters.CharFilter(method='by_region')
    without_parent_region = filters.BooleanFilter(method='by_parent_region')

    class Meta:
        """Meta class."""
        model = models.Region
        fields = (
            'country_id',
            'sub_regions_by_region_id',
            'without_parent_region',
        )

    def by_region(self, queryset, name, value):
        """Search regions by sub region id."""
        if value not in EMPTY_VALUES:
            return queryset.sub_regions_by_region_id(value)

    def by_parent_region(self, queryset, name, value):
        """
        Search if region instance has a parent region..
        If True then show only Regions
        Otherwise show only Sub regions.
        """
        if value not in EMPTY_VALUES:
            return queryset.without_parent_region(value)
