from django.core.exceptions import ValidationError
from abc import ABC, abstractmethod
from N3wtSchool import bdd, error

class Validator(ABC):

    formName=""
    data = {}
    errorFields={}

    @abstractmethod
    def validate(self):
        pass

    def getErrorFields(self):
        return self.errorFields

class ValidatorAuthentication(Validator):

    def __init__(self, **kwargs):
        self.data = kwargs['data']
        self.errorFields = {'email':'','password':''}

    def __str__ (self):
        return "VALIDATOR_AUTHENTICATION : " + self.data

    def validate(self):
        email = self.data.get("email")
        password = self.data.get("password")

        if email == None or not email:
            self.errorFields['email'] = error.returnMessage[error.INCOMPLETE]

        if password == None or not password:
            self.errorFields['password'] = error.returnMessage[error.INCOMPLETE]


        return (not self.errorFields['email'] and not self.errorFields['password']), self.errorFields

    def getErrorFields(self):
        return super().getErrorFields()


class ValidatorSubscription(Validator):

    def __init__(self, **kwargs):
        self.data = kwargs['data']
        self.errorFields = {'email':'','password1':'', 'password2':''}

    def __str__ (self):
        return "VALIDATOR_SUBSCRIPTION : " + self.data

    def validate(self):
        email = self.data.get("email")
        password1 = self.data.get("password1")
        password2 = self.data.get("password2")

        if password1 != password2:
            self.errorFields['password1'] = error.returnMessage[error.DIFFERENT_PASWWORD]
            self.errorFields['password2'] = error.returnMessage[error.DIFFERENT_PASWWORD]
        else:
            if email == None or not email:
                self.errorFields['email'] = error.returnMessage[error.INCOMPLETE]
            if password1 == None or not password1:
                self.errorFields['password1'] = error.returnMessage[error.INCOMPLETE]
            if password2 == None or not password2:
                self.errorFields['password2'] = error.returnMessage[error.INCOMPLETE]

        return (not self.errorFields['email'] and not self.errorFields['password1'] and not self.errorFields['password2']), self.errorFields

    def getErrorFields(self):
        return super().getErrorFields()

class ValidatorNewPassword(Validator):

    def __init__(self, **kwargs):
        self.data = kwargs['data']
        self.errorFields = {'email':''}

    def __str__ (self):
        return "VALIDATOR_NEW_PASSWORD : " + self.data

    def validate(self):
        email = self.data.get("email")

        if email == None or not email:
            self.errorFields['email'] = error.returnMessage[error.INCOMPLETE]

        return not self.errorFields['email'], self.errorFields

    def getErrorFields(self):
        return super().getErrorFields()

class ValidatorResetPassword(Validator):

    def __init__(self, **kwargs):
        self.data = kwargs['data']
        self.errorFields = {'password1':'', 'password2':''}

    def __str__ (self):
        return "VALIDATOR_RESET_PASSWORD : " + self.data

    def validate(self):
        password1 = self.data.get("password1")
        password2 = self.data.get("password2")

        if password1 != password2:
            self.errorFields['password1'] = error.returnMessage[error.DIFFERENT_PASWWORD]
            self.errorFields['password2'] = error.returnMessage[error.DIFFERENT_PASWWORD]

        else:
            if password1 == None or not password1:
                self.errorFields['password1'] = error.returnMessage[error.INCOMPLETE]
            if password2 == None or not password2:
                self.errorFields['password2'] = error.returnMessage[error.INCOMPLETE]

        return (not self.errorFields['password1'] and not self.errorFields['password2']), self.errorFields
        
    def getErrorFields(self):
        return super().getErrorFields()
