from typing import Callable

from rio import Component, Row, Text, TextStyle, Color, Rectangle, PointerEventListener

from src.ezgg_lan_manager.types.CateringOrder import CateringOrderStatus, CateringOrder

MAX_LEN = 24

class CateringOrderItem(Component):
    order: CateringOrder
    info_modal_cb: Callable

    def get_display_text_and_color_for_order_status(self, order_status: CateringOrderStatus) -> tuple[str, Color]:
        match order_status:
            case CateringOrderStatus.RECEIVED:
                return "In Bearbeitung", self.session.theme.success_color
            case CateringOrderStatus.DELAYED:
                return "Verspätet", Color.from_hex("eed202")
            case CateringOrderStatus.READY_FOR_PICKUP:
                return "Abholbereit", self.session.theme.success_color
            case CateringOrderStatus.EN_ROUTE:
                return "Unterwegs", self.session.theme.success_color
            case CateringOrderStatus.COMPLETED:
                return "Abgeschlossen", self.session.theme.success_color
            case CateringOrderStatus.CANCELED:
                return "Storniert", self.session.theme.danger_color
            case _:
                return "Unbekannt(wtf?)", self.session.theme.danger_color


    def build(self) -> Component:
        order_status, color = self.get_display_text_and_color_for_order_status(self.order.status)
        return PointerEventListener(
            Rectangle(
                content=Row(
                    Text(f"ID: {str(self.order.order_id):0>6}", align_x=0, overflow="wrap", min_width=10, style=TextStyle(fill=self.session.theme.background_color, font_size=0.9), margin_right=1),
                    Text(order_status, overflow="wrap", min_width=10, style=TextStyle(fill=color, font_size=0.9), margin_right=1),
                    Text(self.order.order_date.strftime("%d.%m. %H:%M"), style=TextStyle(fill=self.session.theme.background_color, font_size=0.9), align_x=1)
                ),
                fill=self.session.theme.primary_color,
                hover_fill=self.session.theme.hud_color,
                transition_time=0.1,
                cursor="pointer"
            ),
            on_press=lambda _: self.info_modal_cb(self.order),
        )
