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

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.example.wabi.ui.theme.WabiTheme
import com.example.wabi.view.components.buttons.ButtonBack
import com.example.wabi.view.components.buttons.MainButton
import com.example.wabi.view.components.textFields.MainTextField
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 EmailForReset(vm: LogInViewModel, state: MutableState<Int>) {
    val data = vm.signInData

    var correctEmail = remember { mutableStateOf(false) }
    val currentEmail = remember { mutableStateOf(data.email) }
    val messageVisible = remember { mutableStateOf(false) }

    Row(
        modifier = Modifier.fillMaxWidth()
    ) {
        ButtonBack(onClick = {
            state.value = LogInStateDestinations.signIn
        }, modifier = Modifier.padding(10.dp))
    }
    HeaderText(text = "Восстановите\nпароль")

    MainText(
        text = "Для восстановления\n" + "пароля введите\n" + "свою почту"
    )

    ErrorPrint(text = if (correctEmail.value) ErrorList.emailNotExists else "")

    if (messageVisible.value) {
        MainText(
            "Сообщение\nдля восстановления\nпароля было отправлено\nна почту",
            maxLines = 4,
            color = WabiTheme.colors.accentColorTwo
        )
    }

    MainTextField(
        value = currentEmail.value,
        input = { currentEmail.value = it },
        placeholder = "Почта",
        lable = "Введите почту:",
        trailingText = ""
    )

    MainButton(
        onClick = {
            correctEmail.value = false
            messageVisible.value = vm.sendEmailForResetPassword(currentEmail.value)
        },
        enabled = vm.validateEmail(currentEmail.value),
        textContent = "Отправить код",
        modifier = Modifier.padding(30.dp, 0.dp)
    )
}