from typing import Callable
from decimal import Decimal

import rio
from rio import Component, Row, Text, IconButton, TextStyle

from src.ezgg_lan_manager import AccountingService

MAX_LEN = 24

class CateringCartItem(Component):
    article_name: str
    article_price: Decimal
    article_id: int
    list_id: int
    remove_item_cb: Callable

    @staticmethod
    def ellipsize_string(string: str) -> str:
        if len(string) <= MAX_LEN:
            return string

        return string[:MAX_LEN - 3] + "..."

    def build(self) -> rio.Component:
        return Row(
            Text(self.ellipsize_string(self.article_name), align_x=0, overflow="wrap", min_width=19, style=TextStyle(fill=self.session.theme.background_color, font_size=0.9)),
            Text(AccountingService.make_euro_string_from_decimal(self.article_price), style=TextStyle(fill=self.session.theme.background_color, font_size=0.9)),
            IconButton(icon="material/close", min_size=2, color=self.session.theme.danger_color, style="plain-text", on_press=lambda: self.remove_item_cb(self.list_id)),
            proportions=(19, 5, 2)
        )
