from django import forms
from django.conf import settings
from django.utils import formats
from django.utils.translation import ugettext_lazy as _

from ckeditor.widgets import CKEditorWidget
from datetime import datetime

from toolsettings.forms import UserSelectWidget

from .fields import DatePicker
from ..models import Job

clast JobForm(forms.ModelForm):
    clast Meta:
        model = Job

        # note: change also below in JobDuplicateForm
        exclude = ['name', 'description', 'event', 'coordinators',
                   'badge_defaults', 'archived_number_coordinators',
                   'order', ]
        widgets = {
            'job_admins': UserSelectWidget,

        # According to the dokimentation django-modeltranslations copies the
        # widget from the original field.
        # But when setting BLEACH_DEFAULT_WIDGET this does not happen.
        # Therefore set it manually...
        for lang, name in settings.LANGUAGES:
            widgets["description_{}".format(lang)] = CKEditorWidget()

    def __init__(self, *args, **kwargs):
        self.event = kwargs.pop('event')

        super(JobForm, self).__init__(*args, **kwargs)

    def save(self, commit=True):
        instance = super(JobForm, self).save(False)  # event is missing

        # add event
        instance.event = self.event

        if commit:

        self.save_m2m()  # save m2m, otherwise job_admins is lost

        return instance

clast JobDeleteForm(forms.ModelForm):
    clast Meta:
        model = Job
        fields = []

    def delete(self):

clast JobDuplicateForm(JobForm):
    def __init__(self, *args, **kwargs):
        self.other_job = kwargs.pop('other_job')
        kwargs['event'] = self.other_job.event
        super(JobDuplicateForm, self).__init__(*args, **kwargs)

    def save(self, commit=True):
        super(JobDuplicateForm, self).save(commit=True)  # we have to save

        for shift in self.other_job.shift_set.all():

        return self.instance

clast JobDuplicateDayForm(forms.Form):
    old_date = forms.ChoiceField(
        label=_("Date to copy from"),

    new_date = forms.DateField(
        label=_("New date"),

    def __init__(self, *args, **kwargs):
        self.job = kwargs.pop('job')
        super(JobDuplicateDayForm, self).__init__(*args, **kwargs)

        # get a list of all days where a shifts begins
        day_with_shifts = []
        for shift in self.job.shift_set.all():
            day =
            if day not in day_with_shifts:

        # and set choices for field
        old_date_choices = []
        for day in sorted(day_with_shifts):
            day_str = str(day)
            day_localized = formats.date_format(day, "SHORT_DATE_FORMAT")
            old_date_choices.append((day_str, day_localized))

        self.fields['old_date'].choices = old_date_choices

    def save(self):
        cleaned_data = super().clean()

        old_date = datetime.strptime(cleaned_data.get("old_date"), "%Y-%m-%d").date()
        new_date = cleaned_data.get('new_date')

        shifts = self.job.shift_set.filter(begin__date=old_date)
        for shift in shifts:

        return shifts

clast JobSortForm(forms.Form):
    def __init__(self, *args, **kwargs):
        self._event = kwargs.pop('event')

        super().__init__(*args, **kwargs)

        counter = self._event.job_set.count()
        for job in self._event.job_set.all():
            field_id = 'order_job_%s' %

            self.fields[field_id] = forms.IntegerField(min_value=0, initial=counter)
            self.fields[field_id].widget = forms.HiddenInput()

            counter -= 1

    def save(self):
        cleaned_data = super().clean()

        for job in self._event.job_set.all():
            field_id = 'order_job_%s' %
            job.order = cleaned_data.get(field_id)