from django import forms
from django.conf import settings
from django.contrib.auth import get_user_model, password_validation
from django.contrib.auth.forms import UsernameField, AuthenticationForm as BaseAuthenticationForm
from django.utils.translation import ugettext_lazy as _

from registration.forms import RegistrationForm as BaseRegistrationForm
from captcha.fields import CaptchaField

User = get_user_model()
username_label = _('%(service)s address' % {'service': settings.SITE_TITLE})


class LdhAuthenticationForm(BaseAuthenticationForm):
    username = UsernameField(
        label=username_label,
        max_length=254,
        widget=forms.TextInput(attrs={'autofocus': True}),
    )

    password = forms.CharField(
        label=_('Passphrase'),
        strip=False,
        widget=forms.PasswordInput,
    )


class RegistrationForm(BaseRegistrationForm):
    email = None  # override base definition to remove email field
    error_messages = {
        'password_mismatch': _("The two passphrase fields didn't match."),
    }
    password1 = forms.CharField(
        label=_("Passphrase"),
        strip=False,
        widget=forms.PasswordInput,
        help_text=_('A good passphrase is made of at least three long words.'),
    )
    password2 = forms.CharField(
        label=_("Passphrase confirmation"),
        widget=forms.PasswordInput,
        strip=False,
        help_text=_("Enter the same passphrase as before, for verification."),
    )

    def __init__(self, *args, **kwargs):
        super(RegistrationForm, self).__init__(*args, **kwargs)
        self.fields[User.USERNAME_FIELD].label = username_label

    class Meta(BaseRegistrationForm.Meta):
        model = User
        # override base definition to remove email field
        fields = (
            User.USERNAME_FIELD,
            'password1',
            'password2',
        )
