# -*- encoding: utf-8 -*-

from django import forms
from django.forms import ModelForm

from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError

from . import models

class AppUserForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(AppUserForm, self).__init__(*args, **kwargs)
        self.fields['email'].disabled = True
        self.fields['email_contact_active'].disabled = True
        self.fields['dc_stamp'].disabled = True

    class Meta:
        model = models.AppUser
        fields = ['first_name','last_name',
            'city', 'postcode', 'district', 'kind',
            'email', 'email_contact', 'email_contact_active', 'dc_stamp']

    def clean_postcode(self):
        data = self.cleaned_data['postcode']
        if data is not None:
            if data < 10000 or data > 99999:
                raise ValidationError(_('PSČ musí být číslo mezi 10000 a 99999.'), code='invalid')
        return data

    def clean_first_name(self):
        data = self.cleaned_data['first_name']
        if data is not None:
            if len(data) > 30:
                raise ValidationError(_('Jméno může mít maximálně 30 znaků.'), code='invalid')
        return data

    def clean_last_name(self):
        data = self.cleaned_data['last_name']
        if data is not None:
            if len(data) > 30:
                raise ValidationError(_('Příjmení může mít maximálně 30 znaků.'), code='invalid')
        return data

    def clean_city(self):
        data = self.cleaned_data['city']
        if data is not None:
            if len(data) > 50:
                raise ValidationError(_('Město může mít maximálně 50 znaků.'), code='invalid')
        return data


class AppUserSsoForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(AppUserSsoForm, self).__init__(*args, **kwargs)
        self.fields['email'].disabled = True
        self.fields['email_contact_active'].disabled = True
        self.fields['dc_stamp'].disabled = True

    class Meta:
        model = models.AppUser
        fields = ['city', 'postcode', 'district', 'kind',
            'email', 'email_contact', 'email_contact_active', 'dc_stamp']

    def clean_postcode(self):
        data = self.cleaned_data['postcode']
        if data is not None:
            if data < 10000 or data > 99999:
                raise ValidationError(_('PSČ musí být číslo mezi 10000 a 99999.'), code='invalid')
        return data

    def clean_first_name(self):
        data = self.cleaned_data['first_name']
        if data is not None:
            if len(data) > 30:
                raise ValidationError(_('Jméno může mít maximálně 30 znaků.'), code='invalid')
        return data

    def clean_last_name(self):
        data = self.cleaned_data['last_name']
        if data is not None:
            if len(data) > 30:
                raise ValidationError(_('Příjmení může mít maximálně 30 znaků.'), code='invalid')
        return data

    def clean_city(self):
        data = self.cleaned_data['city']
        if data is not None:
            if len(data) > 50:
                raise ValidationError(_('Město může mít maximálně 50 znaků.'), code='invalid')
        return data



class AppRegEmailForm(ModelForm):
    class Meta:
        model = models.AppRegEmail
        fields = ['email', 'postcode', 'kind', 'interestedIn', 'data_consent']

    def clean_postcode(self):
        data = self.cleaned_data['postcode']
        if data is not None:
            if data < 10000 or data > 99999:
                raise ValidationError(_('PSČ musí být číslo mezi 10000 a 99999.'), code='invalid')
        return data

    def clean_data_consent(self):
        data = self.cleaned_data['data_consent']
        if data is None or data == False:
            raise ValidationError(_('Prosím, potvrďte souhlas se zpracováním osobních údajů.'), code='invalid')
        return data

    def clean_kind(self):
        data = self.cleaned_data['kind']
        if data is None:
            raise ValidationError(_('Prosím vyberte, o co máte zájem.'), code='invalid')
        return data


class AppRegFollowEmailForm(ModelForm):
    class Meta:
        model = models.AppRegEmail
        fields = ['email', 'data_consent']

    def clean_data_consent(self):
        data = self.cleaned_data['data_consent']
        if data is None or data == False:
            raise ValidationError(_('Prosím, potvrďte souhlas se zpracováním osobních údajů.'), code='invalid')
        return data



class UserFormForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(UserFormForm, self).__init__(*args, **kwargs)
        self.fields['skills'] = forms.ModelMultipleChoiceField(
            label=_("Moje dovednosti (dokážu pomoci s)"),
            widget=forms.CheckboxSelectMultiple,
            queryset=models.UserSkill.objects.all(),
            required=False)
        self.fields['topics'] = forms.ModelMultipleChoiceField(
            label=_("Zájmová témata"),
            widget=forms.CheckboxSelectMultiple,
            queryset=models.UserTopic.objects.all(),
            required=False)
        self.fields['regions'] = forms.ModelMultipleChoiceField(
            label=_("Zájmové regiony"),
            widget=forms.CheckboxSelectMultiple,
            queryset=models.InterestRegion.objects.all(),
            required=False)

    class Meta:
        model = models.UserForm
        fields = ['skills','skills_note', 'topics','regions']

class UserFollowFormForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(UserFollowFormForm, self).__init__(*args, **kwargs)
        self.fields['topics'] = forms.ModelMultipleChoiceField(
            label=_("Zájmová témata"),
            widget=forms.CheckboxSelectMultiple,
            queryset=models.UserTopic.objects.all(),
            required=False)
        self.fields['regions'] = forms.ModelMultipleChoiceField(
            label=_("Novinky zasílané e-mailem"),
            widget=forms.CheckboxSelectMultiple,
            queryset=models.InterestRegion.objects.all(),
            required=False)

    class Meta:
        model = models.UserForm
        fields = ['topics','regions']



class EmailVizitkaForm(forms.Form):
    name = forms.CharField(label=_('Jméno'), max_length=50)
    email = forms.CharField(label=_('E-mail'), max_length=50, required=False)
    nazev_funkce = forms.CharField(label=_('Název funkce'), max_length=50, required=False)
    soc_fb = forms.CharField(label=_('Facebook odkaz'), max_length=50, required=False)
    soc_in = forms.CharField(label=_('LinkedIn odkaz'), max_length=50, required=False)
    phone = forms.CharField(label=_('Telefon'), max_length=50, required=False)



##
#   Newsletters
##
class NewsletterForm(ModelForm):
    #def __init__(self, *args, **kwargs):
    #    super().__init__(*args, **kwargs)

    class Meta:
        model = models.Newsletter
        exclude = []

class NewsMsgForm(ModelForm):
    #def __init__(self, *args, **kwargs):
    #    super().__init__(*args, **kwargs)

    class Meta:
        model = models.NewsMsg
        fields = ['title', 'headerText', 'footerText',
            'delivery_ts', 'sent_ts',
            'testMailRecipients'] 

class NewsMsgBlockForm(ModelForm):
    #def __init__(self, *args, **kwargs):
    #    super().__init__(*args, **kwargs)

    class Meta:
        model = models.NewsMsgBlock
        exclude = ['newsmsg']


class Euro2019InterestForm(ModelForm):
    class Meta:
        model = models.Euro2019Interest
        exclude = ['createdStamp', 'dc_stamp']

    def clean_postcode(self):
        data = self.cleaned_data['postcode']
        if data is not None:
            if data < 10000 or data > 99999:
                raise ValidationError(_('PSČ musí být číslo mezi 10000 a 99999.'), code='invalid')
        return data

    def clean_data_consent(self):
        data = self.cleaned_data['data_consent']
        if data is None or data == False:
            raise ValidationError(_('Prosím, potvrďte souhlas se zpracováním osobních údajů.'), code='invalid')
        return data

