package com.example.triphelper.view.RouteScreens

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
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.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.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.theme.Roboto
import com.example.triphelper.view.theme.SeoulBold

@Composable
fun RestaurantsDesc(navController: NavController, iconsRouteIsActivity: MutableState<Boolean>,idRestaurants: Int, titleRestaurants:String, viewModel: RouteViewModel = hiltViewModel()) {
    iconsRouteIsActivity.value = true
    val restaurants by viewModel.restaurants.collectAsState(initial = emptyList())

    LaunchedEffect(key1 = viewModel.navigationTo) {
        viewModel.navigationTo.collect { destination ->
            destination?.let {
                navController.navigate(destination)
            }
        }
    }
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.White),
        verticalArrangement = Arrangement.Top,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Column {
            Text(
                text = titleRestaurants,
                color = Color(0xFF510B3C),
                fontSize = 28.sp,
                fontFamily = SeoulBold,
                fontWeight = FontWeight.Bold,
                textAlign = TextAlign.Center,
                modifier = Modifier.padding(top = 60.dp)
            )
        }
        LaunchedEffect(Unit) {
            viewModel.GetRestaurants(idRestaurants)
        }
        LazyColumn(
            modifier = Modifier
                .padding(top = 20.dp, bottom = 60.dp)
                .fillMaxHeight(0.94f)
        ) {
            items(
                restaurants,
                key = { restaurants -> restaurants.id },
            ) { restaurants ->
                val imageState = rememberAsyncImagePainter(
                    model = ImageRequest.Builder(LocalContext.current).data(restaurants.photo)
                        .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()
                    ) {
                        Box(
                            contentAlignment = Alignment.Center,
                            modifier = Modifier.padding(bottom = 20.dp).fillMaxWidth()
                        ) {
                            Image(
                                modifier = Modifier
                                    .fillMaxWidth(0.9f)
                                    .height(190.dp)
                                    .clip(RoundedCornerShape(10.dp)),
                                painter = imageState.painter,
                                contentDescription = "",
                                contentScale = ContentScale.Crop,
                                alignment = Alignment.Center
                            )
                        }
                        Column(
                            horizontalAlignment = Alignment.Start,
                            verticalArrangement = Arrangement.spacedBy(20.dp),
                            modifier = Modifier.padding(start = 20.dp).fillMaxWidth(0.9f)
                        ) {
                            Text(
                                text = "Адрес",
                                color = Color(0xFF510B3C),
                                fontSize = 26.sp,
                                fontFamily = SeoulBold,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Start
                            )
                            Text(
                                text = restaurants.address,
                                color = Color(0xFF000000),
                                fontSize = 20.sp,
                                fontFamily = Roboto,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Start,
                            )
                            Text(
                                text = "Основная информация",
                                color = Color(0xFF510B3C),
                                fontSize = 26.sp,
                                fontFamily = SeoulBold,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Start
                            )
                            Text(
                                text = restaurants.basic_info,
                                color = Color(0xFF000000),
                                fontSize = 20.sp,
                                fontFamily = Roboto,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Start
                            )
                            Text(
                                text = "Интерьер",
                                color = Color(0xFF510B3C),
                                fontSize = 26.sp,
                                fontFamily = SeoulBold,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Start
                            )
                            Text(
                                text = restaurants.interior,
                                color = Color(0xFF000000),
                                fontSize = 20.sp,
                                fontFamily = Roboto,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Start,
                            )
                            Text(
                                text = "Цены",
                                color = Color(0xFF510B3C),
                                fontSize = 26.sp,
                                fontFamily = SeoulBold,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Start
                            )
                            Text(
                                text = restaurants.price,
                                color = Color(0xFF000000),
                                fontSize = 20.sp,
                                fontFamily = Roboto,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Start,
                            )
                            Text(
                                text = "Рекомендации",
                                color = Color(0xFF510B3C),
                                fontSize = 26.sp,
                                fontFamily = SeoulBold,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Start
                            )
                            Text(
                                text = restaurants.recommendations,
                                color = Color(0xFF000000),
                                fontSize = 20.sp,
                                fontFamily = Roboto,
                                fontWeight = FontWeight.Bold,
                                textAlign = TextAlign.Start,
                                modifier = Modifier
                                    .padding(bottom = 40.dp)
                            )
                        }
                    }
                }
            }

        }
    }
}