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.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
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.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.example.development.R
import com.example.development.domain.utils.CustomEmail
import com.example.development.domain.utils.CustomPassword
import com.example.development.ui.theme.OldStandart
import com.example.development.mainActivity.MainViewModel

@Composable
fun AuthorizationScreen(navController: NavHostController, viewModel: MainViewModel){
    val context = LocalContext.current
    val users by viewModel.users
    val email = remember { mutableStateOf("") }
    val password = remember { mutableStateOf("") }
    val flag = remember { mutableStateOf(false) }
    val flagNotNull = 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.fillMaxSize(), verticalArrangement = Arrangement.SpaceAround, horizontalAlignment = Alignment.CenterHorizontally)
        {
            Column(verticalArrangement = Arrangement.Top, horizontalAlignment = Alignment.CenterHorizontally)
            {
                Image(
                    painter = painterResource(id = R.drawable.logo),
                    contentDescription = "",
                    contentScale = ContentScale.FillHeight,
                    modifier = Modifier
                        .height(150.dp)
                        .width(150.dp)
                )
            }
            Column(verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally, )
            {
                Text(
                    "АВТОРИЗАЦИЯ",
                    color = Color.White,
                    modifier = Modifier.padding(bottom = 50.dp),
                    fontSize = 32.sp,
                    fontWeight = FontWeight.Bold,
                    fontFamily = OldStandart
                )
                Column(
                    modifier = Modifier
                        .padding(),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally
                )
                {
                    CustomEmail(search = email.value, onValueChange = {email.value = it })
                    CustomPassword("Пароль", search = password.value, onValueChange = {password.value = it})
                    if (password.value == "" || email.value == "")
                    {
                        flagNotNull.value = false
                    }
                    else{
                        flagNotNull.value = true
                    }
                    Button(
                        modifier = Modifier
                            .padding(top = 15.dp)
                            .height(50.dp)
                            .width(280.dp),
                        onClick = {
                            if (flagNotNull.value) { viewModel.onSignInEmailPassword(email.value,password.value)
                                viewModel.FindUser()
                                flag.value = true }
                            else { Toast.makeText(context, "Заполнены не все поля", Toast.LENGTH_SHORT).show() }

                        },
                        shape = RoundedCornerShape(23.dp),
                        border = BorderStroke(1.dp, Color(255,215,0)),
                        colors = ButtonDefaults.buttonColors(
                            containerColor = Color(255, 215, 0),
                            contentColor = Color.White
                        )
                    )
                    {
                        Text("ВОЙТИ", fontSize = 20.sp,
                            color = Color.White,
                            fontWeight = FontWeight.Normal,
                            fontFamily = OldStandart)
                    }
                    if (flag.value){
                        when{
                            viewModel.users.value.isEmpty() -> CircularProgressIndicator()
                            viewModel.users.value.isNotEmpty() -> {
                                navController.navigate("MainPage")
                                flag.value = false
                            }
                        }
                    }
                    Button(
                        modifier = Modifier
                            .padding(top = 5.dp)
                            .height(50.dp)
                            .width(280.dp),
                        onClick = {navController.navigate("MainPage")},
                        shape = RoundedCornerShape(23.dp),
                        border = BorderStroke(1.dp, Color(255,215,0, 0)),
                        colors = ButtonDefaults.buttonColors(
                            containerColor = Color(255, 215, 0, 0),
                            contentColor = Color.White
                        )
                    )
                    {
                        Text("Продолжить без входа", fontSize = 18.sp,
                            color = Color.White,
                            fontWeight = FontWeight.Normal,
                            fontFamily = OldStandart)
                    }
                }
            }

            Column(
                modifier = Modifier.padding(),
                verticalArrangement = Arrangement.Bottom,
                horizontalAlignment = Alignment.CenterHorizontally
            )
            {
                Text("Или войдите с помощью", fontSize = 16.sp,
                    color = Color.White,
                    fontWeight = FontWeight.Normal,
                    fontFamily = OldStandart)
                Button(
                    modifier = Modifier
                        .padding(top = 10.dp)
                        .height(70.dp)
                        .width(270.dp),
                    onClick = {},
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color(230, 235, 239, 123),
                        contentColor = Color.Gray
                    ),
                    shape = RoundedCornerShape(10.dp),
                    border = BorderStroke(0.dp, color = Color(230, 235, 239,123))
                )
                {
                    Text(
                        "Войти с Google",
                        fontSize = 20.sp,
                        color = Color.White,
                        fontWeight = FontWeight.Normal,
                        fontFamily = OldStandart
                    )
                }
                Button(
                    modifier = Modifier
                        .padding(top = 10.dp),
                    onClick = {navController.navigate("RegistrationScreen")},
                    colors = ButtonDefaults.buttonColors(
                        containerColor = Color(230, 235, 239,0),
                        contentColor = Color.Gray
                    ),
                    border = BorderStroke(0.dp, color = Color(230, 235, 239, 0))
                )
                {
                    Text(
                        "Нет аккаунта? Зарегистрируйтесь",
                        fontSize = 18.sp,
                        color = Color.White,
                        fontWeight = FontWeight.Normal,
                        fontFamily = OldStandart
                    )
                }
            }
        }
    }
}