package com.example.iplace.main.viewModel.mainView

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.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.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Lock
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.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.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
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.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.example.iplace.R
import com.example.iplace.main.viewModel.MainViewModel
import com.example.iplace.ui.theme.beige_white
import com.example.iplace.ui.theme.brown_contrast

@Composable
fun  Auth(navController: NavHostController){
    val viewModel = viewModel<MainViewModel>()
    var email by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }
    var isPasswordVisible by remember { mutableStateOf(false) }

    val headlinesOswald = FontFamily( //загрузка шрифта
        Font(resId = R.font.headlines_oswald)
    )
    val basicNunitosans = FontFamily(
        Font(resId = R.font.basic_nunitosans)
    )

    Image(
        painter = painterResource(id = R.drawable.au3),
        contentDescription = "",
        contentScale = ContentScale.FillHeight,
        modifier = Modifier.fillMaxSize()
    )

    Column(
        modifier = Modifier
            .fillMaxSize(),
        verticalArrangement = Arrangement.Top,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Spacer(modifier = Modifier.height(60.dp)) //отступ между блоками
        Text(
            text = "I.PLACE",
            fontSize = 45.sp,
            fontWeight = FontWeight.Bold,
            fontFamily = headlinesOswald,
            color = Color.White
        )
        Spacer(modifier = Modifier.height(55.dp))
        Box(
            modifier = Modifier
                .width(350.dp)
                .height(485.dp)
                .clip(RoundedCornerShape(40.dp))
                .background(beige_white.copy(alpha = 0.8f))
//        .alpha(0.8f) //возможная прозрачность формы
        ) {
            Column(
                modifier = Modifier.fillMaxSize(),
                verticalArrangement = Arrangement.Top,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Spacer(modifier = Modifier.height(5.dp)) // Отступ от заголовка

                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    IconButton(onClick = { navController.navigate("selectEnter") }) {
                        Icon(
                            imageVector = Icons.Default.ArrowBack, // Или другой иконки
                            contentDescription = "Назад",
                            modifier = Modifier.padding(top = 20.dp)
                        )
                    }
                    Spacer(modifier = Modifier.width(80.dp)) // Отступ между кнопкой и текстом
                    Text(
                        text = "Авторизация",
                        fontSize = 18.sp,
                        fontWeight = FontWeight.Bold,
                        fontFamily = headlinesOswald,
                        modifier = Modifier.padding(top = 20.dp) // Отступ сверху
                    )
                }

                Spacer(modifier = Modifier.height(40.dp)) // Отступ от заголовка
                OutlinedTextField(
                    value = email,
                    onValueChange = { newText -> email = newText },
                    label = { Text("Логин", color = Color.Black) },
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp)
                        .clip(RoundedCornerShape(20.dp)),
                    textStyle = TextStyle(color = Color.Black)
                )

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

                Column(
                    modifier = Modifier.fillMaxWidth(),
                    verticalArrangement = Arrangement.Center,
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
                    OutlinedTextField(
                        value = password,
                        onValueChange = { newText -> password = newText },
                        label = { Text("Пароль", color = Color.Black) },
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(horizontal = 16.dp)
                            .clip(RoundedCornerShape(20.dp)),
                        textStyle = TextStyle(color = Color.Black),
                        visualTransformation = if (isPasswordVisible) VisualTransformation.None else PasswordVisualTransformation(),
                        trailingIcon = {
                            IconButton(onClick = { isPasswordVisible = !isPasswordVisible }) {
                                Icon(
                                    imageVector = if (isPasswordVisible) Icons.Filled.Lock else Icons.Filled.Lock,
                                    contentDescription = if (isPasswordVisible) "Скрыть пароль" else "Показать пароль",
                                    tint = Color.Black
                                )
                            }
                        },
                        keyboardOptions = KeyboardOptions.Default.copy(keyboardType = KeyboardType.Password)
                    )
                    Spacer(modifier = Modifier.height(10.dp))
                }
                Spacer(modifier = Modifier.height(10.dp))
                Text(
                    text = viewModel.InfoText.value,
                    fontSize = 18.sp,
                    color = Color.DarkGray
                )
                Spacer(modifier = Modifier.height(20.dp))

                Button(
                    onClick = {
                        viewModel.AutorisationFun(email, password, navController)
                    },
                    modifier = Modifier.width(290.dp).height(50.dp),
                    colors = ButtonDefaults.buttonColors(brown_contrast)
                ) {
                    Text(
                        text = "Войти",
                        fontSize = 24.sp,
                        fontFamily = basicNunitosans
                    )
                }

                Spacer(modifier = Modifier.height(16.dp)) // Отступ снизу
            }
        }
    }
}


