import re

class Validations:
    def __init__(self, error_message, page):
        self.page = page
        self.error_message = error_message

    def is_valid_email(self, email: str) -> bool:
        email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
        if re.fullmatch(email_regex, email) is not None:
            self.error_message.value = ""
            self.error_message.update()
            return True
        else: 
            self.error_message.value = "Va rugam inserati o adresa e email valida!"
            self.error_message.update()
            return False
    
    def are_all_fields_inserted(self, email=None, password=None, repeat_password=None):
        valid = True
        self.error_message.value = ''
        if not email:
            valid = False
        if not password:
            valid = False
        if not repeat_password:
            valid = False
        if not valid:
            self.error_message.value = "Toate campurile sunt obligatorii!"
        self.error_message.update()
        return valid
    
    def check_repeat_password(self, password, confirm_password):
        if password == confirm_password:
            self.error_message.value = ""
            self.error_message.update()
            return True
        else:
            self.error_message.value = "Parolele nu se potrivesc!"
            self.error_message.update()
            return False
        
    def is_password_strong(self, password):
        self.error_message.value = ""
        if len(password) < 8:
            self.error_message.value = "Parola trebuie sa aiba cel putin 8 caractere!"
            self.error_message.update()
            return False
        if not re.search(r"[A-Z]", password):
            self.error_message.value = "Parola trebuie sa contina cel putin o litera mare!"
            self.error_message.update()
            return False
        if not re.search(r"[a-z]", password):
            self.error_message.value = "Parola trebuie sa contina cel putin o litera mica!"
            self.error_message.update()
            return False
        if not re.search(r"[0-9]", password):
            self.error_message.value = "Parola trebuie sa contina cel putin o cifra!"
            self.error_message.update()
            return False
        if not re.search(r"[^a-zA-Z0-9]", password):
            self.error_message.value = "Parola trebuie sa contina cel putin un caracter special (de exemplu: !@#$%^&*)!"
            self.error_message.update()
            return False
        self.error_message.update()
        return True
