"""
Created on 24.11.2011

@author: christian
"""
import datetime

from django import forms


class DateInput(forms.widgets.DateInput):
    input_type = 'date'
    attrs = {'class': 'dateinput'}

    def __init__(self, attrs=None, **kwargs):
        forms.widgets.DateInput.__init__(self,
                                         attrs=attrs,
                                         format='%Y-%m-%d',
                                         **kwargs)


class NumberInput(forms.widgets.Input):
    input_type = 'number'


class TimeInput(forms.widgets.Select):
    def __init__(self, attrs=None, ):
        timeset = datetime.datetime(2000, 1, 1, 0, 0)
        choices = [('', '-------')]
        while timeset < datetime.datetime(2000, 1, 2, 0, 0):
            choices.append((
                timeset.strftime('%H:%M:%S'),
                timeset.strftime('%H:%M')
            ))
            timeset += datetime.timedelta(minutes=30)
        forms.widgets.Select.__init__(self, attrs=attrs, choices=choices)

    def render(self, name, value, attrs=None, choices=()):
        return forms.widgets.Select.render(self, name, value, attrs=attrs,
                                           choices=choices)


class SplitDateTimeWidget(forms.widgets.MultiWidget):
    """
    A Widget that splits datetime input into two <input type="text"> boxes.
    """

    def __init__(self, attrs=None, date_format='%Y-%m-%d'):
        widgets = (
            DateInput(attrs=attrs, format=date_format),
            TimeInput(attrs=attrs)
        )
        super(SplitDateTimeWidget, self).__init__(widgets, attrs)

    def decompress(self, value):
        if value:
            return [value.date(), value.time()]
        return [None, None]
