package com.example.triphelper.view.RouteScreens

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.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.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
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.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.style.AttractionsLazy
import com.example.triphelper.view.style.HotelsLazy
import com.example.triphelper.view.style.MetroLazy
import com.example.triphelper.view.style.RestaurantsLazy
import com.example.triphelper.view.style.StyleMainFone
import com.example.triphelper.view.theme.RobotoB
import com.example.triphelper.view.theme.SeoulBold

//
@Composable
fun Tours(navController: NavController, iconsRouteIsActivity: MutableState<Boolean>,titleCity : String, numberTour: String,  viewModel: RouteViewModel = hiltViewModel()) {
    iconsRouteIsActivity.value = true
    val titleToure by viewModel.titleToure.collectAsState("")
    var attarctionsColumn by remember { mutableStateOf(true) }
    var hotelColumn by remember { mutableStateOf(false) }
    var restouranceColumn by remember { mutableStateOf(false) }
    var metroColumn by remember { mutableStateOf(false) }
    val orgTour by viewModel.orgTour.collectAsState(initial = emptyList())
    val metro by viewModel.metro.collectAsState(initial = emptyList())


    LaunchedEffect(key1 = viewModel.navigationTo) {
        viewModel.navigationTo.collect { destination ->
            destination?.let {
                navController.navigate(destination)
            }
        }
    }
    LaunchedEffect(Unit) {
        viewModel.getNameToure(numberTour.toInt())
    }
    viewModel.GetOrgTour(numberTour.toInt(),titleCity)
    StyleMainFone()
    Column(
        modifier = Modifier
            .fillMaxSize()
            .fillMaxWidth()
            .padding(top = 65.dp),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Box(
            modifier = Modifier
                .fillMaxWidth(0.8f)
                .background(Color(0xFF510B3C), RoundedCornerShape(10.dp))
                .height(50.dp),
            contentAlignment = Alignment.Center
        ) {
            Text(
                text = "${titleToure} тур",
                fontSize = 32.sp,
                fontFamily = RobotoB,
                color = Color.White,
                textAlign = TextAlign.Center
            )
        }
        Column(modifier = Modifier.padding(top = 10.dp)) {
            Text(
                text = titleCity,
                fontSize = 36.sp,
                fontFamily = SeoulBold,
                fontWeight = FontWeight.Bold,
                color = Color(0xFF510B3C),
                textAlign = TextAlign.Center
            )
        }
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 10.dp)
                .fillMaxHeight(0.1f)
                .background(Color.White)
                .border(
                    width = 5.dp,
                    color = Color(0xFF510B3C)
                ),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceEvenly
        ) {
            Box(
                modifier = Modifier
                    .fillMaxHeight(0.9f)
                    .background(if (attarctionsColumn) Color(0xFFDC5B6E).copy(alpha = 0.4f) else Color.Transparent)
            ) {
                IconButton(
                    onClick = {
                        attarctionsColumn = true
                        hotelColumn = false
                        restouranceColumn = false
                        metroColumn = false
                        viewModel.getCountry()
                    },
                    modifier = Modifier
                        .size(60.dp)
                ) {
                    Icon(
                        painter = painterResource(id = R.drawable.attractions_icon),
                        contentDescription = "Достопримечательности",
                        tint = Color.Unspecified,
                        modifier = Modifier.size(50.dp)
                    )
                }
            }
            Box(
                modifier = Modifier
                    .fillMaxHeight(0.9f)
                    .background(if (hotelColumn) Color(0xFFDC5B6E).copy(alpha = 0.4f) else Color.Transparent)
            ) {
                IconButton(
                    onClick = {
                        attarctionsColumn = false
                        hotelColumn = true
                        restouranceColumn = false
                        metroColumn = false
                    },
                    modifier = Modifier
                        .size(60.dp)
                ) {
                    Icon(
                        painter = painterResource(id = R.drawable.hotel_icons),
                        contentDescription = "Отели",
                        tint = Color.Unspecified,
                        modifier = Modifier.size(50.dp)
                    )
                }
            }
            Box(
                modifier = Modifier
                    .fillMaxHeight(0.9f)
                    .background(if (restouranceColumn) Color(0xFFDC5B6E).copy(alpha = 0.4f) else Color.Transparent)
            ) {
                IconButton(
                    onClick = {
                        attarctionsColumn = false
                        hotelColumn = false
                        restouranceColumn = true
                        metroColumn = false
                    },
                    modifier = Modifier
                        .size(60.dp)
                ) {
                    Icon(
                        painter = painterResource(id = R.drawable.restourants_icons),
                        contentDescription = "Рестораны",
                        tint = Color.Unspecified,
                        modifier = Modifier.size(45.dp)
                    )
                }
            }
            Box(
                modifier = Modifier
                    .fillMaxHeight(0.9f)
                    .background(if (metroColumn) Color(0xFFDC5B6E).copy(alpha = 0.4f) else Color.Transparent)
            ) {
                IconButton(
                    onClick = {
                        attarctionsColumn = false
                        hotelColumn = false
                        restouranceColumn = false
                        metroColumn = true
                    },
                    modifier = Modifier
                        .size(60.dp)
                ) {
                    Icon(
                        painter = painterResource(id = R.drawable.metro_icons),
                        contentDescription = "Метро",
                        tint = Color.Unspecified,
                        modifier = Modifier.size(45.dp)
                    )
                }
            }
        }
        LaunchedEffect(Unit) {
            viewModel.GetOrgTour(numberTour.toInt(), titleCity)
        }
        if (attarctionsColumn) {
            AttractionsLazy(navController,orgTour)
        }
        if (hotelColumn) {
            HotelsLazy(navController,orgTour)
        }
        if(restouranceColumn){
            RestaurantsLazy(navController,orgTour)
        }
        if(metroColumn){
            MetroLazy(metro)
        }
    }
}