import uuid

from rio import Component, TextStyle, Color, TextInput, Button, Text, Rectangle, Column, Row, Spacer, \
    EventHandler, Webview

from src.ezgg_lan_manager import RefreshService
from src.ezgg_lan_manager.services.LocalDataService import LocalDataService, LocalData
from src.ezgg_lan_manager.services.UserService import UserService
from src.ezgg_lan_manager.types.User import User
from src.ezgg_lan_manager.types.UserSession import UserSession


class LoginBox(Component):
    status_change_cb: EventHandler = None
    user_name_input_text: str = ""
    password_input_text: str = ""
    user_name_input_is_valid = True
    password_input_is_valid = True
    login_button_is_loading = False
    is_account_locked: bool = False

    async def _on_login_pressed(self) -> None:
        if await self.session[UserService].is_login_valid(self.user_name_input_text, self.password_input_text):
            user: User = await self.session[UserService].get_user(self.user_name_input_text)
            if not user.is_active:
                self.is_account_locked = True
                return
            self.user_name_input_is_valid = True
            self.password_input_is_valid = True
            self.login_button_is_loading = False
            self.is_account_locked = False
            user_session = UserSession(id=uuid.uuid4(), user_id=user.user_id, is_team_member=user.is_team_member)
            self.session.attach(user_session)
            token = self.session[LocalDataService].set_session(user_session)
            self.session[LocalData].stored_session_token = token
            self.session.attach(self.session[LocalData])
            await self.status_change_cb()
            await self.session[RefreshService].trigger_refresh()
        else:
            self.user_name_input_is_valid = False
            self.password_input_is_valid = False
            self.login_button_is_loading = False
            self.is_account_locked = False
            self.force_refresh()

    def build(self) -> Component:
        user_name_input = TextInput(
            text=self.bind().user_name_input_text,
            label="Benutzername",
            accessibility_label="Benutzername",
            min_height=0.5,
            on_confirm=lambda _: self._on_login_pressed(),
            is_valid=self.user_name_input_is_valid
        )
        password_input = TextInput(
            text=self.bind().password_input_text,
            label="Passwort",
            accessibility_label="Passwort",
            is_secret=True,
            on_confirm=lambda _: self._on_login_pressed(),
            is_valid=self.password_input_is_valid
        )
        login_button = Button(
            Text("LOGIN", fill=Color.from_hex("02dac5"), style=TextStyle(font_size=0.9), justify="center"),
            shape="rectangle",
            style="minor",
            color="secondary",
            margin_bottom=0.4,
            on_press=self._on_login_pressed
        )
        register_button = Button(
            Text("REG", fill=Color.from_hex("02dac5"), style=TextStyle(font_size=0.9), justify="center"),
            shape="rectangle",
            style="minor",
            color="secondary",
            on_press=lambda: self.session.navigate_to("./register")
        )
        forgot_password_button = Button(
            Text("LST PWD", fill=Color.from_hex("02dac5"), style=TextStyle(font_size=0.9), justify="center"),
            shape="rectangle",
            style="minor",
            color="secondary",
            on_press=lambda: self.session.navigate_to("./forgot-password")
        )

        return Rectangle(
            content=Column(
                user_name_input,
                password_input,
                Column(
                    Row(
                        login_button
                    ),
                    Row(
                        register_button,
                        Spacer(),
                        forgot_password_button,
                        proportions=(49, 2, 49)
                    ),
                    margin_bottom=0.5
                ),
                Text(text="Dieses Konto\nist gesperrt", fill=self.session.theme.danger_color, style=TextStyle(font_size=0.9 if self.is_account_locked else 0), align_x=0.5),
                spacing=0.4
            ),
            fill=Color.TRANSPARENT,
            min_height=8,
            min_width=12,
            align_x=0.5,
            margin_top=0.3,
            margin_bottom=1.5
        )
