from decimal import Decimal
from typing import Optional, Callable

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

from src.ezgg_lan_manager import TicketingService
from src.ezgg_lan_manager.types.UserSession import UserSession


class SeatingPlanInfoBox(Component):
    show: bool
    purchase_cb: Callable
    is_booking_blocked: bool
    seat_id: Optional[str] = None
    seat_occupant: Optional[str] = None
    seat_price: Decimal = Decimal("0")
    is_blocked: bool = False
    has_user_ticket: bool = False
    booking_button_text: str = ""
    override_text: str = ""  # If this is set, all other functionality is disabled and the text is shown

    @event.on_populate
    async def check_ticket(self) -> None:
        try:
            user_id = self.session[UserSession].user_id
            user_ticket = await self.session[TicketingService].get_user_ticket(user_id)
            self.has_user_ticket = not (user_ticket is None)
            self.booking_button_text = "Buchen" if self.has_user_ticket else "Ticket kaufen"
            self.force_refresh()
        except KeyError:
            return

    async def purchase_clicked(self):
        if self.has_user_ticket:
            await self.purchase_cb()
        else:
            self.session.navigate_to("./buy_ticket")

    def build(self) -> Component:
        try:
            user_id = self.session[UserSession].user_id
        except KeyError:
            user_id = None

        if self.override_text:
            return Column(Text(self.override_text, margin=1,
                               style=TextStyle(fill=self.session.theme.neutral_color, font_size=1.4), overflow="wrap",
                               justify="center"), min_height=10)

        if not self.show:
            return Spacer()
        if self.is_blocked:
            return Column(Text(f"Sitzplatz gesperrt", margin=1,
                               style=TextStyle(fill=self.session.theme.neutral_color, font_size=1.4), overflow="wrap",
                               justify="center"), min_height=10)
        if self.seat_id is None and self.seat_occupant is None:
            return Column(
                Text(f"Sitzplatz auswählen...", margin=1, style=TextStyle(fill=self.session.theme.neutral_color),
                     overflow="wrap", justify="center"), min_height=10)
        return Column(
            Text(f"Dieser Sitzplatz ({self.seat_id}) ist gebucht von:", margin=1,
                 style=TextStyle(fill=self.session.theme.neutral_color), overflow="wrap", justify="center"),
            Text(f"{self.seat_occupant}", margin_bottom=1,
                 style=TextStyle(fill=self.session.theme.neutral_color, font_size=1.4), overflow="wrap",
                 justify="center"),
            min_height=10
        ) if self.seat_id and self.seat_occupant else Column(
            Text(f"Dieser Sitzplatz ({self.seat_id}) ist frei", margin=1,
                 style=TextStyle(fill=self.session.theme.neutral_color), overflow="wrap", justify="center"),
            Button(
                Text(
                    text=self.booking_button_text,
                    margin=1,
                    style=TextStyle(fill=self.session.theme.neutral_color, font_size=1.1),
                    overflow="wrap",
                    justify="center"
                ),
                shape="rounded",
                style="major",
                color="secondary",
                margin=1,
                grow_y=False,
                is_sensitive=not self.is_booking_blocked,
                on_press=self.purchase_clicked
            ) if user_id is not None else Text(f"Du musst eingeloggt sein um einen Sitzplatz zu buchen",
                                               margin=1,
                                               style=TextStyle(fill=self.session.theme.neutral_color),
                                               overflow="wrap", justify="center"),
            min_height=10
        )
