from typing import Optional, Callable

from rio import Component, event, Spacer, Card, Column, Text, TextStyle

from src.ezgg_lan_manager.services.ConfigurationService import ConfigurationService
from src.ezgg_lan_manager.services.UserService import UserService
from src.ezgg_lan_manager.components.DesktopNavigationButton import DesktopNavigationButton
from src.ezgg_lan_manager.components.NavigationSponsorBox import NavigationSponsorBox
from src.ezgg_lan_manager.components.UserInfoAndLoginBox import UserInfoAndLoginBox
from src.ezgg_lan_manager.types.User import User
from src.ezgg_lan_manager.types.UserSession import UserSession


class DesktopNavigation(Component):
    user: Optional[User] = None

    @event.on_populate
    async def on_populate(self) -> None:
        try:
            self.user = await self.session[UserService].get_user(self.session[UserSession].user_id)
        except KeyError:
            self.user = None

    def build(self) -> Component:
        lan_info = self.session[ConfigurationService].get_lan_info()
        user_info_and_login_box = UserInfoAndLoginBox(state_changed_cb=self.on_populate)
        navigation = [
            DesktopNavigationButton("News", "./news"),
            Spacer(min_height=0.7),
            DesktopNavigationButton(f"Über {lan_info.name} {lan_info.iteration}", "./overview"),
            DesktopNavigationButton("Ticket kaufen", "./buy_ticket"),
            DesktopNavigationButton("Sitzplan", "./seating"),
            DesktopNavigationButton("Catering", "./catering"),
            DesktopNavigationButton("Teilnehmer", "./guests"),
            DesktopNavigationButton("Teams", "./teams"),
            DesktopNavigationButton("Turniere", "./tournaments"),
            DesktopNavigationButton("FAQ", "./faq"),
            DesktopNavigationButton("Regeln & AGB", "./rules-gtc"),
            Spacer(min_height=0.7),
            DesktopNavigationButton("Discord", "https://discord.gg/8gTjg34yyH", open_new_tab=True),
            DesktopNavigationButton("Die EZ GG e.V.", "https://ezgg-ev.de/about", open_new_tab=True),
            Spacer(min_height=0.7)
        ]

        if self.user is not None and self.user.is_team_member:
            navigation.insert(0, DesktopNavigationButton("Adminbereich", "./admin", is_team_navigation=True))

        return Card(
            Column(
                Text(lan_info.name, align_x=0.5, margin_top=0.3, style=TextStyle(fill=self.session.theme.hud_color, font_size=1.9)),
                Text(f"Edition {lan_info.iteration}", align_x=0.5, style=TextStyle(fill=self.session.theme.hud_color, font_size=1.2), margin_top=0.3, margin_bottom=2),
                user_info_and_login_box,
                *navigation,
                Text("Unsere Sponsoren", align_x=0.5, style=TextStyle(fill=self.session.theme.hud_color, font_size=0.9), margin_bottom=0.5, margin_top=1),
                NavigationSponsorBox(img_name="crackz", url="https://www.crackz.gg/"),
                align_y=0
            ),
            color=self.session.theme.neutral_color,
            min_width=15,
            grow_y=True,
            corner_radius=(0.5, 0, 0, 0),
            margin_right=0.1
        )
