import hashlib
import random

from django import forms
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm, UserChangeForm
from authapp.models import ShopUser, UserActivation, ShopUserProfile


class ShopUserRegisterForm(UserCreationForm):
    class Meta:
        model = ShopUser
        fields = ('username', 'first_name', 'password1', 'password2', 'email', 'age', 'avatar')

    def __init__(self, *args, **kwargs):
        super(ShopUserRegisterForm, self).__init__(*args, **kwargs)
        for field_name, field in self.fields.items():
            field.widget.attrs['class'] = 'input-form-control'
            field.help_text = ''
        self.fields['avatar'].widget.attrs['class'] = 'input-form-file'

    def save(self):
        user = super(ShopUserRegisterForm, self).save()
        user.is_active = False
        salt = hashlib.sha1(str(random.random()).encode('utf8')).hexdigest()[:6]

        user_activation = UserActivation()
        user_activation.user = user
        user_activation.activation_key = hashlib.sha1((user.email + salt).encode('utf8')).hexdigest()
        user_activation.save()
        return user

    def clean_age(self):
        data = self.cleaned_data['age']
        if data < 14:
            raise forms.ValidationError("You are too young")

        return data


class ShopUserEditForm(UserChangeForm):
    class Meta:
        model = ShopUser
        fields = ('username', 'first_name', 'email', 'age', 'avatar')

    def __init__(self, *args, **kwargs):
        super(ShopUserEditForm, self).__init__(*args, **kwargs)
        for field_name, field in self.fields.items():
            field.widget.attrs['class'] = 'input-form-control'
            field.help_text = ''
        self.fields['avatar'].widget.attrs['class'] = 'input-form-file'

    # def clean_age(self):
    #     data = self.cleaned_data['age']
    #     if data < 14:
    #         raise forms.ValidationError("You are too young")
    #
    #     return data


class ShopUserLoginForm(AuthenticationForm):
    class Meta:
        model = ShopUser

        fields = ('username', 'password')

    def __init__(self, *args, **kwargs):
        super(ShopUserLoginForm, self).__init__(*args, **kwargs)
        for field_name, field in self.fields.items():
            field.widget.attrs['class'] = 'input-form-control'


class ShopUserProfileEditForm(forms.ModelForm):
    class Meta:
        model = ShopUserProfile
        fields = ('tags', 'aboutMe', 'gender')

    def __init__(self, *args, **kwargs):
        super(ShopUserProfileEditForm, self).__init__(*args, **kwargs)
        for field_name, field in self.fields.items():
            field.widget.attrs['class'] = 'input-form-control'
