from decimal import Decimal
from typing import Callable

import rio
from rio import Component, Row, Text, IconButton, TextStyle, Column, Spacer, Card, Color

from src.ezgg_lan_manager import AccountingService

MAX_LEN = 24


class CateringSelectionItem(Component):
    article_name: str
    article_price: Decimal
    article_id: int
    on_add_callback: Callable
    is_sensitive: bool
    additional_info: str
    is_grey: bool

    @staticmethod
    def split_article_name(article_name: str) -> tuple[str, str]:
        if len(article_name) <= MAX_LEN:
            return article_name, ""
        top, bottom = "", ""
        words = article_name.split(" ")
        last_word_added = ""
        while len(top) <= MAX_LEN:
            w = words.pop(0)
            top += f" {w}"
            last_word_added = w

        top = top.replace(last_word_added, "")
        bottom = f"{last_word_added} " + " ".join(words)

        return top.strip(), bottom.strip()

    def build(self) -> rio.Component:
        article_name_top, article_name_bottom = self.split_article_name(self.article_name)

        return Card(
            content=Column(
                Row(
                    Text(article_name_top, 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/add" if self.is_sensitive else "material/do_not_disturb_on_total_silence",
                        min_size=2,
                        color=self.session.theme.success_color if self.is_sensitive else self.session.theme.danger_color,
                        style="colored-text",
                        on_press=lambda: self.on_add_callback(self.article_id),
                        is_sensitive=self.is_sensitive
                    ),
                    proportions=(19, 5, 2),
                    margin_bottom=0
                ),
                Spacer() if not article_name_bottom else Text(article_name_bottom, align_x=0, overflow="wrap",
                                                              min_width=19,
                                                              style=TextStyle(fill=self.session.theme.background_color,
                                                                              font_size=0.9)),
                Row(
                    Text(
                        self.additional_info,
                        align_x=0,
                        overflow="wrap",
                        min_width=19,
                        style=TextStyle(fill=self.session.theme.background_color, font_size=0.6)
                    ),
                    margin_top=0
                ),
                margin_bottom=0.5,
            ),
            color=Color.from_hex("d3d3d3") if self.is_grey else self.session.theme.primary_color
        )
