import flet as ft
from pages.auth.register import Register
from pages.auth.forgot_password import ForgotPassword
import requests
from pages.auth.verify_code import VerifyCode

class Login:
    def __init__(self, page: ft.Page, auth):
        self.auth = auth
        self.page = page
        self.email = ft.TextField(label="E-mail", expand=True)
        self.password = ft.TextField(label='Parola', password=True, can_reveal_password=True, expand=True)
        self.error_message = ft.Text(color=ft.Colors.RED)
        self.verify_code = VerifyCode(self.page, self.auth)
        self.keep_me_auth = ft.Checkbox(label='Pastreaza-ma autentificat', on_change=self.on_keep_me_authenticated)

    def on_register_btn_click(self, e):
        self.register = Register(self.page, self, self.auth)
        self.auth.placeholder.content = self.register.build()

    def on_forgot_password_btn_click(self, e):
        self.forgot_password = ForgotPassword(self.page, self, self.auth)
        self.auth.placeholder.content = self.forgot_password.build()

    async def on_login_btn_click(self, e):
        email = self.email.value
        self.page.session.store.set('email',email)
        if not email:
            self.error_message.value = 'Toate campurile sunt obligatorii!'
            print('email not found')
            return
        password = self.password.value
        if not password:
            self.error_message.value = 'Toate campurile sunt obligatorii!'
            print('password not found')
            return
        base_url = self.page.session.store.get('api_base_url')

        response = requests.post(f'{base_url}/auth/login', json={"email": email, "password": password})
        if response.status_code == 200:
            self.auth.placeholder.content = self.verify_code.build()
        else:
            self.error_message.value = 'Credentiale incorecte!'
            print('no user found')

    async def set_value(self, key, value):
        await ft.SharedPreferences().set(key, str(value))

    def on_keep_me_authenticated(self, e):
        self.page.session.store.set('keep_me_authenticated', e.data)

    def build(self):
        return ft.Column(
            [
                ft.Text(
                        'Bine ati venit!', 
                        text_align=ft.TextAlign.CENTER, 
                        size=20, 
                        weight=ft.FontWeight.BOLD,
                        width=350
                ),
                self.email, 
                self.password,
                self.error_message,
                ft.Button(
                    'Autentificare',
                    on_click=self.on_login_btn_click
                ),
                ft.Row([self.keep_me_auth], alignment=ft.MainAxisAlignment.CENTER),
                ft.Row(
                    [
                        ft.TextButton(
                            'Creaza cont',
                            on_click=self.on_register_btn_click
                        ),
                        ft.TextButton(
                            'Ai uitat parola?',
                            on_click=self.on_forgot_password_btn_click
                        )
                    ],
                    alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
                )
            ],
            horizontal_alignment=ft.CrossAxisAlignment.CENTER,
        )