import flet as ft
from helpers.default_user import DefaultUser

class PreloadCard:
    def __init__(self, page: ft.Page):
        self.page = page
        self.ask_for_login_dialog = ft.AlertDialog(
            title="Va rugam sa va autentificati",
            content=ft.Text("Daca nu aveti un cont activ, puteti crea unul."),
            actions=[
                ft.FilledButton(
                    "Continua fara cont", 
                    bgcolor=ft.Colors.GREY,
                    on_click=self.on_cancel_go_to_login_btn_click),
                ft.FilledButton(
                    "Autentificare",
                    on_click=self.on_confirm_go_to_login_btn_click)
            ]
        )

    def on_cancel_go_to_login_btn_click(self, e):
        #self.page.close(self.ask_for_login_dialog)
        self.user = DefaultUser(self.page)
        self.page.session.set("user", self.user.default_user)
        self.page.go('/cos')

    def on_confirm_go_to_login_btn_click(self, e):
        #self.page.close(self.ask_for_login_dialog)
        self.page.go('/auth')

    def build(self):
        if not self.page.session.get("user"):
            return ft.Container(
                content=ft.Column(
                    [
                        ft.Text("Va rugam sa va autentificati", size=18, weight=ft.FontWeight.BOLD),
                        ft.Text("Daca nu aveti un cont activ, puteti crea unul."),
                        ft.Row(
                            [
                                ft.FilledButton(
                                    "Continua fara cont", 
                                    bgcolor=ft.Colors.GREY,
                                    on_click=self.on_cancel_go_to_login_btn_click),
                                ft.FilledButton(
                                    "Autentificare",
                                    on_click=self.on_confirm_go_to_login_btn_click)
                            ],
                            alignment=ft.MainAxisAlignment.CENTER
                        )
                        #self.page.open(self.ask_for_login_dialog)
                    ],
                    alignment=ft.MainAxisAlignment.CENTER,
                    horizontal_alignment=ft.CrossAxisAlignment.CENTER
                )
            )
        else:
            return ft.Container(
                content=self.page.go('/cos')
            )
            