package com.example.triphelper.view.MainScreen


import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
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.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.CircularProgressIndicator
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.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.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
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 coil.compose.AsyncImagePainter
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import com.example.triphelper.view.navigation.NavigationRoutes
import com.example.triphelper.view.style.StyleMainFone
import com.example.triphelper.view.theme.RobotoB
import com.example.triphelper.view.theme.SeoulBold
import kotlin.random.Random

@Composable
fun MainScreen(
    navController: NavController, iconsMainIsActivity: MutableState<Boolean>, viewModel: MainScreenViewModel = hiltViewModel()) {
    iconsMainIsActivity.value = true
    val attractions by viewModel.attractions.collectAsState(initial = emptyList())
    val shuffledAttractions = remember(attractions) { // Перемешивание ТОЛЬКО при изменении attractions
        attractions.shuffled(Random(System.currentTimeMillis()))
    }
    LaunchedEffect(key1 = viewModel.navigationTo) {
        viewModel.navigationTo.collect { destination ->
            destination?.let {
                navController.navigate(destination)
            }
        }
    }
    StyleMainFone()
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight(0.89f)
            .background(color = Color.Transparent)
    ) {
        Column(
            modifier = Modifier
                .background(Color.Transparent)
                .padding(top = 50.dp, bottom = 20.dp),
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Text(
                text = "Рекомендации",
                fontFamily = SeoulBold,
                fontWeight = FontWeight.Bold,
                fontSize = 40.sp,
                modifier = Modifier.padding(top = 15.dp),
                color = Color(0xFF510B3C)
            )
        }
        LaunchedEffect(Unit) {
            viewModel.GetAttractions()
        }
        LazyColumn {
            items(
                shuffledAttractions,
                key = { attractions -> attractions.id },
            ) { attractions ->
                val imageState = rememberAsyncImagePainter(
                    model = ImageRequest.Builder(LocalContext.current).data(attractions.photo)
                        .size(coil.size.Size.ORIGINAL).build()
                ).state
                val flagsState = rememberAsyncImagePainter(
                    model = ImageRequest.Builder(LocalContext.current)
                        .data(attractions.city.country.flag)
                        .size(coil.size.Size.ORIGINAL).build()
                ).state
                if (imageState is AsyncImagePainter.State.Error) {
                    Box(
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(200.dp),
                        contentAlignment = Alignment.Center
                    ) {
                        CircularProgressIndicator()
                    }
                }
                if (imageState is AsyncImagePainter.State.Success) {
                    Column(
                        modifier = Modifier
                            .fillMaxWidth(0.9f)
                            .padding(bottom = 40.dp),
                    ) {
                        Box(
                            Modifier
                                .fillMaxSize()
                                .clickable {
                                    navController.navigate("${NavigationRoutes.ATT_DESC}/${attractions.id}/${attractions.name}")
                                }
                        ) {

                            Image(
                                modifier = Modifier
                                    .fillMaxSize()
                                    .size(210.dp)
                                    .clip(RoundedCornerShape(20.dp))
                                    .border(
                                        color = Color(0xFF510B3C),
                                        width = 5.dp,
                                        shape = RoundedCornerShape(20.dp)
                                    )
                                    .clickable {
                                        navController.navigate("${NavigationRoutes.ATT_DESC}/${attractions.id}/${attractions.name}")
                                    },
                                painter = imageState.painter,
                                contentDescription = "",
                                contentScale = ContentScale.Crop
                            )
                            Box(
                                Modifier.fillMaxWidth()
                                    .size(210.dp)
                                    .clip(RoundedCornerShape(20.dp))
                                    .background(color = Color(0xFF000000).copy(alpha = 0.4f))
                                    .padding(bottom = 10.dp),
                                contentAlignment = Alignment.BottomCenter
                            ) {
                                Text(
                                    text = attractions.name,
                                    color = Color.White,
                                    fontSize = 24.sp,
                                    fontFamily = RobotoB,
                                    textAlign = TextAlign.Center
                                )
                            }
                            Box(
                                Modifier.fillMaxWidth(0.25f)
                                    .size(70.dp),
                                contentAlignment = Alignment.TopStart
                            ) {
                                flagsState.painter?.let {
                                    Image(
                                        modifier = Modifier
                                            .wrapContentSize()
                                            .padding(start = 20.dp, top = 20.dp)
                                            .clip(RoundedCornerShape(0.dp)),
                                        painter = it,
                                        contentDescription = "",
                                        contentScale = ContentScale.Crop
                                    )
                                }
                            }
                        }

                    }
                }

            }
        }
    }
}
