import flet as ft
from dbActions.users import Users
from helpers.emails import send_gmail
import re
import string
import secrets

class ForgotPassword:
    def __init__(self, page: ft.Page, auth, login):
        self.page = page
        self.auth = auth
        self.login = login
        self.email = ft.TextField(label="E-mail")
        self.password = ft.TextField(label="Parola", password=True, can_reveal_password=True)
        self.repeat_password = ft.TextField(label="Repeta Parola", password=True, can_reveal_password=True)
        self.inserted_code = ft.TextField(label="Inserati codul primit prin e-mail")
        self.error_message = ft.Text("", color=ft.Colors.RED)
        self.placeholder = ft.Column([], horizontal_alignment=ft.CrossAxisAlignment.CENTER)
        self.otp_code = self._generate_numeric_code()
        self.user_manager = Users()
        self.trimite_code_placeholder = ft.Column(
            [
                self.email,
                ft.Button("Trimite cod", on_click=self.send_code_on_email, width=150),
            ],
            alignment=ft.MainAxisAlignment.CENTER,
            horizontal_alignment=ft.CrossAxisAlignment.CENTER
        )
        self.code_placeholder = ft.Column([], horizontal_alignment=ft.CrossAxisAlignment.CENTER)

    def _generate_numeric_code(self) -> str:
        digits = string.digits 
        return ''.join(secrets.choice(digits) for _ in range(6))

    def send_code_on_email(self, e):
        if self._is_valid_email(self.email.value):
            self.trimite_code_placeholder.controls = []
            self.trimite_code_placeholder.update()
            self.code_placeholder.controls.clear()
            self.code_placeholder.controls.append(self.inserted_code)
            self.code_placeholder.controls.append(ft.Button("Verifica", width=150, on_click=self.verfy_code))
            self.code_placeholder.update()
            #print(self.otp_code)
            send_gmail(
                to_email=self.email.value,
                subject="Codul de verificare",
                body=f"Codul de verificare este: {self.otp_code}"
            ) 
        
    
    def _are_all_fields_inserted(self, password=None, repeat_password=None):
        valid = True
        self.error_message.value = ''
        if not password:
            valid = False
        if not repeat_password:
            valid = False
        if not valid:
            self.error_message.value = "All fields are required!"
        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 = "The passwords don't match!"
            self.error_message.update()
            return False
        
    def _is_password_strong(self, password):
        self.error_message.value = ""
        if len(password) < 8:
            self.error_message.value = "Password must be at least 8 characters long!"
            self.error_message.update()
            return False
        if not re.search(r"[A-Z]", password):
            self.error_message.value = "Password must contain at least one uppercase letter!"
            self.error_message.update()
            return False
        if not re.search(r"[a-z]", password):
            self.error_message.value = "Password must contain at least one lowercase letter!"
            self.error_message.update()
            return False
        if not re.search(r"[0-9]", password):
            self.error_message.value = "Password must contain at least one digit!"
            self.error_message.update()
            return False
        if not re.search(r"[^a-zA-Z0-9]", password):
            self.error_message.value = "Password must contain at least one special character!"
            self.error_message.update()
            return False
        self.error_message.update()
        return True

    def on_save_btn_click(self, e):
        password = self.password.value
        repeat_password = self.repeat_password.value
        if self._are_all_fields_inserted(password, repeat_password):
            if self._is_password_strong(password):
                if self._check_repeat_password(password, repeat_password):
                    password_hash = self.user_manager.hash_password(password)
                    self.user_manager.update_password(self.email.value, password_hash)
                    self.error_message.value = "Parola a fost salvata cu success."
                    self.error_message.color = ft.Colors.GREEN
                    self.error_message.update()


    def verfy_code(self, e):
        self.code_placeholder.controls = []
        self.code_placeholder.update()
        inserted_code = self.inserted_code.value
        if inserted_code == self.otp_code:
            self.placeholder.controls.clear()
            self.placeholder.controls.append(self.password)
            self.placeholder.controls.append(self.repeat_password)
            self.placeholder.controls.append(
                ft.Button("Salveaza", width=150, on_click=self.on_save_btn_click)
            )
            self.placeholder.update()
            
        else:
            print(inserted_code)
            print(self.otp_code)

    def on_back_btn_click(self, e):
        self.auth.placeholder.content.clean()
        self.auth.placeholder.content = self.login.build()
        self.auth.placeholder.update()

    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 and self.user_manager.get_user(email):
            self.error_message.value = ""
            self.error_message.update()
            return True
        else: 
            self.error_message.value = "Va rugam inserati o adresa de e-mail cu un cont valid!"
            self.error_message.update()
            return False

    def build(self):
        return ft.Container(
            content=ft.Column(
                [
                    self.trimite_code_placeholder,
                    self.code_placeholder,
                    self.placeholder,
                    self.error_message,
                    ft.TextButton("Inapoi la Autentificare.", on_click=self.on_back_btn_click)
                ],
                horizontal_alignment=ft.CrossAxisAlignment.CENTER
            )
        )