package com.example.tasks.screens

import android.util.Patterns
import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
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.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.KeyboardArrowLeft
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
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.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.example.tasks.components.CustomEmail
import com.example.tasks.components.CustomPassword
import com.example.tasks.model.UserState
import com.example.tasks.navigation.Screen
import com.example.tasks.viewModels.AuthViewModel
import com.example.tasks.viewModels.DatabaseViewModel

@Composable
fun SignUpWithPassword(navController:NavController, viewModel: AuthViewModel) {
    val context = LocalContext.current
    val userState by viewModel.userState
    val email = remember { mutableStateOf("") }
    val password = remember { mutableStateOf("") }
    val password2 = remember { mutableStateOf("") }
    val enable = remember { mutableStateOf(false) }
    val flag = remember { mutableStateOf(false) }
    var currentUserState by remember { mutableStateOf("") }

    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 60.dp, start = 20.dp),
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.Top
    ) {
        Button(
            onClick = { navController.navigate(Screen.Start.route) },
            shape = RoundedCornerShape(size = 8.dp),
            colors = ButtonDefaults.buttonColors(
                containerColor = Color(0xFFF5F5F9)
            ),
            modifier = Modifier
                .width(32.dp)
                .height(32.dp), contentPadding = PaddingValues(1.dp)
        )
        {
            Icon(
                imageVector = Icons.AutoMirrored.Filled.KeyboardArrowLeft,
                contentDescription = "Назад",
                tint = Color.Black,
                modifier = Modifier.size(20.dp)
            )
        }
    }
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color(0xFF951DDD))
            .padding(10.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        CustomEmail(search = email.value, onValueChange = { email.value = it }, isError = !enable.value)
        if (Patterns.EMAIL_ADDRESS.matcher(email.value).matches()) {
            enable.value = true
        } else {
            Row(Modifier.fillMaxWidth()){
                Text(
                    "Некорректная почта",
                    fontSize = 12.sp,
                    color = Color(0xFFFEB3F6),
                    textAlign = TextAlign.Start,
                    modifier = Modifier.padding(10.dp)
                )
            }
        }
        Spacer(Modifier.height(50.dp))
        CustomPassword(search = password.value, onValueChange = { password.value = it }, text = "Введите пароль")
        Spacer(Modifier.height(50.dp))
        CustomPassword(search = password2.value, onValueChange = { password2.value = it }, text = "Введите пароль повторно")
        Spacer(Modifier.height(50.dp))
        Button(
            onClick = {
                if(password.value != password2.value){
                    Toast.makeText(context, "Пароли не совпадают", Toast.LENGTH_SHORT).show()
                }
                else{
                    viewModel.onSignUpEmailPassword(context, email.value, password.value)
                    flag.value = true
                }
            },
            Modifier
                .fillMaxWidth()
                .height(50.dp),
            shape = RoundedCornerShape(20),
            enabled = enable.value,
            colors = ButtonColors(
                contentColor = Color(0xFF951DDD),
                containerColor = Color.White,
                disabledContentColor = Color(0xFF951DDD),
                disabledContainerColor = Color.LightGray
            )
        ) {
            Text(text = "Далее", fontSize = 20.sp, fontWeight = FontWeight.Bold)

        }

    }
    if(flag.value){
        when (userState) {
            is UserState.Loading -> {
                LoadingComponent()
            }

            is UserState.Success -> {
                val message = (userState as UserState.Success).message
                currentUserState = message
                navController.navigate(Screen.CreateProfile.route)
                flag.value = false
            }

            is UserState.Error -> {
                val message = (userState as UserState.Error).message
                currentUserState = message
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
                flag.value = false
            }
        }

        if (currentUserState.isNotEmpty()) {
            println(currentUserState)
        }
    }

}