import flet as ft
from admin.category import Category
from admin.products import ProductsPage
from admin.banner import Banner
from admin.orders import OrdersPage
from admin.clients import Clients
from admin.fidelity_cards import FidelityCards
from admin.settings import Settings
from admin.inventory.inventory import Inventory
from admin.admin_chat import AdminChat

class Dashboard:
    def __init__(self, page: ft.Page):
        self.page = page
        # Hide client floating chat button when in admin dashboard
        # Home/landing page is responsible for re-creating it when needed.
        try:
            # If a floating_action_button exists (client chat), remove it for admin views
            if getattr(self.page, "floating_action_button", None) is not None:
                self.page.floating_action_button = None
        except Exception as e:
            # If anything goes wrong, fail silently; admin UI will still work.
            print(e)
        self.category = Category(self.page)
        self.placeholder = ft.Container(
            content=self.category.build(),
            padding=10,
            expand=True
        )
        self.rail = ft.NavigationRail(
            selected_index=0,
            label_type=ft.NavigationRailLabelType.ALL,
            min_width=100,
            #min_extended_width=400,
            leading=ft.Image('images/tainagustului.png', width=80),
            group_alignment=-0.9,
            destinations=[
                ft.NavigationRailDestination(
                    icon=ft.Icons.CATEGORY_OUTLINED,
                    selected_icon=ft.Icons.CATEGORY_ROUNDED,
                    label="Categori",
                ),
                ft.NavigationRailDestination(
                    icon=ft.Icons.ADD_BOX_OUTLINED,
                    selected_icon=ft.Icons.ADD_BOX_ROUNDED,
                    label="Produse",
                ),
                ft.NavigationRailDestination(
                    icon=ft.Icon(ft.Icons.MANAGE_ACCOUNTS_OUTLINED),
                    selected_icon=ft.Icon(ft.Icons.MANAGE_ACCOUNTS_ROUNDED),
                    label="Clienti",
                ),
                ft.NavigationRailDestination(
                    icon=ft.Icons.SHOPPING_CART_OUTLINED,
                    selected_icon=ft.Icon(ft.Icons.SHOPPING_CART_ROUNDED),
                    label_content=ft.Text("Comenzi"),
                ),
                ft.NavigationRailDestination(
                    icon=ft.Icons.SHOPIFY_OUTLINED,
                    selected_icon=ft.Icon(ft.Icons.SHOPIFY_ROUNDED),
                    label_content=ft.Text("Magazin"),
                ),
                ft.NavigationRailDestination(
                    icon=ft.Icons.IMAGE_OUTLINED,
                    selected_icon=ft.Icon(ft.Icons.IMAGE_ROUNDED),
                    label_content=ft.Text("Banner"),
                ),
                ft.NavigationRailDestination(
                    icon=ft.Icons.CHAT_OUTLINED,
                    selected_icon=ft.Icon(ft.Icons.CHAT),
                    label_content=ft.Text("Chat"),
                ),
                ft.NavigationRailDestination(
                    icon=ft.Icons.INVENTORY_2_OUTLINED,
                    selected_icon=ft.Icon(ft.Icons.INVENTORY_2),
                    label_content=ft.Text("Inventar"),
                ),
                ft.NavigationRailDestination(
                    icon=ft.Icons.SETTINGS_APPLICATIONS_OUTLINED,
                    selected_icon=ft.Icon(ft.Icons.SETTINGS_APPLICATIONS),
                    label_content=ft.Text("Setari"),
                ),
                ft.NavigationRailDestination(
                    icon=ft.Icons.LOGOUT_OUTLINED,
                    selected_icon=ft.Icon(ft.Icons.LOGOUT_ROUNDED),
                    label_content=ft.Text("Logout"),
                ),
            ],
            on_change=lambda e: self.navigate_to(e),
        )

    def navigate_to(self, e):
        index = e.control.selected_index
        match index:
            case 0:
                self.category = Category(self.page)
                self.placeholder.content = self.category.build()
                self.placeholder.update()
            case 1: 
                self.products = ProductsPage(self.page)
                self.placeholder.content = self.products.build()
                self.placeholder.update()
            case 2: 
                self.clients = Clients(self.page)
                self.placeholder.content = self.clients.build()
                self.placeholder.update()
            case 3: 
                self.orders = OrdersPage(self.page)
                self.placeholder.content = self.orders.build()
                self.placeholder.update()
            case 4: 
                self.page.launch_url('https://tainagustului.ro')
            case 5: 
                self.banner = Banner(self.page)
                self.placeholder.content = self.banner.build()
                self.placeholder.update()
            case 6:
                # self.fidelity_cards = FidelityCards(self.page)
                # self.placeholder.content = self.fidelity_cards.build()
                # self.placeholder.update()
                self.chat = AdminChat(self.page)
                self.placeholder.content = self.chat.build()
                self.placeholder.update()

            case 7:
                self.inventory = Inventory(self.page, self)
                self.placeholder.content = self.inventory.build()
                self.placeholder.update()
            case 8:
                self.settings = Settings(self.page, self)
                self.placeholder.content = self.settings.build()
                self.placeholder.update()
            case 9:
                self.page.client_storage.clear()
                self.page.session.clear()
                self.page.go('/')

    def build(self):
        if self.page.session.get("user") is None or self.page.session.get("user")['role'] != 'admin':
            return ft.Container(
                content=ft.Column(
                    [
                        ft.Text(
                            "Nu aveti drepturi sa accesati pagina de admin!", 
                            size=20, 
                            weight=ft.FontWeight.BOLD, 
                            text_align=ft.TextAlign.CENTER
                        ),
                        ft.Text("Va rugam sa va autentificati cu un rol de admin",text_align=ft.TextAlign.CENTER)
                    ],
                    alignment=ft.MainAxisAlignment.CENTER,
                    horizontal_alignment=ft.CrossAxisAlignment.CENTER
                )
            )
        else:
            return ft.Container(
                content=ft.Column(
                    [
                        ft.Row(
                            [   
                                self.rail,
                                ft.VerticalDivider(width=1),
                                self.placeholder
                            ],
                            expand=True
                        ),
                        #self.shop_page
                    ],
                    expand=True
                ),
                expand=True
            )