"""This module holds the widgets for the ``common`` app"""

from django import forms
from django.conf import settings
from django.utils.safestring import mark_safe

from django.forms.widgets import DateInput

try:
    STATIC_URL_BASE = settings.STATIC_URL
except AttributeError:
    try:
        STATIC_URL_BASE = settings.MEDIA_URL
    except AttributeError:
        STATIC_URL_BASE = ''


class DateRangeInput(forms.MultiWidget):
    """Widget to select a range of date

    Notes
    -----
    Made to work with the ``django_filters.fields.DateRangeField`` field.

    """

    template_name = 'extras/form_widgets/date_range_input.html'

    def __init__(self, attrs=None):
        """Init the input with a tuple of two ``DatePicker`` widgets

        For the parameters, see ``django.forms.MultiWidget"""

        widgets = (DateInput(attrs=attrs), DateInput(attrs=attrs))

        super().__init__(widgets, attrs)

    def decompress(self, value):
        """Returns a list of decompressed values for the given compressed value.

        The given value can be assumed to be valid, but not necessarily non-empty.

        Parameters
        ----------
        value: list
            list of values, each corresponding to a widget

        """

        if value:
            return [value.start, value.stop]
        return [None, None]

