package com.example.triphelper.view.Auth


import androidx.compose.foundation.background
import androidx.compose.foundation.border
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.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
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.clip
import androidx.compose.ui.graphics.Color
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.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.example.testirovanye_supabase.R
import com.example.triphelper.view.navigation.NavigationRoutes
import com.example.triphelper.view.style.ButtonStyleBackSquare
import com.example.triphelper.view.style.IconButtonStyleBackSquare
import com.example.triphelper.view.style.StyleAutorisationFone
import com.example.triphelper.view.style.TextFieldStyleAuth
import com.example.triphelper.view.style.TextFieldStylePasswordAuth
import com.example.triphelper.view.style.TripHelperLogoStyle
import com.example.triphelper.view.theme.Roboto

//Данная функция отвечает за экран авторизации. Подключается к viewModel и передает данные, вводимые пользователем
@Composable
fun Auth(navController: NavController, viewModel: AuthViewModel = hiltViewModel()) {
    val email = remember { mutableStateOf("") } //Переменная для сохранения почты
    val password = remember { mutableStateOf("") } //Переменная для сохранения пароля

    LaunchedEffect(key1 = viewModel.navigationTo) {
        viewModel.navigationTo.collect { destination ->
            destination?.let {
                navController.navigate(destination)
            }
        }
    }
    StyleAutorisationFone() //Стиль фона для авторизации
    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.SpaceAround,
        horizontalAlignment = Alignment.CenterHorizontally
    )
    {
        TripHelperLogoStyle() //Отрисовка логотипа
        Column(
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Column(
                verticalArrangement = Arrangement.spacedBy(42.dp),
                modifier = Modifier.fillMaxWidth(0.9f)
            )
            {
                TextFieldStyleAuth("Логин", email,true)
                TextFieldStylePasswordAuth("Пароль", password,false)
                if(viewModel.wrongSignIn.value){
                    Box(
                        modifier = Modifier
                            .fillMaxHeight(0.1f)
                            .fillMaxWidth()
                            .clip(RoundedCornerShape(10.dp))
                            .background(color = Color.White)
                            .border(
                                width = 3.dp,
                                color = Color(0xFF510B3C),
                                shape = RoundedCornerShape(10.dp)
                            ),
                    ){
                        Text(
                            text = viewModel.wrongText.value,
                            fontFamily = Roboto,
                            fontWeight = FontWeight.W600,
                            fontSize = 22.sp,
                            color = Color.Red,
                            modifier = Modifier.align(Alignment.Center)
                        )
                    }

                }
                Row(
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    IconButtonStyleBackSquare(
                        IconbuttonContent = {Icon(
                            painter = painterResource(id = R.drawable.adduser),
                            contentDescription = "Создать пользователя",
                            tint = Color.White,
                            modifier = Modifier.fillMaxSize(0.8f)
                        )})
                    {
                        navController.navigate(NavigationRoutes.REG)
                    }
                    Spacer(modifier = Modifier.width(20.dp))
                    ButtonStyleBackSquare("Вход",45.sp) {
                        viewModel.onSignInEmailPassword(
                            email.value,
                            password.value,
                            navController
                        )
                    }
                }
            }
        }
    }
}




