package com.example.triphelper.view.MainScreen


import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import com.example.triphelper.view.style.ButtonStyleBackSquare
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.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.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.MutableState
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.StyleMainFone
import com.example.triphelper.view.theme.Roboto
import com.example.triphelper.view.theme.RobotoB
import com.example.triphelper.view.theme.SeoulBold

@Composable
fun LawScreen(
    navController: NavController,
    iconsLawIsActivity: MutableState<Boolean>,
    viewModel: LawScreenViewModel = hiltViewModel()
) {
    iconsLawIsActivity.value = true
    var selectedItem by remember { mutableStateOf("") } //Переменная для всплывающего списка
    val coutryTitle = remember { mutableStateOf("") } //Переменная для сохранения названия страны
    val country by viewModel.country.collectAsState(initial = emptyList())
    var expanded by remember { mutableStateOf(false) } //Переменная для всплывающего списка
    var wrongEmpty by remember { mutableStateOf(false) }

    LaunchedEffect(key1 = viewModel.navigationTo) {
        viewModel.navigationTo.collect { destination ->
            destination?.let {
                navController.navigate(destination)
            }
        }
    }
    StyleMainFone()
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight(0.9f),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = "Страна",
            fontFamily = SeoulBold,
            fontWeight = FontWeight.Bold,
            fontSize = 40.sp,
            color = Color(0xFF510B3C),
            modifier = Modifier.padding(20.dp)
        )
        Box(modifier = Modifier.padding(20.dp)) {
            Button(
                onClick = {
                    expanded = true
                    viewModel.getCountry()
                },
                shape = RoundedCornerShape(15.dp),
                modifier = Modifier
                    .fillMaxWidth(0.9f)
                    .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 = RobotoB,
                                fontSize = 22.sp,
                                color = Color(0xFF510B3C).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 = RobotoB,
                            fontWeight = FontWeight.W600,
                            fontSize = 23.sp,
                            color = Color(0xFF510B3C),
                            textAlign = TextAlign.Start
                        )
                    }
                }
            }
            DropdownMenu(
                expanded = expanded,
                onDismissRequest = { expanded = false },
                containerColor = Color.White,
                modifier = Modifier
                    .fillMaxWidth(0.8f)
                    .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
                            )
                        }
                    )
                }
            }
        }
        if (wrongEmpty) {
            Box(
                modifier = Modifier
                    .fillMaxWidth(0.8f)
                    .clip(RoundedCornerShape(10.dp))
                    .background(color = Color.White)
                    .border(
                        width = 2.dp,
                        color = Color.White,
                        shape = RoundedCornerShape(10.dp)
                    ),
            ) {
                Text(
                    text = "Не выбрана страна",
                    fontFamily = Roboto,
                    fontWeight = FontWeight.W600,
                    fontSize = 22.sp,
                    color = Color.Red,
                    modifier = Modifier.align(Alignment.Center)
                )
            }
        }
        Row(modifier = Modifier.fillMaxWidth(0.6f).padding(top = 10.dp)) {
            ButtonStyleBackSquare("Далее", 33.sp) {
                viewModel.GetRules(coutryTitle.value)
                if (coutryTitle.value != "") {
                    wrongEmpty = false
                    navController.navigate("${NavigationRoutes.DESCLAW}/${coutryTitle.value}")
                }
                else{
                    wrongEmpty = true
                }
            }
        }
    }
}
