from django import forms
from django.utils import timezone
from django.contrib.auth.models import User


class LoginForm(forms.Form):

    username = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)

    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        self.fields['username'].label = 'Login'
        self.fields['password'].label = 'Password'

    def clean(self):
        username = self.cleaned_data['username']
        password = self.cleaned_data['password']
        if not User.objects.filter(username=username).exists():
            raise forms.ValidationError('A user with this name is not registered in the system!')

        user = User.objects.get(username=username)
        if user and not user.check_password(password):
            raise forms.ValidationError('Incorrect password')


class RegistrationForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)
    password_check = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = [
            'username',
            'password',
            'password_check',
            'first_name',
            'last_name',
            'email'
        ]

    def __init__(self, *args, **kwargs):
        super(RegistrationForm, self).__init__(*args, **kwargs)
        self.fields['username'].label = 'Login'
        self.fields['password'].label = 'Password'
        self.fields['password'].help_text = 'Create a password'
        self.fields['password_check'].label = 'Confirm password'
        self.fields['first_name'].label = 'Name'
        self.fields['last_name'].label = 'Surname'
        self.fields['email'].label = 'Your mail'
        self.fields['email'].help_text = 'Please provide a valid address'

    def clean(self):
        username = self.cleaned_data['username']
        password = self.cleaned_data['password']
        password_check = self.cleaned_data['password_check']
        email = self.cleaned_data['email']
        if User.objects.filter(username=username).exists():
            raise forms.ValidationError('A user with this login is already registered in the system!')
        if User.objects.filter(email=email).exists():
            raise forms.ValidationError('The user with this mailing address is already registered!')
        if password != password_check:
            raise forms.ValidationError('Your passwords do not match! Try it again!')


class OrderForm(forms.Form):
    name = forms.CharField()
    last_name = forms.CharField(required=False)
    phone = forms.CharField()
    buying_type = forms.ChoiceField(widget=forms.Select(), choices=([("self", "With no deliveries"),("delivery", "Delivery")]))
    date = forms.DateField(widget=forms.SelectDateWidget(), initial=timezone.now())
    address = forms.CharField(required=False)
    comments = forms.CharField(widget=forms.Textarea, required=False)

    def __init__(self, *args, **kwargs):
        super(OrderForm, self).__init__(*args, **kwargs)
        self.fields['name'].label = 'Name'
        self.fields['last_name'].label = 'Last name'
        self.fields['phone'].label = 'Сontact number'
        self.fields['phone'].help_text = 'Please provide a valid phone number where you can be contacted.'
        self.fields['buying_type'].label = 'Way of receiving'
        self.fields['address'].label = 'Delivery address'
        self.fields['address'].help_text = '*Be sure to specify the city!'
        self.fields['comments'].label = 'Сomments to the order'
        self.fields['date'].label = 'Delivery date'
        self.fields['date'].help_text = 'Delivery is made on the next day after placing the order. The manager will contact you in advance.'
