from __future__ import annotations

from asyncio import sleep
from typing import *  # type: ignore

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

from src.ezgg_lan_manager.services.DatabaseService import DatabaseService
from src.ezgg_lan_manager import ConfigurationService
from src.ezgg_lan_manager.components.MainViewContentBox import MainViewContentBox


class DbErrorPage(Component):
    @event.on_window_size_change
    async def on_window_size_change(self) -> None:
        self.force_refresh()

    @event.on_mount
    async def retry_db_connect(self) -> None:
        await self.session.set_title(f"{self.session[ConfigurationService].get_lan_info().name} - Fehler")
        while not await self.session[DatabaseService].is_healthy():
            await sleep(2)
        self.session.navigate_to("./")

    def build(self) -> Component:
        content = Card(
            content=MainViewContentBox(
                content=Text(
                    text="Ouh-oh, da läuft gerade irgendwas schief.\n\n"
                         "Unser Team kümmert sich bereits um das Problem.\n\n"
                         "Du wirst automatisch weitergeleitet sobald das System wieder verfügbar ist.",
                    margin=2,
                    style=TextStyle(
                        fill=self.session.theme.danger_color,
                        font_size=1.3
                    ),
                    overflow="wrap"
                )
            ),
            color="secondary",
            min_width=38,
            corner_radius=(0, 0.5, 0, 0)
        )
        if self.session.window_width > 28:
            return Container(
                content=Column(
                    Column(
                        Row(
                            Spacer(grow_x=True, grow_y=True),
                            Card(
                                content=Spacer(),
                                color=self.session.theme.neutral_color,
                                min_width=15,
                                grow_y=True,
                                corner_radius=(0.5, 0, 0, 0),
                                margin_right=0.1
                            ),
                            content,
                            Spacer(grow_x=True, grow_y=True),
                            grow_y=True
                        ),
                        Row(
                            Spacer(grow_x=True, grow_y=False),
                            Card(
                                content=Text(f"EZGG LAN Manager Version {self.session[ConfigurationService].APP_VERSION} © EZ GG e.V.", align_x=0.5, align_y=0.5, style=TextStyle(fill=self.session.theme.primary_color, font_size=0.5)),
                                color=self.session.theme.neutral_color,
                                corner_radius=(0, 0, 0.5, 0.5),
                                grow_x=False,
                                grow_y=False,
                                min_height=1.2,
                                min_width=53.1,
                                margin_bottom=3
                            ),
                            Spacer(grow_x=True, grow_y=False),
                            grow_y=False
                        ),
                        margin_top=4
                    )
                ),
            grow_x=True,
            grow_y=True
            )
        else:
            return Text(
                "Der EZGG LAN Manager wird\nauf mobilen Endgeräten nur\nim Querformat unterstützt.\nBitte drehe dein Gerät.",
                align_x=0.5,
                align_y=0.5,
                style=TextStyle(fill=Color.from_hex("FFFFFF"), font_size=0.8)
            )
