from django.contrib.auth import password_validation
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit

from users.validators import usernameValidator

class UserRegisterForm(UserCreationForm):
    field_order = ("username", "password1", "password2")

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'id-register'
        self.helper.form_class = 'label'
        self.helper.form_method = 'post'
        self.helper.form_action = '/users/register'
        self.helper.add_input(Submit('submit', 'Create'))

    username = forms.CharField(min_length=3,
                               max_length=16,
                               label="Username",
                               required=True,
                               validators=[usernameValidator,],
                               help_text=_("3-16 chars, alphanumeric and _- pls"))

    password1 = forms.CharField(
        label="Password",
        required=True,
        strip=False,
        widget=forms.PasswordInput(attrs={"autocomplete": "new-password"}),
        #help_text=password_validation.password_validators_help_text_html(),
    )
    password2 = forms.CharField(
        #label="Password2",
        required=True,
        widget=forms.PasswordInput(attrs={"autocomplete": "new-password"}),
        strip=False,
        # help_text=_("yes again"),
    )

    class Meta:
        model = User
        fields = [
            "username",
        ]

class UserLoginForm(AuthenticationForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_id = 'id-login'
        self.helper.form_class = 'label'
        self.helper.form_method = 'post'
        self.helper.form_action = '/users/login'
        self.helper.add_input(Submit('submit', 'Login'))

    username = forms.CharField(min_length=3,
                               max_length=16,
                               label="Username",
                               required=True)

    class Meta:
        model = User
        fields = [
            "username",
        ]