package com.example.wabi.view.screeens.logIn.items

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.ui.theme.WabiTheme
import com.example.wabi.view.components.buttons.ButtonLink
import com.example.wabi.view.components.buttons.MainButton
import com.example.wabi.view.components.textFields.MainTextField
import com.example.wabi.view.components.textFields.PasswordTextField
import com.example.wabi.view.components.texts.Error.ErrorList
import com.example.wabi.view.components.texts.Error.ErrorPrint
import com.example.wabi.view.components.texts.HeaderText
import com.example.wabi.view.components.texts.MainText
import com.example.wabi.view.screeens.logIn.LogInViewModel

@Composable
fun SignIn(
    navController: NavHostController,
    printError: MutableState<Boolean>,
    state: MutableState<Int>,
    vm: LogInViewModel
) {
    val data = vm.signInData

    HeaderText(
        text = "Добро\n" + "пожаловать!"
    )
    MainText(text = "Войдите, чтобы\nпользоваться функциями приложения")
    ErrorPrint(text = if (printError.value) ErrorList.errorPassOrEmail else "")
    Column(
        modifier = Modifier.fillMaxWidth()
    ) {
        MainTextField(
            value = data.email,
            input = { vm.updateDataSignIn(data.copy(email = it)) },
            placeholder = "Почта",
            lable = "Введите почту:",
            trailingText = ""
        )
        Spacer(modifier = Modifier.height(10.dp))
        PasswordTextField(
            value = data.password,
            input = { vm.updateDataSignIn(data.copy(password = it)) },
            placeholder = "Пароль",
            lable = "Введите пароль:"
        )
    }
    Column(
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = "Нет аккаунта?",
            style = WabiTheme.fonts.acentFont,
            color = WabiTheme.colors.mainColor
        )
        ButtonLink(
            onClick = {
                state.value = LogInStateDestinations.signUp
            },
            textContent = "Зарегистирируйтесь!",
            modifier = 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 = "Войти",
            modifier = Modifier.fillMaxWidth(0.82f)
        )
        Spacer(Modifier.height(5.dp))
        ButtonLink(
            onClick = {
                state.value = LogInStateDestinations.resetPassword
            }, textContent = "Забыли пароль?", colorContent = WabiTheme.colors.mainColor
        )
    }
}