from django.contrib.auth import login, authenticate, get_user_model
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.contrib.auth.views import LoginView
from django.shortcuts import render, redirect
from django.urls import reverse
from django.views.generic import TemplateView, FormView

from user.form import SignUpForm


def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('/')
    else:
        form = SignUpForm()
    return render(request, 'signup.html', {'form': form})


class CustomAuthenticationForm(AuthenticationForm):

    def __init__(self, request=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['username'].label = ''
        self.fields['password'].label = ''
        self.fields['username'].widget.attrs['class'] = 'username'
        self.fields['password'].widget.attrs['class'] = 'username'
        self.fields['username'].widget.attrs['placeholder'] = 'username**'
        self.fields['password'].widget.attrs['placeholder'] = 'password**'


class CustomUserCreationForm(UserCreationForm):

    def __init__(self, request=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['username'].label = ''
        self.fields['password1'].label = ''
        self.fields['password2'].label = ''
        self.fields['username'].help_text = ''
        self.fields['password1'].help_text = ''
        self.fields['password2'].help_text = ''
        self.fields['username'].widget.attrs['class'] = 'username'
        self.fields['password1'].widget.attrs['class'] = 'username'
        self.fields['password2'].widget.attrs['class'] = 'username'
        self.fields['username'].widget.attrs['placeholder'] = 'username**'
        self.fields['password1'].widget.attrs['placeholder'] = 'password1**'
        self.fields['password2'].widget.attrs['placeholder'] = 'password2**'

    class Meta(UserCreationForm.Meta):
        model = get_user_model()


class UserLoginView(LoginView):
    template_name = 'login.html'
    form_class = CustomAuthenticationForm

    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        ctx['registration_form'] = CustomUserCreationForm()
        return ctx


class UserRegisterView(FormView):
    form_class = CustomUserCreationForm

    def form_valid(self, form):
        form.save()
        username = form.cleaned_data.get('username')
        raw_password = form.cleaned_data.get('password1')
        user = authenticate(username=username, password=raw_password)
        login(self.request, user)
        return redirect(reverse('enquiry:index'))
