import logging

#from allauth.account.forms import SignupForm, ChangePasswordForm, ResetPasswordKeyForm, SetPasswordForm
from django import forms as d_forms
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from .ungleich_ldap import LdapManager

logger = logging.getLogger(__name__)

class MySignupForm(SignupForm):
    first_name = d_forms.CharField(max_length=30)
    last_name = d_forms.CharField(max_length=30)

    def custom_signup(self, request, user):
        user.first_name = self.cleaned_data["first_name"]
        user.last_name = self.cleaned_data["last_name"]
        user.save()
        if settings.LDAP_ENABLED:
            ldap_manager = LdapManager()
            try:
               user_exists_in_ldap, entries = ldap_manager.check_user_exists(user.username)
            except Exception:
                logger.exception("Exception occur while searching for user in LDAP")
            else:
                if not user_exists_in_ldap:
                    ldap_manager.create_user(user.username, user.password, user.first_name, user.last_name, user.email)

class MyResetPasswordKeyForm(ResetPasswordKeyForm):
    def save(self):
        ldap_manager = LdapManager()
        try:
            user_exists_in_ldap, entries = ldap_manager.check_user_exists(self.user.username)
            if not user_exists_in_ldap:
                super(MyResetPasswordKeyForm, self).save()
            else:
                if ldap_manager.change_password(entries[0].entry_dn, self.cleaned_data["password1"]):
                    super(MyResetPasswordKeyForm, self).save()
        except Exception:
            logger.exception("Exception occur while searching for user in LDAP")
            raise d_forms.ValidationError(_("An error occurred, please try again later"))

class MyChangePasswordForm(ChangePasswordForm):
    def save(self):
        ldap_manager = LdapManager()
        try:
            user_exists_in_ldap, entries = ldap_manager.check_user_exists(self.user.username)
            if not user_exists_in_ldap:
                super(MyChangePasswordForm, self).save()
            else:
                if ldap_manager.change_password(self.user.username, self.cleaned_data["password1"]):
                    super(MyChangePasswordForm, self).save()
        except Exception:
            logger.exception("Exception occur while searching for user in LDAP")
            raise d_forms.ValidationError(_("An error occurred, please try again later"))

class MySetPasswordForm(SetPasswordForm):
    def save(self):
        ldap_manager = LdapManager()
        try:
            user_exists_in_ldap, entries = ldap_manager.check_user_exists(self.user.username)
            if not user_exists_in_ldap:
                super(MySetPasswordForm, self).save()
            else:
                if ldap_manager.change_password(self.user.username, self.cleaned_data["password1"]):
                    super(MySetPasswordForm, self).save()
        except Exception:
            logger.exception("Exception occur while searching for user in LDAP")
            raise d_forms.ValidationError(_("An error occurred, please try again later"))
