from typing import Optional

from rio import Column, Component, event, TextStyle, Text, Spacer, Revealer, SwitcherBar, SwitcherBarChangeEvent, ProgressCircle

from src.ezgg_lan_manager import ConfigurationService, CateringService
from src.ezgg_lan_manager.components.CateringSelectionItem import CateringSelectionItem
from src.ezgg_lan_manager.components.MainViewContentBox import MainViewContentBox
from src.ezgg_lan_manager.components.ShoppingCartAndOrders import ShoppingCartAndOrders
from src.ezgg_lan_manager.services.RefreshService import RefreshService
from src.ezgg_lan_manager.types.CateringMenuItem import CateringMenuItemCategory, CateringMenuItem
from src.ezgg_lan_manager.types.UserSession import UserSession


class CateringPage(Component):
    show_cart = True
    all_menu_items: Optional[list[CateringMenuItem]] = None
    shopping_cart_and_orders: list[ShoppingCartAndOrders] = []

    @event.on_populate
    async def on_populate(self) -> None:
        await self.session.set_title(f"{self.session[ConfigurationService].get_lan_info().name} - Catering")
        self.all_menu_items = await self.session[CateringService].get_menu()

    async def on_user_logged_in_status_changed(self) -> None:
        self.force_refresh()

    async def on_switcher_bar_changed(self, _: SwitcherBarChangeEvent) -> None:
        await self.shopping_cart_and_orders[0].switch()

    @staticmethod
    def get_menu_items_by_category(all_menu_items: list[CateringMenuItem], category: Optional[CateringMenuItemCategory]) -> list[CateringMenuItem]:
        return list(filter(lambda item: item.category == category, all_menu_items))

    def build(self) -> Component:
        try:
            user_id = self.session[UserSession].user_id
        except KeyError:
            user_id = None
        if len(self.shopping_cart_and_orders) == 0:
            self.shopping_cart_and_orders.append(ShoppingCartAndOrders())
        if len(self.shopping_cart_and_orders) > 1:
            self.shopping_cart_and_orders.clear()
            self.shopping_cart_and_orders.append(ShoppingCartAndOrders())
        switcher_bar = SwitcherBar(
            values=["cart", "orders"],
            names=["Warenkorb", "Bestellungen"],
            selected_value="cart",
            margin_left=5,
            margin_right=5,
            margin_top=1,
            margin_bottom=1,
            color=self.session.theme.hud_color,
            on_change=self.on_switcher_bar_changed
        )

        shopping_cart_and_orders_container = MainViewContentBox(
            Column(
                Text(
                    text="Catering",
                    style=TextStyle(
                        fill=self.session.theme.background_color,
                        font_size=1.2
                    ),
                    margin_top=2,
                    margin_bottom=0,
                    align_x=0.5
                ),
                switcher_bar,
                self.shopping_cart_and_orders[0]
            )
        ) if user_id else Spacer()

        menu = [MainViewContentBox(
            ProgressCircle(
                color="secondary",
                align_x=0.5,
                margin_top=2,
                margin_bottom=2
            )
        )] if not self.all_menu_items else [MainViewContentBox(
            Revealer(
                header="Snacks",
                content=Column(
                    *[CateringSelectionItem(
                        article_name=catering_menu_item.name,
                        article_price=catering_menu_item.price,
                        article_id=catering_menu_item.item_id,
                        on_add_callback=self.shopping_cart_and_orders[0].on_add_item,
                        is_sensitive=(user_id is not None) and not catering_menu_item.is_disabled,
                        additional_info=catering_menu_item.additional_info,
                        is_grey=idx % 2 == 0
                    ) for idx, catering_menu_item in enumerate(self.get_menu_items_by_category(self.all_menu_items, CateringMenuItemCategory.SNACK))],
                ),
                header_style=TextStyle(
                    fill=self.session.theme.background_color,
                    font_size=1.2
                ),
                margin=1,
                align_y=0.5
            )
        ),
            MainViewContentBox(
                Revealer(
                    header="Frühstück",
                    content=Column(
                        *[CateringSelectionItem(
                            article_name=catering_menu_item.name,
                            article_price=catering_menu_item.price,
                            article_id=catering_menu_item.item_id,
                            on_add_callback=self.shopping_cart_and_orders[0].on_add_item,
                            is_sensitive=(user_id is not None) and not catering_menu_item.is_disabled,
                            additional_info=catering_menu_item.additional_info,
                            is_grey=idx % 2 == 0
                        ) for idx, catering_menu_item in enumerate(self.get_menu_items_by_category(self.all_menu_items, CateringMenuItemCategory.BREAKFAST))],
                    ),
                    header_style=TextStyle(
                        fill=self.session.theme.background_color,
                        font_size=1.2
                    ),
                    margin=1,
                    align_y=0.5
                )
            ),
            MainViewContentBox(
                Revealer(
                    header="Hauptspeisen",
                    content=Column(
                        *[CateringSelectionItem(
                            article_name=catering_menu_item.name,
                            article_price=catering_menu_item.price,
                            article_id=catering_menu_item.item_id,
                            on_add_callback=self.shopping_cart_and_orders[0].on_add_item,
                            is_sensitive=(user_id is not None) and not catering_menu_item.is_disabled,
                            additional_info=catering_menu_item.additional_info,
                            is_grey=idx % 2 == 0
                        ) for idx, catering_menu_item in enumerate(self.get_menu_items_by_category(self.all_menu_items, CateringMenuItemCategory.MAIN_COURSE))],
                    ),
                    header_style=TextStyle(
                        fill=self.session.theme.background_color,
                        font_size=1.2
                    ),
                    margin=1,
                    align_y=0.5
                )
            ),
            MainViewContentBox(
                Revealer(
                    header="Desserts",
                    content=Column(
                        *[CateringSelectionItem(
                            article_name=catering_menu_item.name,
                            article_price=catering_menu_item.price,
                            article_id=catering_menu_item.item_id,
                            on_add_callback=self.shopping_cart_and_orders[0].on_add_item,
                            is_sensitive=(user_id is not None) and not catering_menu_item.is_disabled,
                            additional_info=catering_menu_item.additional_info,
                            is_grey=idx % 2 == 0
                        ) for idx, catering_menu_item in enumerate(self.get_menu_items_by_category(self.all_menu_items, CateringMenuItemCategory.DESSERT))],
                    ),
                    header_style=TextStyle(
                        fill=self.session.theme.background_color,
                        font_size=1.2
                    ),
                    margin=1,
                    align_y=0.5
                )
            ),
            MainViewContentBox(
                Revealer(
                    header="Wasser & Softdrinks",
                    content=Column(
                        *[CateringSelectionItem(
                            article_name=catering_menu_item.name,
                            article_price=catering_menu_item.price,
                            article_id=catering_menu_item.item_id,
                            on_add_callback=self.shopping_cart_and_orders[0].on_add_item,
                            is_sensitive=(user_id is not None) and not catering_menu_item.is_disabled,
                            additional_info=catering_menu_item.additional_info,
                            is_grey=idx % 2 == 0
                        ) for idx, catering_menu_item in enumerate(self.get_menu_items_by_category(self.all_menu_items, CateringMenuItemCategory.BEVERAGE_NON_ALCOHOLIC))],
                    ),
                    header_style=TextStyle(
                        fill=self.session.theme.background_color,
                        font_size=1.2
                    ),
                    margin=1,
                    align_y=0.5
                )
            ),
            MainViewContentBox(
                Revealer(
                    header="Alkoholische Getränke",
                    content=Column(
                        *[CateringSelectionItem(
                            article_name=catering_menu_item.name,
                            article_price=catering_menu_item.price,
                            article_id=catering_menu_item.item_id,
                            on_add_callback=self.shopping_cart_and_orders[0].on_add_item,
                            is_sensitive=(user_id is not None) and not catering_menu_item.is_disabled,
                            additional_info=catering_menu_item.additional_info,
                            is_grey=idx % 2 == 0
                        ) for idx, catering_menu_item in enumerate(self.get_menu_items_by_category(self.all_menu_items, CateringMenuItemCategory.BEVERAGE_ALCOHOLIC))],
                    ),
                    header_style=TextStyle(
                        fill=self.session.theme.background_color,
                        font_size=1.2
                    ),
                    margin=1,
                    align_y=0.5
                )
            ),
            MainViewContentBox(
                Revealer(
                    header="Cocktails & Longdrinks",
                    content=Column(
                        *[CateringSelectionItem(
                            article_name=catering_menu_item.name,
                            article_price=catering_menu_item.price,
                            article_id=catering_menu_item.item_id,
                            on_add_callback=self.shopping_cart_and_orders[0].on_add_item,
                            is_sensitive=(user_id is not None) and not catering_menu_item.is_disabled,
                            additional_info=catering_menu_item.additional_info,
                            is_grey=idx % 2 == 0
                        ) for idx, catering_menu_item in enumerate(self.get_menu_items_by_category(self.all_menu_items, CateringMenuItemCategory.BEVERAGE_COCKTAIL))],
                    ),
                    header_style=TextStyle(
                        fill=self.session.theme.background_color,
                        font_size=1.2
                    ),
                    margin=1,
                    align_y=0.5
                )
            ),
            MainViewContentBox(
                Revealer(
                    header="Shots",
                    content=Column(
                        *[CateringSelectionItem(
                            article_name=catering_menu_item.name,
                            article_price=catering_menu_item.price,
                            article_id=catering_menu_item.item_id,
                            on_add_callback=self.shopping_cart_and_orders[0].on_add_item,
                            is_sensitive=(user_id is not None) and not catering_menu_item.is_disabled,
                            additional_info=catering_menu_item.additional_info,
                            is_grey=idx % 2 == 0
                        ) for idx, catering_menu_item in enumerate(self.get_menu_items_by_category(self.all_menu_items, CateringMenuItemCategory.BEVERAGE_SHOT))],
                    ),
                    header_style=TextStyle(
                        fill=self.session.theme.background_color,
                        font_size=1.2
                    ),
                    margin=1,
                    align_y=0.5
                )
            ),
            MainViewContentBox(
                Revealer(
                    header="Sonstiges",
                    content=Column(
                        *[CateringSelectionItem(
                            article_name=catering_menu_item.name,
                            article_price=catering_menu_item.price,
                            article_id=catering_menu_item.item_id,
                            on_add_callback=self.shopping_cart_and_orders[0].on_add_item,
                            is_sensitive=(user_id is not None) and not catering_menu_item.is_disabled,
                            additional_info=catering_menu_item.additional_info,
                            is_grey=idx % 2 == 0
                        ) for idx, catering_menu_item in enumerate(self.get_menu_items_by_category(self.all_menu_items, CateringMenuItemCategory.NON_FOOD))],
                    ),
                    header_style=TextStyle(
                        fill=self.session.theme.background_color,
                        font_size=1.2
                    ),
                    margin=1,
                    align_y=0.5
                )
            )]

        return Column(
            # SHOPPING CART
            shopping_cart_and_orders_container,
            # ITEM SELECTION
            *menu,
            align_y=0
        )
