package com.example.triphelper.view.Registration

import androidx.compose.foundation.Image
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.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.buttonColors
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
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.res.painterResource
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.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.TextFieldStyleAuth
import com.example.triphelper.view.style.TextFieldStylePasswordAuth
import com.example.triphelper.view.style.TripHelperLogoStyle
import com.example.triphelper.view.theme.Roboto
import com.example.triphelper.view.theme.SeoulLight


@Composable
fun Registration(
    navController: NavController,
    viewModel: RegistrationViewModel = hiltViewModel()
) {
    val email = remember { mutableStateOf("") } //Переменная для сохранения почты
    var expanded by remember { mutableStateOf(false) } //Переменная для всплывающего списка
    val password = remember { mutableStateOf("") } //Переменная для сохранения пароля
    var selectedItem by remember { mutableStateOf("") } //Переменная для всплывающего списка
    val repeatPassword = remember { mutableStateOf("") } //Переменная для сохранения повтора пароля
    val name = remember { mutableStateOf("") } //Переменная для сохранения имени
    val coutryTitle = remember { mutableStateOf("") } //Переменная для сохранения названия страны
    val country by viewModel.country.collectAsState(initial = emptyList()) //Переменная для списка стран

    LaunchedEffect(key1 = viewModel.navigationTo) {
        viewModel.navigationTo.collect { destination ->
            destination?.let {
                navController.navigate(destination)
            }
        }
    }
    Box(modifier = Modifier.fillMaxSize())
    {
        Image(
            painter = painterResource(id = R.drawable.foneauth),
            contentDescription = null,
            modifier = Modifier.fillMaxSize()
        )
    }
    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.SpaceAround,
        horizontalAlignment = Alignment.CenterHorizontally
    )
    {

        TripHelperLogoStyle()
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.fillMaxWidth(0.9f)
        ) {
            Column(
                verticalArrangement = Arrangement.spacedBy(20.dp)
            )
            {
                TextFieldStyleAuth("Почта", email,true)
                TextFieldStyleAuth("Имя", name, true)
                Box {
                    Button(
                        onClick = {
                            expanded = true
                            viewModel.getCountry()
                        },
                        shape = RoundedCornerShape(15.dp),
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(70.dp)
                            .border(
                                width = 4.dp,
                                color = Color(0xFF510B3C),
                                shape = RoundedCornerShape(15.dp)
                            ),
                        colors = buttonColors(
                            containerColor = Color.White
                        )
                    ) {
                        Row(
                            modifier = Modifier.fillMaxSize(),
                            horizontalArrangement = Arrangement.Start,
                            verticalAlignment = Alignment.CenterVertically
                        )
                        {
                            if (selectedItem.isEmpty()) {
                                Row(
                                    horizontalArrangement = Arrangement.SpaceBetween,
                                    verticalAlignment = Alignment.CenterVertically,
                                    modifier = Modifier.fillMaxWidth()
                                )
                                {
                                    Text(
                                        "Выберите страну",
                                        fontFamily = SeoulLight,
                                        fontSize = 24.sp,
                                        color = Color(0xFFDC5B6E).copy(alpha = 0.7f),
                                        textAlign = TextAlign.Start
                                    )
                                    Icon(
                                        painter = painterResource(id = R.drawable.galka_down),
                                        contentDescription = null,
                                        tint = Color(0xFFDC5B6E).copy(alpha = 0.7f),

                                    )
                                }
                            } else {
                                Text(
                                    "$selectedItem",
                                    fontFamily = SeoulLight,
                                    fontWeight = FontWeight.W600,
                                    fontSize = 25.sp,
                                    color = Color(0xFFDC5B6E),
                                    textAlign = TextAlign.Start
                                )
                            }

                        }
                    }
                    DropdownMenu(
                        expanded = expanded,
                        onDismissRequest = { expanded = false },
                        containerColor = Color.White,
                        modifier = Modifier
                            .fillMaxWidth(0.9f)
                            .height(200.dp)
                    ) {
                        country.forEach { item ->
                            DropdownMenuItem(
                                onClick = {
                                    selectedItem = item.title
                                    coutryTitle.value = item.title
                                    expanded = false
                                },
                                text = {
                                    Text(
                                        text = item.title,
                                        fontSize = 18.sp,
                                        fontFamily = Roboto,
                                        color = Color(0xFF510B3C),
                                        fontWeight = FontWeight.Bold,
                                        textAlign = TextAlign.Start
                                    )
                                }
                            )
                        }
                    }
                }
                TextFieldStylePasswordAuth("Пароль", password, true)
                TextFieldStylePasswordAuth("Повтор пароля", repeatPassword, false)
                if(viewModel.wrongSignUp.value){
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .clip(RoundedCornerShape(10.dp))
                            .background(color = Color.White)
                            .border(
                                width = 2.dp,
                                color = Color.White,
                                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(
                modifier = Modifier

                    .padding(top = 20.dp),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                IconButtonStyleBackSquare(
                    IconbuttonContent = {Icon(
                        painter = painterResource(id = R.drawable.backauth),
                        contentDescription = "Назад",
                        tint = Color.White,
                        modifier = Modifier.fillMaxSize(0.8f)
                    )})
                {
                    navController.navigate(NavigationRoutes.AUTH)
                }
                Spacer(modifier = Modifier.width(20.dp))
                ButtonStyleBackSquare("Регистрация",33.sp) {
                    viewModel.createNewUser(
                        emailUser = email.value,
                        passwordUser = password.value,
                        repeatPasswordUser = repeatPassword.value,
                        nameUser = name.value,
                        countryTitle = coutryTitle.value
                    )
                }
            }
        }
    }
}
