from django import forms
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from .models import Accounts, Media
from django.contrib.auth import authenticate, get_user_model
from multiupload.fields import MultiFileField

class BranchCreationForm(forms.ModelForm):
    """A form for creating new users. Includes all the required
    fields, plus a repeated password."""
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)

    class Meta:
        model = Accounts
        fields = ('username', 'branch_name', 'email', 'is_admin', 'location')

    def clean_password2(self):
        # Check that the two password entries match
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords don't match")
        return password2

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super().save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.save()
        return user



class BranchChangeForm(UserChangeForm):
    class Meta:
        model = Accounts
        fields = ('branch_id', 'username', 'branch_name', 'location', 'email', 'password', 'is_admin',)




class LoginForm(forms.Form):
    username = forms.CharField(max_length=255, widget=forms.TextInput(
        attrs={
        'class':'form-control',
        'placeholder':"Enter your username"
    }), required=True)
    password = forms.CharField(widget=forms.PasswordInput( attrs={
                "class": "form-control",
                'placeholder':"Password...."
             }), required=True)

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')
        user = authenticate(username=username, password=password)
        if not user or not user.is_active:
            raise forms.ValidationError("Sorry, that login was invalid. Please try again.")
        return self.cleaned_data

    def login(self, request):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')
        user = authenticate(request, username=username, password=password)
        return user



class ResetPasswordForm(UserCreationForm):
    password1 = forms.CharField(max_length=255,
        widget=forms.PasswordInput(
            attrs={
                 "class": "form-control form-control-xl",
                "placeholder":"Password"
            }
        )
    )
    password2 = forms.CharField(max_length=255,
        widget=forms.PasswordInput(
            attrs={
                 "class": "form-control form-control-xl",
                "placeholder":"Confirm Password"
            }
        )
    )
    class Meta:
        model = get_user_model()
        fields = ('password1', 'password2')



class UploadForm(forms.Form):
    file = MultiFileField(min_num=1, max_num=10)
    media_name = forms.CharField(widget=forms.TextInput(), required=True)
