import flet as ft
from helpers.validations import Validations
import requests
import time

class Register:
    def __init__(self, page, login, auth):
        self.page = page
        self.login = login
        self.auth = auth
        
        self.email = ft.TextField(label="E-mail", expand=True)
        self.password = ft.TextField(label="Parola", password=True, can_reveal_password=True, expand=True)
        self.confirm_password = ft.TextField(label="Confirmați parola", password=True, can_reveal_password=True, expand=True)
        self.error_message = ft.Text(color=ft.Colors.RED)

        self.validation = Validations(self.error_message, self.page)

    def on_register_btn_click(self, e):
        email = self.email.value
        password = self.password.value
        repeat_password = self.confirm_password.value
        if self.validation.are_all_fields_inserted(email, password, repeat_password):
            print("All fileds are inserted")
            if self.validation.is_valid_email(email):
                print("Email valid", email)
                if self.validation.is_password_strong(password):
                    print("Password is strong")
                    if self.validation.check_repeat_password(password, repeat_password):
                        print('Password Valid!')
                        self.error_message.value = "Validam datele, va rugam asteptati!"
                        self.error_message.color = ft.Colors.WHITE
                        self.error_message.update()
                        API_BASE_URL = self.page.session.store.get('api_base_url')
                        response = requests.post(f"{API_BASE_URL}/auth/register", json={
                            "workspace_id": 0,
                            "email": self.email.value,
                            "password": self.password.value
                        })
                        if response.status_code == 201:
                            self.error_message.value = "Inregistrare finalizata, acum va puteti autentifica!"
                            self.error_message.color = ft.Colors.WHITE
                            self.error_message.update()
                            #time.sleep(3)
                            #self.auth.placeholder.content = self.login.build()
                        else:
                            self.error_message.value = "Exista deja un cont pentru aceasta adresa de e-mail"
                            self.error_message.update()


    def on_go_to_login_btn_click(self, e):
        self.auth.placeholder.content = self.login.build()

    
    def build(self):
        return ft.Column(
            [
                ft.Text(
                    "Creaza cont", 
                    text_align=ft.TextAlign.CENTER, 
                    size=20, 
                    weight=ft.FontWeight.BOLD,
                    width=350
                ),
                self.email,
                self.password,
                self.confirm_password,
                self.error_message,
                ft.Button(
                    "Inregistreaza-te", 
                    on_click=self.on_register_btn_click
                ),
                ft.Text(),
                ft.TextButton(
                    "Accesati Autentificare", 
                    on_click=self.on_go_to_login_btn_click,
                    width=350
                )
            ],
            horizontal_alignment=ft.CrossAxisAlignment.CENTER,
        )