from functools import partial
from typing import Callable, Optional
from decimal import Decimal

from rio import Component, Card, Column, Text, Row, Button, TextStyle, ProgressBar, event, Spacer

from src.ezgg_lan_manager import TicketingService
from src.ezgg_lan_manager.services.AccountingService import AccountingService
from src.ezgg_lan_manager.types.Ticket import Ticket


class TicketBuyCard(Component):
    description: str
    additional_info: str
    price: Decimal
    category: str
    pressed_cb: Callable
    is_enabled: bool
    total_tickets: int
    user_ticket: Optional[Ticket]
    available_tickets: int = 0

    @event.on_populate
    async def on_populate(self) -> None:
        self.available_tickets = await self.session[TicketingService].get_available_tickets_for_category(self.category)

    def build(self) -> Component:
        ticket_description_style = TextStyle(
            fill=self.session.theme.neutral_color,
            font_size=1.2,
        )
        ticket_additional_info_style = TextStyle(
            fill=self.session.theme.neutral_color,
            font_size=0.8
        )
        ticket_owned_style = TextStyle(
            fill=self.session.theme.success_color,
            font_size=0.8
        )

        try:
            progress = self.available_tickets / self.total_tickets
        except ZeroDivisionError:
            progress = 0
        progress_bar = ProgressBar(
            progress=progress,
            color=self.session.theme.success_color if progress > 0.25 else self.session.theme.danger_color,
            margin_right=1,
            grow_x=True
        )

        tickets_side_text = Text(
            f"{self.available_tickets}/{self.total_tickets}",
            align_x=1
        )

        return Card(
            Column(
                Text(self.description, margin_left=1, margin_top=1, style=ticket_description_style),
                Text("Du besitzt dieses Ticket!", margin_left=1, margin_top=1, style=ticket_owned_style) if self.user_ticket is not None and self.user_ticket.category == self.category else Spacer(),
                Text(self.additional_info, margin_left=1, margin_top=1, style=ticket_additional_info_style, overflow="wrap"),
                Row(
                    progress_bar,
                    tickets_side_text,
                    margin_top=1,
                    margin_left=1,
                    margin_right=1
                ),
                Row(
                    Text(f"{AccountingService.make_euro_string_from_decimal(self.price)}", margin_left=1, margin_top=1, grow_x=True),
                    Button(
                        Text("Kaufen", align_x=0.5, margin=0.4),
                        margin_right=1,
                        margin_top=1,
                        style="major",
                        shape="rounded",
                        on_press=partial(self.pressed_cb, self.category),
                        is_sensitive=self.is_enabled
                    ),
                    margin_bottom=1
                )
            ),
            margin_left=3,
            margin_right=3,
            margin_bottom=1,
            color=self.session.theme.hud_color,
            corner_radius=0.2
        )