from django import forms
from django.utils.translation import ugettext_lazy as _

from .models import WinoseUser

# TODO: what a load of crap this is

class WinoseUserBaseForm(forms.ModelForm):
    class Meta:
        # base form hides the groups, the sa bool and the username (which is fixed anyway)
        fields = ['first_name', 'last_name', 'email', 'phone', 'mobile', 'gender', 'signature', 'position']
        exclude = ['password']  # always exclude the pw field... pointless
        model = WinoseUser

    def __init__(self, *args, **kwargs):
        self.current_user = kwargs.pop('current_user', None)
        super(WinoseUserBaseForm, self).__init__(*args, **kwargs)

    def clean(self):
        # for non sa, check that they don't create a user higher than their
        # own access level
        if self.cleaned_data.get('user_group', '') >= self.current_user.user_group and \
                not self.current_user.is_superuser:
            raise forms.ValidationError(
                {'user_group': _('you cannot grant higher or equal access rights than you possess yourself.')})
        super(WinoseUserBaseForm, self).clean()


# edit own profile, hide sa/groups
class WinoseUserUpdateFormSelf(WinoseUserBaseForm):
    class Meta:
        fields = ['first_name', 'last_name', 'email', 'phone', 'mobile', 'gender', 'signature', 'position']
        exclude = ['user_group', 'is_superuser', 'password']
        model = WinoseUser


# edit others as an sa, allow all
class WinoseUserUpdateFormOtherSA(WinoseUserBaseForm):
    class Meta:
        fields = ['first_name', 'last_name', 'email', 'phone', 'mobile', 'gender', 'user_group', 'is_superuser',
                  'signature', 'position']
        exclude = ['password']
        model = WinoseUser


# editing others as a non sa, don't allow setting setting sa, group rights
# escalation is taken of at the validation stage
class WinoseUserUpdateForm(WinoseUserBaseForm):
    class Meta:
        fields = ['first_name', 'last_name', 'email', 'phone', 'mobile', 'gender', 'user_group',
                  'signature', 'position']
        exclude = ['is_superuser', 'password']
        model = WinoseUser


# create a user
# this form is meant for non sa, which hides the sa field
class WinoseUserCreateFormBase(WinoseUserBaseForm):
    class Meta:
        fields = ['first_name', 'last_name', 'email', 'phone', 'mobile', 'gender', 'user_group', 'signature',
                  'position', 'username', 'password1', 'password2']
        exclude = ['is_superuser']
        model = WinoseUser

    password1 = forms.CharField(max_length=128,
                                label=_('Password'),
                                widget=forms.PasswordInput(),
                                required=True)

    password2 = forms.CharField(max_length=128,
                                label=_('Password repeat'),
                                widget=forms.PasswordInput(), required=True)

    current_user = None

    def clean(self):
        pw1 = self.cleaned_data.get('password1', None)
        pw2 = self.cleaned_data.get('password2', None)

        # check password repeat field
        if pw1 != pw2:
            raise forms.ValidationError({'password1': _('passwords do not match.')})
        else:
            # important, else the password isn't stored are not stored as a real hash
            self.instance.set_password(pw1)

        super(WinoseUserCreateFormBase, self).clean()


# user create form for admin, which allows to create other admins
class WinoseUserCreateFormSA(WinoseUserCreateFormBase):
    class Meta:
        fields = ['first_name', 'last_name', 'email', 'phone', 'mobile', 'gender', 'signature',
                  'username', 'user_group', 'is_superuser', 'password1', 'password2', 'position']
        model = WinoseUser
