from functools import partial
from typing import Callable, Optional, Literal

from rio import Component, Revealer, TextStyle, Column, Row, Tooltip, Icon, Spacer, Text, Button

from src.ezgg_lan_manager.types.Team import TeamStatus, Team
from src.ezgg_lan_manager.types.User import User


class TeamRevealer(Component):
    user: Optional[User]
    team: Team
    mode: Literal["join", "leave", "display"]
    on_button_pressed: Callable

    def build(self) -> Component:
        return Revealer(
            header=self.team.name,
            header_style=TextStyle(
                fill=self.session.theme.background_color,
                font_size=1
            ),
            content=Column(
                *[Row(
                    Tooltip(
                        anchor=Icon("material/star" if self.team.members[member] == TeamStatus.LEADER else "material/stat_1", fill=self.session.theme.hud_color),
                        tip="Leiter" if self.team.members[member] == TeamStatus.LEADER else "Mitglied", position="top"),
                    Text(member.user_name, style=TextStyle(fill=self.session.theme.background_color, font_size=1), margin_left=0.5),
                    Spacer(grow_y=False))
                    for member in self.team.members
                ],
                Row(Button(
                    content=f"{self.team.name} beitreten" if self.mode == "join" else f"{self.team.name} verlassen",
                    shape="rectangle",
                    style="major",
                    color="hud",
                    on_press=partial(self.on_button_pressed, self.team),
                ), margin_top=1, margin_bottom=1),
                margin_right=1,
                margin_left=1
            ),
            margin_left=1,
            margin_right=1,
        )
