package com.example.wabi.view.screens.signIn.states

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.example.wabi.model.screens.SignInState
import com.example.wabi.ui.theme.WabiTheme
import com.example.wabi.view.common_elements.button.ButtonLink
import com.example.wabi.view.common_elements.button.MainButton
import com.example.wabi.view.common_elements.text.error.ErrorList
import com.example.wabi.view.common_elements.text.error.ErrorPrint
import com.example.wabi.view.common_elements.textfields.MainTextField
import com.example.wabi.view.common_elements.textfields.PasswordTextField
import com.example.wabi.view.screens.signIn.SignInViewModel

@Composable
fun Login(
    navController: NavHostController,
    printError: MutableState<Boolean>,
    visible: MutableState<Boolean>,
    data: SignInState,
    vm: SignInViewModel
) {
    Text(
        text = "Добро\nпожаловать!",
        style = WabiTheme.fonts.headerFont,
        maxLines = 2,
        minLines = 2,
        color = WabiTheme.colors.mainColor
    )
    Text(
        text = "Войдите, чтобы\nпользоваться функциями приложения",
        style = WabiTheme.fonts.mainFont,
        color = WabiTheme.colors.mainColor
    )
    ErrorPrint(text = if (printError.value) ErrorList.errorPassOrEmail else "")
    Column(
        modifier = Modifier.fillMaxWidth()
    ) {
        MainTextField(
            value = data.email,
            input = { vm.updateData(data.copy(email = it)) },
            placeholder = "Почта",
            lable = "Введите почту:",
            trailingText = ""
        )
        Spacer(modifier = Modifier.height(10.dp))
        PasswordTextField(
            value = data.password,
            input = { vm.updateData(data.copy(password = it)) },
            placeholder = "Пароль",
            lable = "Введите пароль:"
        )
    }
    Column(
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = "Нет аккаунта?",
            style = WabiTheme.fonts.acentFont,
            color = WabiTheme.colors.mainColor
        )
        ButtonLink(
            onClick = {
                vm.goToRegistration(navController = navController)
            },
            textContent = "Зарегистирируйтесь!",
            modifierButton = Modifier.offset(y = (-15).dp),
            colorContent = WabiTheme.colors.mainColor
        )
        MainButton(
            onClick = {
                vm.signIn(navController = navController, printError = printError)
            },
            enabled = data.password != "" && vm.validateEmail(data.email),
            textContent = "Войти",
            modifierButton = Modifier
                .padding(30.dp, 0.dp)
                .fillMaxWidth()
        )
        Spacer(Modifier.height(5.dp))
        ButtonLink(
            onClick = {
                visible.value = false
            }, textContent = "Забыли пароль?", colorContent = WabiTheme.colors.mainColor
        )
    }
}