package com.example.development.mainActivity.components

import android.widget.Toast
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Person
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.example.development.R
import com.example.development.domain.utils.Constants
import com.example.development.domain.utils.Constants.supabase
import com.example.development.domain.utils.CustomPassword
import com.example.development.mainActivity.MainViewModel
import com.example.development.ui.theme.OldStandart
import io.github.jan.supabase.gotrue.auth

@Composable
fun PasswordRecovery(navController: NavHostController, viewModel: MainViewModel)
{
    val email = remember { mutableStateOf("") }
    val flag = remember { mutableStateOf(false) }
    Box(
        contentAlignment = Alignment.TopCenter,
        modifier = Modifier
            .fillMaxSize()
    )
    {
        Image(
            painter = painterResource(id = R.drawable.fone),
            contentDescription = "",
            contentScale = ContentScale.FillWidth,
            modifier = Modifier.fillMaxWidth().fillMaxHeight()
        )
        Column(
            modifier = Modifier
                .padding(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        )
        {
            Text("ВОССТАНОВЛЕНИЕ ПАРОЛЯ",
                color = Color.White,
                modifier = Modifier.padding(top = 150.dp, bottom = 60.dp),
                fontSize = 30.sp,
                fontWeight = FontWeight.Bold,
                fontFamily = OldStandart,
                softWrap = true,
                textAlign = TextAlign.Center
            )
            OutlinedTextField(value = email.value,
                onValueChange = { it -> email.value = it },
                leadingIcon = { Icon(Icons.Filled.Person, contentDescription = "email") },
                colors = TextFieldDefaults.colors(
                    focusedContainerColor = Color(0XFFF5F5F9),
                    focusedIndicatorColor = Color(0xFFEBEBEB),
                    focusedTextColor = Color.Black,
                    disabledIndicatorColor = Color.Transparent,
                    unfocusedIndicatorColor = Color.Transparent,
                    cursorColor = Color(0XFF578FFF),
                    focusedSupportingTextColor = Color(0xFF00000)
                ),
                modifier = Modifier
                    .fillMaxWidth(1f)
                    .background(Color(0XFFF5F5F9))
                    .height(height = 56.dp)
                    .width(width = 200.dp),
                isError = flag.value,
                placeholder = {
                    Text(
                        modifier = Modifier.fillMaxWidth(),
                        text = "e-mail",
                        fontWeight = FontWeight.Normal,
                        fontSize = 15.sp,
                        color = Color(0XFF939396)
                    )
                }
            )

            if (email.value.isEmpty()) {
                flag.value = true
                Text("Введите e-mail", color = Color.Red, fontSize = 10.sp)
            } else {
                flag.value = false
            }
            Button(modifier = Modifier
                .width(width = 270.dp)
                .height(height = 40.dp),
                onClick = {
                          navController.navigate("PasswordRecovery2")},
                shape = RoundedCornerShape(23.dp),
                border = BorderStroke(0.dp, Color(0xFFEBEBEB)),
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color(255,215,0),
                    contentColor = Color.White)
            )
            {
                Text("ПОЛУЧИТЬ КОД",
                    fontFamily = OldStandart)
            }
        }
    }
}

@Composable
fun PasswordRecovery2(navController: NavHostController, viewModel: MainViewModel)
{
    val context = LocalContext.current
    val password = remember { mutableStateOf("") }
    val checkPassword = remember { mutableStateOf("") }
    val flag = remember { mutableStateOf(false) }
    Box(
        contentAlignment = Alignment.TopCenter,
        modifier = Modifier
            .fillMaxSize()
    )
    {
        Image(
            painter = painterResource(id = R.drawable.fone),
            contentDescription = "",
            contentScale = ContentScale.FillWidth,
            modifier = Modifier.fillMaxWidth().fillMaxHeight()
        )
        Column(
            modifier = Modifier
                .padding(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        )
        {
            Text("ВОССТАНОВЛЕНИЕ ПАРОЛЯ",
                color = Color.White,
                modifier = Modifier.padding(top = 150.dp, bottom = 60.dp),
                fontSize = 30.sp,
                fontWeight = FontWeight.Bold,
                fontFamily = OldStandart,
                softWrap = true,
                textAlign = TextAlign.Center
            )
            Column(verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.Start)
            {
                Text(
                    "Придумайте новый пароль",
                    color = Color.White,
                    modifier = Modifier.padding(bottom = 60.dp),
                    fontSize = 18.sp,
                    fontWeight = FontWeight.Normal,
                    softWrap = true,
                    textAlign = TextAlign.Start,
                    fontFamily = OldStandart
                )
                CustomPassword("Введите пароль", search = password.value, onValueChange = {password.value = it})
                CustomPassword("Повторите пароль", search = checkPassword.value, onValueChange = {checkPassword.value = it})
                if (password.value == checkPassword.value)
                {
                    flag.value = true
                }
                else
                {
                    flag.value = false
                }
                Button(
                    modifier = Modifier
                        .width(width = 270.dp)
                        .height(height = 40.dp),
                    onClick = {
                        if (flag.value) {
                            viewModel.upadatePassword(password.value)
                            navController.navigate("MainPage")}
                        else { Toast.makeText(context, "Пароли не совпадают", Toast.LENGTH_SHORT).show() }
                    },
                    shape = RoundedCornerShape(23.dp),
                    border = BorderStroke(0.dp, Color(0xFFEBEBEB)),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color(255, 215, 0),
                        contentColor = Color.White
                    )
                )
                {
                    Text("ПОДТВЕРДИТЬ",
                        color = Color.White,
                        modifier = Modifier.padding(),
                        fontSize = 18.sp,
                        fontWeight = FontWeight.Normal,
                        fontFamily = OldStandart
                    )
                }
            }
        }
    }
}