from typing import Callable

from rio import Component, Rectangle, Grid, Column, Row, Text, TextStyle, Color, PointerEventListener, Spacer

from src.ezgg_lan_manager.components.SeatingPlanPixels import SeatPixel, WallPixel, InvisiblePixel, TextPixel
from src.ezgg_lan_manager.types.Seat import Seat

MAX_GRID_WIDTH_PIXELS = 60
MAX_GRID_HEIGHT_PIXELS = 60

class SeatingPlanLegend(Component):
    def build(self) -> Component:
        return Column(
            Text("Legende", style=TextStyle(fill=self.session.theme.neutral_color), justify="center", margin=1),
            Row(
                Spacer(),
                Rectangle(
                    content=Text("Normaler Platz", style=TextStyle(fill=self.session.theme.neutral_color, font_size=0.7), margin=0.2, justify="center"),
                    fill=Color.TRANSPARENT,
                    stroke_width=0.2,
                    stroke_color=Color.from_hex("003300"),
                    min_width=20,
                    margin_right=1
                ),
                Rectangle(
                    content=Text("Deluxe Platz", style=TextStyle(fill=self.session.theme.neutral_color, font_size=0.7), margin=0.2, justify="center"),
                    fill=Color.TRANSPARENT,
                    stroke_width=0.2,
                    stroke_color=Color.from_hex("66ff99"),
                    min_width=20
                ),
                Spacer()
            ),
            Row(
                Rectangle(
                    content=Column(
                        Text(f"Freier Platz", style=TextStyle(fill=self.session.theme.primary_color, font_size=0.7), align_x=0.5, selectable=False),
                        Text(f"", style=TextStyle(fill=self.session.theme.primary_color, font_size=0.9), align_x=0.5,
                             selectable=False, overflow="wrap")
                    ),
                    min_width=1,
                    min_height=1,
                    fill=self.session.theme.success_color,
                    grow_x=False,
                    grow_y=False,
                    hover_fill=self.session.theme.success_color,
                    transition_time=0.4,
                    ripple=True
                ),
                Rectangle(
                    content=Column(
                        Text(f"Belegter Platz", style=TextStyle(fill=self.session.theme.primary_color, font_size=0.7), align_x=0.5, selectable=False),
                        Text(f"", style=TextStyle(fill=self.session.theme.primary_color, font_size=0.9), align_x=0.5,
                             selectable=False, overflow="wrap")
                    ),
                    min_width=1,
                    min_height=1,
                    fill=self.session.theme.danger_color,
                    grow_x=False,
                    grow_y=False,
                    hover_fill=self.session.theme.danger_color,
                    transition_time=0.4,
                    ripple=True
                ),
                Rectangle(
                    content=Column(
                        Text(f"Eigener Platz", style=TextStyle(fill=self.session.theme.primary_color, font_size=0.7), align_x=0.5, selectable=False),
                        Text(f"", style=TextStyle(fill=self.session.theme.primary_color, font_size=0.9), align_x=0.5,
                             selectable=False, overflow="wrap")
                    ),
                    min_width=1,
                    min_height=1,
                    fill=Color.from_hex("800080"),
                    grow_x=False,
                    grow_y=False,
                    hover_fill=Color.from_hex("800080"),
                    transition_time=0.4,
                    ripple=True
                ),
                margin=1,
                spacing=1
            )
        )


