import rio

from ..models import MemberInfo, SOCIAL_PLATFORM_ICON_MAP


class MemberCard(rio.Component):
    info: MemberInfo

    def build(self) -> rio.Component:
        social_links_grid = rio.Grid(align_x=0.5, row_spacing=0.5, column_spacing=0.5, margin_bottom=0.3, height=3)

        content = rio.Column(
                rio.Image(self.info.picture_path, height=9, margin_bottom=1.3, margin_top=0.8),
                rio.Text(self.info.name, margin_bottom=0.4, style=rio.TextStyle(font_weight="bold"), justify="center"),
                rio.Text(self.info.position, margin_bottom=0.4,  style=rio.TextStyle(italic=True), justify="center"),
                rio.Text(f"Mitglied seit",  style=rio.TextStyle(italic=True), margin_bottom=0.4, justify="center"),
                rio.Text(f"{self.info.entry_date}", style=rio.TextStyle(italic=True, font_size=0.9), margin_bottom=0.4, justify="center"),
                social_links_grid,
                margin=0.4
            )

        for i, social_link in enumerate(sorted(self.info.socials, key=lambda s: s.platform.value)):
            social_links_grid.add(
                rio.Link(
                    rio.Icon(SOCIAL_PLATFORM_ICON_MAP[social_link.platform], height=1.3, width=1.3, fill="primary"),
                    social_link.url,
                    open_in_new_tab=True
                ), i//5, i if i < 5 else i - 5)

        return rio.Rectangle(
            content=content,
            fill=self.session.theme.neutral_color,
            corner_radius=self.session.theme.corner_radius_medium,
            shadow_radius=0.5,
            shadow_color=self.session.theme.hud_color,
            shadow_offset_y=0
        )


def build_member_card(info: MemberInfo) -> MemberCard:
    return MemberCard(
        info=info,
        align_y=0,
        margin_top=0,
        margin_bottom=2,
        margin_right=1
    )
