from functools import partial
from typing import Optional, Callable

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

from src.ezgg_lan_manager.services.CateringService import CateringService
from src.ezgg_lan_manager.types.CateringOrder import CateringOrder, CateringOrderStatus
from src.ezgg_lan_manager.types.Seat import Seat

class CateringManagementOrderDisplayStatusButton(Component):
    status: CateringOrderStatus
    clicked_cb: Callable
    def build(self) -> Component:
        return Button(
            content=Text(
                CateringOrder.translate_order_status(self.status)
            ),
            shape="rectangle",
            on_press=partial(self.clicked_cb, self.status)
        )


class CateringManagementOrderDisplay(Component):
    order: CateringOrder
    seat: Optional[Seat]
    clicked_cb: Callable

    def format_order_status(self, status: CateringOrderStatus) -> Text:
        status_text = CateringOrder.translate_order_status(status)

        color = self.session.theme.warning_color
        if status == CateringOrderStatus.DELAYED or status == CateringOrderStatus.CANCELED:
            color = self.session.theme.danger_color
        elif status == CateringOrderStatus.COMPLETED:
            color = self.session.theme.success_color

        return Text(text=status_text, style=TextStyle(fill=color))

    async def status_button_clicked(self, new_status: CateringOrderStatus) -> None:
        if self.order.status == CateringOrderStatus.CANCELED:
            return

        if new_status == CateringOrderStatus.CANCELED:
            # ToDo: Hier sollten wir nochmal nachfragen ob der Bediener sich wirklich sicher ist,
            #       und anwarnen das eine stornierte Bestellung nicht ent-storniert werden kann.
            pass

        if self.order.status != new_status:
            if new_status == CateringOrderStatus.CANCELED:
                success = await self.session[CateringService].cancel_order(self.order)
            else:
                success = await self.session[CateringService].update_order_status(self.order.order_id, new_status)

            if success:
                self.order = CateringOrder(
                    order_id=self.order.order_id,
                    order_date=self.order.order_date,
                    status=new_status,
                    items=self.order.items,
                    customer=self.order.customer,
                    is_delivery=self.order.is_delivery
                )

    def build(self) -> Component:
        return PointerEventListener(
            content=Card(
                content=Column(
                    Row(
                        Text(f"ID: {self.order.order_id}", margin_left=0.3, margin_top=0.2, justify="center", style=TextStyle(font_size=1.2)),
                    ),
                    Row(
                        Text(f"Status: ", margin_left=0.3, margin_top=0.2),
                        self.format_order_status(self.order.status),
                        Spacer(),
                        Text(self.order.order_date.strftime("%d.%m. - %H:%M Uhr"), margin_right=0.3),
                    ),
                    Row(
                        Text(f"Gast: {self.order.customer.user_name}", margin_left=0.3),
                        Spacer(),
                        Text(f"Sitzplatz: {'-' if not self.seat else self.seat.seat_id}", margin_right=0.3),
                    ),
                    Row(
                        Text("Diese Bestellung wird:", margin_left=0.3, margin_bottom=0.5),
                        Spacer(),
                        Text("Geliefert" if self.order.is_delivery else "Abgeholt", margin_right=0.3, margin_bottom=0.5),
                    ),
                    Row(
                        CateringManagementOrderDisplayStatusButton(CateringOrderStatus.RECEIVED, self.status_button_clicked),
                        CateringManagementOrderDisplayStatusButton(CateringOrderStatus.CANCELED, self.status_button_clicked),
                        CateringManagementOrderDisplayStatusButton(CateringOrderStatus.EN_ROUTE, self.status_button_clicked)
                    ),
                    Row(
                        CateringManagementOrderDisplayStatusButton(CateringOrderStatus.READY_FOR_PICKUP, self.status_button_clicked),
                        CateringManagementOrderDisplayStatusButton(CateringOrderStatus.COMPLETED, self.status_button_clicked),
                        CateringManagementOrderDisplayStatusButton(CateringOrderStatus.DELAYED, self.status_button_clicked),
                    )
                ),
                color=self.session.theme.hud_color,
                colorize_on_hover=True,
                margin=1
            ),
            on_press=partial(self.clicked_cb, self.order)
        )