package com.example.mystictale.Screen.registration

import androidx.compose.foundation.Image
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
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.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.paint
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.text.isDigitsOnly
import androidx.navigation.NavController
import com.example.mystictale.R
import com.example.mystictale.ViewModels.AuthViewModel
import com.example.mystictale.navigation.Screens
import com.example.mystictale.resources.components.PasswordTextField
import com.example.mystictale.resources.components.ValidationPassword
import com.example.mystictale.ui.theme.DarkPurple
import com.example.mystictale.ui.theme.Grey
import com.example.mystictale.ui.theme.OpenSans

@Composable
fun RegistrationPassword(navController: NavController, viewModel: AuthViewModel) {
    val focusManager = LocalFocusManager.current
    val keyboardController = LocalSoftwareKeyboardController.current
    val password = remember {
        mutableStateOf("")
    }
    val repeatedPassword = remember {
        mutableStateOf("")
    }
    val passwordFlag = remember { mutableStateOf(false) }
    Column(
        Modifier
            .fillMaxSize()
            .paint(
                painterResource(id = R.drawable.background),
                contentScale = ContentScale.FillBounds
            )
            .pointerInput(Unit) {
                detectTapGestures(onTap = {
                    keyboardController?.hide()
                    focusManager.clearFocus()
                })
            }
            .padding(10.dp),
        verticalArrangement = Arrangement.SpaceBetween,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        Column {
            Box(
                modifier = Modifier
                    .padding(top = 30.dp)
                    .height(40.dp)
                    .fillMaxWidth(),
                contentAlignment = Alignment.TopStart
            ) {
                Button(
                    onClick = { navController.navigate(Screens.RegistrationEmail.route) },
                    colors = ButtonColors(
                        disabledContentColor = Color.White,
                        disabledContainerColor = Color.Transparent,
                        contentColor = Color.White,
                        containerColor = Color.Transparent
                    )
                ) {
                    Image(
                        painter = painterResource(id = R.drawable.back),
                        contentDescription = "back",
                        modifier = Modifier
                            .width(11.dp)
                            .height(14.dp)
                    )
                }
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 22.dp),
                    horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically
                ) {


                    LinearProgressIndicator(
                        progress = 0.4f,
                        modifier = Modifier.width(157.dp),
                        strokeCap = StrokeCap.Round,
                        trackColor = Grey,
                        color = DarkPurple
                    )
                }
            }
            Column(
                modifier = Modifier
                    .padding(20.dp)
                    .padding(start = 15.dp, end = 5.dp)
            ) {
                Spacer(modifier = Modifier.height(30.dp))
                Text(
                    text = "Придумайте пароль",
                    fontWeight = FontWeight.Bold,
                    fontFamily = OpenSans,
                    fontSize = 24.sp,
                    color = Color.White,
                    modifier = Modifier
                        .fillMaxWidth(),
                    style = MaterialTheme.typography.headlineMedium
                )
                Spacer(modifier = Modifier.height(30.dp))

                Text(
                    text = "Пароль должен:\n" +
                            "  ⚪ состоять не менее чем из 6 символов\n" +
                            "  ⚪ содержать хотя бы одну букву\n" +
                            "  ⚪ содержать хотя бы одну цифру", fontWeight = FontWeight.Light,
                    fontFamily = OpenSans,
                    fontSize = 13.sp,
                    color = Color.White,
                    modifier = Modifier
                        .fillMaxWidth()
                )


                Spacer(modifier = Modifier.height(30.dp))

                PasswordTextField(
                    password.value, {
                        password.value = it
                        passwordFlag.value = false
                    }, "Пароль",
                    passwordFlag.value
                )

                Spacer(modifier = Modifier.height(30.dp))

                PasswordTextField(
                    repeatedPassword.value,
                    { repeatedPassword.value = it },
                    "Повторный пароль",
                    passwordFlag.value
                )
                if (passwordFlag.value) {
                    if (password.value.isEmpty()) {
                        Text(
                            "Введите пароль",
                            fontWeight = FontWeight.Light,
                            fontFamily = OpenSans,
                            fontSize = 13.sp,
                            color = Color.Red
                        )
                    } else if (password.value.length < 6) {
                        Text(
                            "Пароль содержит меньше 6 символов",
                            fontWeight = FontWeight.Light,
                            fontFamily = OpenSans,
                            fontSize = 13.sp,
                            color = Color.Red
                        )
                    } else if (password.value.isDigitsOnly()) {
                        Text(
                            "Пароль не содержит букв",
                            fontWeight = FontWeight.Light,
                            fontFamily = OpenSans,
                            fontSize = 13.sp,
                            color = Color.Red
                        )
                    } else if (!password.value.any { it.isDigit() }) {
                        Text(
                            "Пароль не содержит цифр",
                            fontWeight = FontWeight.Light,
                            fontFamily = OpenSans,
                            fontSize = 13.sp,
                            color = Color.Red
                        )
                    } else if (password.value != repeatedPassword.value) {
                        Text(
                            "Пароли не совпадают",
                            fontWeight = FontWeight.Light,
                            fontFamily = OpenSans,
                            fontSize = 13.sp,
                            color = Color.Red
                        )
                    }
                }
            }
        }


        Box(Modifier.padding(bottom = 50.dp)) {
            Button(
                onClick = {
                    if (!ValidationPassword(password.value, repeatedPassword.value)) {
                        viewModel.passwordUser = password.value
                        navController.navigate(Screens.RegistrationName.route)
                    } else {
                        passwordFlag.value = true
                    }
                },
                modifier = Modifier
                    .fillMaxWidth()
                    .height(48.dp)
                    .padding(horizontal = 30.dp),
                shape = RoundedCornerShape(12.dp),
                colors = ButtonColors(
                    containerColor = DarkPurple,
                    contentColor = Color.White,
                    disabledContentColor = Color.White,
                    disabledContainerColor = DarkPurple
                )
            ) {
                Text("Далее", fontSize = 20.sp, fontWeight = FontWeight.Bold)

            }

        }


    }
}