from functools import partial
from typing import Callable, Optional

from rio import Column, Row, Text, Button, Component, Icon, Popup, Rectangle, Color, Tooltip, PointerEventListener, PointerEvent, ProgressCircle

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

ICONS_BY_STATUS = {
    CateringOrderStatus.RECEIVED: "material/move_to_inbox",
    CateringOrderStatus.DELAYED: "material/hourglass_top",
    CateringOrderStatus.READY_FOR_PICKUP: "material/takeout_dining",
    CateringOrderStatus.EN_ROUTE: "material/local_shipping",
    CateringOrderStatus.COMPLETED: "material/check_circle",
    CateringOrderStatus.CANCELED: "material/cancel",
}


class StatusChangeButton(Component):
    status: CateringOrderStatus
    clicked_cb: Callable

    def build(self) -> Component:
        return Tooltip(
            anchor=PointerEventListener(
                content=Rectangle(
                    fill=Color.TRANSPARENT,
                    content=Column(
                        Icon(
                            icon=ICONS_BY_STATUS[self.status]
                        )
                    ),
                    stroke_width=0.1,
                    stroke_color=Color.TRANSPARENT,
                    hover_stroke_width=0.1,
                    hover_stroke_color=Color.BLACK
                ),
                on_press=partial(self.clicked_cb, self.status)
            ),
            tip=Text(text=CateringOrder.translate_order_status(self.status)),
            position="top"
        )


class StatusChangePopup(Component):
    anchor: Component
    popup_open: bool
    status_should_change_cb: Callable
    response: Optional[str] = None
    is_loading: bool = False

    async def handle_button_clicked(self, status: CateringOrderStatus, _: PointerEvent) -> None:
        self.is_loading = True
        self.response = await self.status_should_change_cb(status)
        self.is_loading = False

    def close(self) -> None:
        self.popup_open = False

    def build(self) -> Component:
        if self.is_loading:
            content = Row(
                ProgressCircle(margin=1)
            )
        elif self.response:
            content = Row(
                Text(text=self.response, justify="center", overflow="wrap", margin=1)
            )
        else:
            content = Row(
                StatusChangeButton(CateringOrderStatus.RECEIVED, self.handle_button_clicked),
                StatusChangeButton(CateringOrderStatus.DELAYED, self.handle_button_clicked),
                StatusChangeButton(CateringOrderStatus.READY_FOR_PICKUP, self.handle_button_clicked),
                StatusChangeButton(CateringOrderStatus.EN_ROUTE, self.handle_button_clicked),
                StatusChangeButton(CateringOrderStatus.COMPLETED, self.handle_button_clicked),
                StatusChangeButton(CateringOrderStatus.CANCELED, self.handle_button_clicked),
                spacing=0.5,
                margin=0.5
            )
        return Popup(
            anchor=self.anchor,
            content=Rectangle(
                content=Column(
                    content,
                    Button(content=Text(text="Abbrechen", justify="center", fill=self.session.theme.secondary_color), shape="rectangle", style="colored-text", on_press=self.close),
                    proportions=[2.5, 1]
                ),
                fill=self.session.theme.hud_color,
                min_width=34,
                min_height=8.3
            ),
            is_open=self.popup_open
        )