class SeatingPlan(Component):
    seat_clicked_cb: Callable
    seating_info: list[Seat]
    info_clicked_cb: Callable

    def get_seat(self, seat_id: str) -> Seat:
        seat = next(filter(lambda seat_: seat_.seat_id == seat_id, self.seating_info), None)
        return seat if seat else Seat(seat_id="Z99", is_blocked=True, category="LUXUS", user=None)

    """
    This seating plan is for the community center "Donsbach"
    """
    def build(self) -> Component:
        grid = Grid()
        # Outlines
        for x in range(0, MAX_GRID_WIDTH_PIXELS):
            grid.add(WallPixel(), row=0, column=x)

        for y in range(0, MAX_GRID_HEIGHT_PIXELS):
            grid.add(WallPixel(), row=y, column=0)

        for x in range(0, MAX_GRID_WIDTH_PIXELS):
            grid.add(WallPixel(), row=MAX_GRID_HEIGHT_PIXELS, column=x)

        for x in range(0, 31):
            grid.add(WallPixel(), row=15, column=x)

        for x in range(41, MAX_GRID_WIDTH_PIXELS):
            grid.add(WallPixel(), row=32, column=x)

        for x in range(31, 34):
            grid.add(WallPixel(), row=32, column=x)
            grid.add(WallPixel(), row=19, column=x)

        for x in range(42, MAX_GRID_WIDTH_PIXELS):
            grid.add(WallPixel(), row=11, column=x)
            grid.add(WallPixel(), row=22, column=x)

        for x in range(22, 30):
            grid.add(WallPixel(), row=5, column=x)
            grid.add(WallPixel(), row=10, column=x)

        for y in range(5, 11):
            grid.add(WallPixel(), row=y, column=21)
            grid.add(WallPixel(), row=y, column=30)


        for y in range(40, MAX_GRID_HEIGHT_PIXELS):
            grid.add(WallPixel(), row=y, column=30)

        for y in range(32, 36):
            grid.add(WallPixel(), row=y, column=30)

        for y in range(19, 33):
            grid.add(WallPixel(), row=y, column=34)

        for y in range(16, 20):
            grid.add(WallPixel(), row=y, column=30)

        for y in range(0, 5):
            grid.add(WallPixel(), row=y, column=41)

        for y in range(9, 15):
            grid.add(WallPixel(), row=y, column=41)

        for y in range(19, 33):
            grid.add(WallPixel(), row=y, column=41)


        # Block A
        grid.add(SeatPixel("A01", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("A01"), seat_orientation="bottom"), row=57, column=1, width=5, height=2)
        grid.add(SeatPixel("A02", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("A02"), seat_orientation="bottom"), row=57, column=6, width=5, height=2)
        grid.add(SeatPixel("A03", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("A03"), seat_orientation="bottom"), row=57, column=11, width=5, height=2)
        grid.add(SeatPixel("A04", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("A04"), seat_orientation="bottom"), row=57, column=16, width=5, height=2)
        grid.add(SeatPixel("A05", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("A05"), seat_orientation="bottom"), row=57, column=21, width=5, height=2)

        grid.add(SeatPixel("A10", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("A10"), seat_orientation="top"), row=55, column=1, width=5, height=2)
        grid.add(SeatPixel("A11", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("A11"), seat_orientation="top"), row=55, column=6, width=5, height=2)
        grid.add(SeatPixel("A12", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("A12"), seat_orientation="top"), row=55, column=11, width=5, height=2)
        grid.add(SeatPixel("A13", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("A13"), seat_orientation="top"), row=55, column=16, width=5, height=2)
        grid.add(SeatPixel("A14", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("A14"), seat_orientation="top"), row=55, column=21, width=5, height=2)

        # Block B
        grid.add(SeatPixel("B01", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("B01"), seat_orientation="bottom"), row=50, column=1, width=3, height=2)
        grid.add(SeatPixel("B02", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("B02"), seat_orientation="bottom"), row=50, column=4, width=3, height=2)
        grid.add(SeatPixel("B03", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("B03"), seat_orientation="bottom"), row=50, column=7, width=3, height=2)
        grid.add(SeatPixel("B04", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("B04"), seat_orientation="bottom"), row=50, column=10, width=3, height=2)
        grid.add(SeatPixel("B05", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("B05"), seat_orientation="bottom"), row=50, column=13, width=3, height=2)
        grid.add(SeatPixel("B06", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("B06"), seat_orientation="bottom"), row=50, column=16, width=3, height=2)

        grid.add(SeatPixel("B10", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("B10"), seat_orientation="top"), row=48, column=1, width=3, height=2)
        grid.add(SeatPixel("B11", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("B11"), seat_orientation="top"), row=48, column=4, width=3, height=2)
        grid.add(SeatPixel("B12", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("B12"), seat_orientation="top"), row=48, column=7, width=3, height=2)
        grid.add(SeatPixel("B13", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("B13"), seat_orientation="top"), row=48, column=10, width=3, height=2)
        grid.add(SeatPixel("B14", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("B14"), seat_orientation="top"), row=48, column=13, width=3, height=2)
        grid.add(SeatPixel("B15", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("B15"), seat_orientation="top"), row=48, column=16, width=3, height=2)

        # Block C
        grid.add(SeatPixel("C01", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("C01"), seat_orientation="bottom"), row=43, column=1, width=3, height=2)
        grid.add(SeatPixel("C02", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("C02"), seat_orientation="bottom"), row=43, column=4, width=3, height=2)
        grid.add(SeatPixel("C03", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("C03"), seat_orientation="bottom"), row=43, column=7, width=3, height=2)
        grid.add(SeatPixel("C04", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("C04"), seat_orientation="bottom"), row=43, column=10, width=3, height=2)
        grid.add(SeatPixel("C05", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("C05"), seat_orientation="bottom"), row=43, column=13, width=3, height=2)
        grid.add(SeatPixel("C06", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("C06"), seat_orientation="bottom"), row=43, column=16, width=3, height=2)

        grid.add(SeatPixel("C10", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("C10"), seat_orientation="top"), row=41, column=1, width=3, height=2)
        grid.add(SeatPixel("C11", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("C11"), seat_orientation="top"), row=41, column=4, width=3, height=2)
        grid.add(SeatPixel("C12", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("C12"), seat_orientation="top"), row=41, column=7, width=3, height=2)
        grid.add(SeatPixel("C13", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("C13"), seat_orientation="top"), row=41, column=10, width=3, height=2)
        grid.add(SeatPixel("C14", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("C14"), seat_orientation="top"), row=41, column=13, width=3, height=2)
        grid.add(SeatPixel("C15", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("C15"), seat_orientation="top"), row=41, column=16, width=3, height=2)

        # Block D
        grid.add(SeatPixel("D01", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("D01"), seat_orientation="bottom"), row=34, column=1, width=5, height=2)
        grid.add(SeatPixel("D02", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("D02"), seat_orientation="bottom"), row=34, column=6, width=5, height=2)
        grid.add(SeatPixel("D03", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("D03"), seat_orientation="bottom"), row=34, column=11, width=5, height=2)
        grid.add(SeatPixel("D04", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("D04"), seat_orientation="bottom"), row=34, column=16, width=5, height=2)
        grid.add(SeatPixel("D05", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("D05"), seat_orientation="bottom"), row=34, column=21, width=5, height=2)

        grid.add(SeatPixel("D10", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("D10"), seat_orientation="top"), row=32, column=1, width=5, height=2)
        grid.add(SeatPixel("D11", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("D11"), seat_orientation="top"), row=32, column=6, width=5, height=2)
        grid.add(SeatPixel("D12", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("D12"), seat_orientation="top"), row=32, column=11, width=5, height=2)
        grid.add(SeatPixel("D13", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("D13"), seat_orientation="top"), row=32, column=16, width=5, height=2)
        grid.add(SeatPixel("D14", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("D14"), seat_orientation="top"), row=32, column=21, width=5, height=2)

        # Block E
        grid.add(SeatPixel("E01", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("E01"), seat_orientation="bottom"), row=27, column=1, width=5, height=2)
        grid.add(SeatPixel("E02", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("E02"), seat_orientation="bottom"), row=27, column=6, width=5, height=2)
        grid.add(SeatPixel("E03", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("E03"), seat_orientation="bottom"), row=27, column=11, width=5, height=2)
        grid.add(SeatPixel("E04", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("E04"), seat_orientation="bottom"), row=27, column=16, width=5, height=2)
        grid.add(SeatPixel("E05", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("E05"), seat_orientation="bottom"), row=27, column=21, width=5, height=2)

        grid.add(SeatPixel("E10", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("E10"), seat_orientation="top"), row=25, column=1, width=5, height=2)
        grid.add(SeatPixel("E11", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("E11"), seat_orientation="top"), row=25, column=6, width=5, height=2)
        grid.add(SeatPixel("E12", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("E12"), seat_orientation="top"), row=25, column=11, width=5, height=2)
        grid.add(SeatPixel("E13", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("E13"), seat_orientation="top"), row=25, column=16, width=5, height=2)
        grid.add(SeatPixel("E14", on_press_cb=self.seat_clicked_cb, seat=self.get_seat("E14"), seat_orientation="top"), row=25, column=21, width=5, height=2)

        # Stage
        grid.add(PointerEventListener(
            TextPixel(text="Bühne"),
            on_press=lambda _: self.info_clicked_cb("Hier darf ab Freitag 20 Uhr ebenfalls geschlafen werden.")
        ), row=16, column=1, width=29, height=4)

        # Drinks
        grid.add(PointerEventListener(
            TextPixel(text="G\ne\nt\nr\nä\nn\nk\ne"),
            on_press=lambda _: self.info_clicked_cb("Ich mag Bier, B - I - R")
        ), row=20, column=30, width=4, height=12)

        # Main Entrance
        grid.add(PointerEventListener(
            TextPixel(text="H\na\nl\nl\ne\nn\n\ne\ni\nn\ng\na\nn\ng"),
            on_press=lambda _: self.info_clicked_cb("Hallo, ich bin ein Haupteingang")
        ), row=33, column=56, width=4, height=27)

        # Sleeping
        grid.add(PointerEventListener(
            TextPixel(icon_name="material/bed"),
            on_press=lambda _: self.info_clicked_cb("In diesem Raum kann geschlafen werden.\nAchtung: Hier werden nicht alle Teilnehmer Platz finden.")
        ), row=1, column=1, width=20, height=14)

        # Toilet
        grid.add(PointerEventListener(
            TextPixel(icon_name="material/wc"),
            on_press=lambda _: self.info_clicked_cb("Damen Toilette")
        ), row=1, column=42, width=19, height=10)
        grid.add(PointerEventListener(
            TextPixel(icon_name="material/wc"),
            on_press=lambda _: self.info_clicked_cb("Herren Toilette")
        ), row=12, column=42, width=19, height=10)

        # Entry/Helpdesk
        grid.add(PointerEventListener(
            TextPixel(text="Einlass\n &Orga"),
            on_press=lambda _: self.info_clicked_cb("Für alle Anliegen findest du hier rund um die Uhr jemanden vom Team.")
        ), row=40, column=22, width=8, height=12)


        return Rectangle(
            content=grid,
            grow_x=True,
            grow_y=True,
            stroke_color=self.session.theme.neutral_color,
            stroke_width=0.1,
            fill=self.session.theme.primary_color,
            margin=0.5
        )

