package com.example.triphelper.view.style

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.Row
import androidx.compose.foundation.layout.fillMaxHeight
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.width
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.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.res.painterResource
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.testirovanye_supabase.R
import com.example.triphelper.model.Advice
import com.example.triphelper.model.Metro
import com.example.triphelper.model.Organization_of_tours
import com.example.triphelper.view.RouteScreens.AdviceViewModel
import com.example.triphelper.view.RouteScreens.RouteViewModel
import com.example.triphelper.view.navigation.NavigationRoutes
import com.example.triphelper.view.theme.Roboto
import com.example.triphelper.view.theme.RobotoB

@Composable
fun AttractionsLazy(navController: NavController,orgTour:List<Organization_of_tours>,viewModel: RouteViewModel = hiltViewModel()){
    LazyColumn(
        modifier = Modifier.fillMaxHeight(0.84f)
    ) {
        items(
            orgTour,
            key = { orgTour -> orgTour.id },
        ) { orgTour ->
            val imageState = rememberAsyncImagePainter(
                model = ImageRequest.Builder(LocalContext.current).data(orgTour.attractions.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(0.8f)
                        .padding(top = 10.dp)
                        .height(175.dp)
                        .padding(bottom = 10.dp)
                        .border(
                            width = 4.dp,
                            color = Color(0xFF510B3C),
                            shape = RoundedCornerShape(15.dp)
                        ),
                ) {
                    Box(
                        Modifier
                            .fillMaxWidth()
                            .fillMaxHeight()
                            .clickable {
                                navController.navigate("${NavigationRoutes.ATT_DESC}/${orgTour.attractions.id}/${orgTour.attractions.name}")
                            }
                            .clip(RoundedCornerShape(15.dp))
                            .background(Color.White)
                    ) {
                        Row {
                            Image(
                                modifier = Modifier
                                    .width(140.dp)
                                    .fillMaxHeight()
                                    .clip(RoundedCornerShape(15.dp))
                                    .clickable {
                                        navController.navigate("${NavigationRoutes.ATT_DESC}/${orgTour.attractions.id}/${orgTour.attractions.name}")
                                    },
                                painter = imageState.painter,
                                contentDescription = "",
                                contentScale = ContentScale.Crop
                            )
                            Box(
                                Modifier
                                    .fillMaxWidth()
                                    .fillMaxHeight()
                                    .size(210.dp)
                                    .padding(top = 10.dp),
                                contentAlignment = Alignment.TopCenter
                            ) {
                                Text(
                                    text = orgTour.attractions.name,
                                    color = Color(0xFF510B3C),
                                    fontSize = 16.sp,
                                    fontFamily = RobotoB,
                                    textAlign = TextAlign.Center
                                )
                                Box(
                                    Modifier
                                        .fillMaxWidth()
                                        .fillMaxHeight()
                                        .padding(top = 10.dp, bottom = 20.dp, start = 5.dp),
                                    contentAlignment = Alignment.BottomStart
                                ) {
                                    Column(
                                        verticalArrangement = Arrangement.spacedBy(10.dp)
                                    ) {
                                        Row {
                                            Text(
                                                text = "Посещение: ",
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = RobotoB,
                                                textAlign = TextAlign.Center
                                            )
                                            Text(
                                                text = orgTour.attractions.visiting,
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = Roboto,
                                                textAlign = TextAlign.Center
                                            )
                                        }
                                        Row {
                                            Text(
                                                text = "Время работы: ",
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = RobotoB,
                                                textAlign = TextAlign.Center
                                            )
                                            Text(
                                                text = orgTour.attractions.work_schedule,
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = Roboto,
                                                textAlign = TextAlign.Center
                                            )
                                        }
                                        Row {
                                            Text(
                                                text = "Цена: ",
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = RobotoB,
                                                textAlign = TextAlign.Center
                                            )
                                            Text(
                                                text =
                                                if (orgTour.attractions.price_showing == 0) {
                                                    "Бесплтано"
                                                } else {
                                                    "${orgTour.attractions.price_showing} ${orgTour.attractions.currency.title}"
                                                },
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = Roboto,
                                                textAlign = TextAlign.Center
                                            )
                                        }
                                    }
                                }
                            }
                        }
                    }

                }
            }

        }
    }
}

@Composable
fun HotelsLazy(navController: NavController,orgTour:List<Organization_of_tours>,viewModel: RouteViewModel = hiltViewModel()){
    LazyColumn(
        modifier = Modifier.fillMaxHeight(0.84f)
    ) {
        items(
            orgTour,
            key = { orgTour -> orgTour.id },
        ) { orgTour ->
            val imageState = rememberAsyncImagePainter(
                model = ImageRequest.Builder(LocalContext.current).data(orgTour.hotels.photo_hotel)
                    .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.8f)
                        .padding(top = 10.dp)
                        .height(175.dp)
                        .padding(bottom = 10.dp)
                        .border(
                            width = 4.dp,
                            color = Color(0xFF510B3C),
                            shape = RoundedCornerShape(15.dp)
                        ),
                ) {
                    Box(
                        Modifier
                            .fillMaxWidth()
                            .fillMaxHeight()
                            .clickable {
                                navController.navigate("${NavigationRoutes.HOTEL_DESC}/${orgTour.hotels.id}/${orgTour.hotels.name}")
                            }
                            .clip(RoundedCornerShape(15.dp))
                            .background(Color.White)
                    ) {
                        Row {
                            Image(
                                modifier = Modifier
                                    .width(140.dp)
                                    .fillMaxHeight()
                                    .clip(RoundedCornerShape(15.dp))
                                    .clickable {
                                        navController.navigate("${NavigationRoutes.HOTEL_DESC}/${orgTour.hotels.id}/${orgTour.hotels.name}")
                                    },
                                painter = imageState.painter,
                                contentDescription = "",
                                contentScale = ContentScale.Crop
                            )
                            Box(
                                Modifier
                                    .fillMaxWidth()
                                    .fillMaxHeight()
                                    .size(210.dp)
                                    .padding(top = 10.dp, start = 5.dp),
                                contentAlignment = Alignment.TopCenter
                            ) {
                                Text(
                                    text = orgTour.hotels.name,
                                    color = Color(0xFF510B3C),
                                    fontSize = 16.sp,
                                    fontFamily = RobotoB,
                                    textAlign = TextAlign.Center
                                )
                                Box(
                                    Modifier
                                        .fillMaxWidth()
                                        .fillMaxHeight()
                                        .padding(top = 10.dp, bottom = 20.dp),
                                    contentAlignment = Alignment.BottomStart
                                ) {
                                    Column(
                                        verticalArrangement = Arrangement.spacedBy(10.dp)
                                    ) {
                                        Row(
                                            verticalAlignment = Alignment.CenterVertically,
                                            modifier = Modifier.height(20.dp)
                                        ) {
                                            Text(
                                                text = "Рейтинг: ",
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = RobotoB,
                                                textAlign = TextAlign.Center
                                            )
                                            Row(
                                                verticalAlignment = Alignment.CenterVertically
                                            ){
                                                (0 until 5).map { IndexedValue(it, it < orgTour.hotels.rating) }
                                                    .forEach { (index, filled) ->
                                                        Image(
                                                            painter = painterResource(if (filled) R.drawable.star_active else R.drawable.star_notactive),
                                                            contentDescription = if (filled) "Filled Star" else "Outlined Star",
                                                            modifier = Modifier.size(20.dp),
                                                            contentScale = ContentScale.Fit,
                                                            alignment = Alignment.Center
                                                        )
                                                    }
                                            }
                                        }
                                        Row {
                                            Text(
                                                text = "Кол-во номеров: ",
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = RobotoB,
                                                textAlign = TextAlign.Center
                                            )
                                            Text(
                                                text = orgTour.hotels.quantity_of_rooms.toString(),
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = Roboto,
                                                textAlign = TextAlign.Center
                                            )
                                        }
                                        Row {
                                            Text(
                                                text = "Этажей: ",
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = RobotoB,
                                                textAlign = TextAlign.Center
                                            )
                                            Text(
                                                text = orgTour.hotels.floors.toString(),
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = Roboto,
                                                textAlign = TextAlign.Center
                                            )
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }

    }
}

@Composable
fun RestaurantsLazy(navController: NavController,orgTour:List<Organization_of_tours>,viewModel: RouteViewModel = hiltViewModel()){
    LazyColumn(
        modifier = Modifier.fillMaxHeight(0.84f)
    ) {
        items(
            orgTour,
            key = { orgTour -> orgTour.id },
        ) { orgTour ->
            val imageState = rememberAsyncImagePainter(
                model = ImageRequest.Builder(LocalContext.current).data(orgTour.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(0.8f)
                        .padding(top = 10.dp)
                        .height(175.dp)
                        .padding(bottom = 10.dp)
                        .border(
                            width = 4.dp,
                            color = Color(0xFF510B3C),
                            shape = RoundedCornerShape(15.dp)
                        ),
                ) {
                    Box(
                        Modifier
                            .fillMaxWidth()
                            .fillMaxHeight()
                            .clickable {
                                navController.navigate("${NavigationRoutes.REST_DESC}/${orgTour.restaurants.id}/${orgTour.restaurants.name}")
                            }
                            .clip(RoundedCornerShape(15.dp))
                            .background(Color.White)
                    ) {
                        Row {
                            Image(
                                modifier = Modifier
                                    .width(140.dp)
                                    .fillMaxHeight()
                                    .clip(RoundedCornerShape(15.dp))
                                    .clickable {
                                        navController.navigate("${NavigationRoutes.REST_DESC}/${orgTour.restaurants.id}/${orgTour.restaurants.name}")
                                    },
                                painter = imageState.painter,
                                contentDescription = "",
                                contentScale = ContentScale.Crop
                            )
                            Box(
                                Modifier
                                    .fillMaxWidth()
                                    .fillMaxHeight()
                                    .size(210.dp)
                                    .padding(top = 10.dp, start = 5.dp),
                                contentAlignment = Alignment.TopCenter
                            ) {
                                Text(
                                    text = orgTour.restaurants.name,
                                    color = Color(0xFF510B3C),
                                    fontSize = 16.sp,
                                    fontFamily = RobotoB,
                                    textAlign = TextAlign.Center
                                )
                                Box(
                                    Modifier
                                        .fillMaxWidth()
                                        .fillMaxHeight()
                                        .padding(top = 10.dp, bottom = 20.dp),
                                    contentAlignment = Alignment.BottomStart
                                ) {
                                    Column(
                                        verticalArrangement = Arrangement.spacedBy(10.dp)
                                    ) {
                                        Row {
                                            Text(
                                                text = "График: ",
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = RobotoB,
                                                textAlign = TextAlign.Center
                                            )
                                            Text(
                                                text = orgTour.restaurants.work_schedule,
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = Roboto,
                                                textAlign = TextAlign.Center
                                            )
                                        }
                                        Row {
                                            Text(
                                                text = "Средний чек: ",
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = RobotoB,
                                                textAlign = TextAlign.Center
                                            )
                                            Text(
                                                text = "${orgTour.restaurants.average_check} ${orgTour.restaurants.currency.title}",
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = Roboto,
                                                textAlign = TextAlign.Center
                                            )
                                        }
                                        Row {
                                            Text(
                                                text = "Телефон: ",
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = RobotoB,
                                                textAlign = TextAlign.Center
                                            )
                                            Text(
                                                text = orgTour.restaurants.phone,
                                                color = Color(0xFF510B3C),
                                                fontSize = 14.sp,
                                                fontFamily = Roboto,
                                                textAlign = TextAlign.Center
                                            )
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }
    }
}

@Composable
fun MetroLazy(metro:List<Metro>, viewModel: RouteViewModel = hiltViewModel()){
    LazyColumn(
        modifier = Modifier.fillMaxHeight(0.7f),
        verticalArrangement = Arrangement.Center
    ) {
        items(
            metro,
            key = { metro -> metro.id },
        ) { metroItem ->
            //Не работает условие
            if(metroItem.photo == ""){
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .fillMaxHeight()
                        .background(Color.Red)
                        .height(200.dp),
                    contentAlignment = Alignment.Center
                ) {
                    Text(text = "kjhdwhjdwjjdw", fontSize = 50.sp)
                }
            }
            else{
                val imageState = rememberAsyncImagePainter(
                    model = ImageRequest.Builder(LocalContext.current).data(metroItem.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) {
                    Image(
                        modifier = Modifier
                            .fillMaxHeight()
                            .fillMaxWidth(0.9f)
                            .clip(RoundedCornerShape(5.dp)),
                        painter = imageState.painter,
                        contentDescription = "",
                        contentScale = ContentScale.Crop
                    )
                }
            }
        }
    }
}

@Composable
fun FoodAdviceLazy(navController: NavController, advice: List<Advice>, viewModel: AdviceViewModel = hiltViewModel()) {
    LaunchedEffect(Unit) {
        viewModel.GetAdvice("Питание")
    }
    LazyColumn(
        modifier = Modifier.fillMaxHeight(0.84f)
    ) {
        items(
            advice,
            key = { advice -> advice.id },
        ) { advice ->
            Column(
                modifier = Modifier
                    .fillMaxWidth(0.8f)
                    .padding(top = 10.dp)
                    .height(175.dp)
                    .padding(bottom = 10.dp)
                    .border(
                        width = 4.dp,
                        color = Color(0xFF510B3C),
                        shape = RoundedCornerShape(15.dp)
                    ),
            ) {
                Box(
                    Modifier
                        .fillMaxWidth()
                        .fillMaxHeight()
                        .clip(RoundedCornerShape(15.dp))
                        .background(Color.White)
                ) {
                    Row {
                        Box(
                            Modifier
                                .fillMaxWidth()
                                .fillMaxHeight()
                                .size(210.dp)
                                .padding(top = 10.dp),
                            contentAlignment = Alignment.TopCenter
                        ) {
                            Text(
                                text = advice.title,
                                color = Color(0xFF510B3C),
                                fontSize = 16.sp,
                                fontFamily = RobotoB,
                                textAlign = TextAlign.Center
                            )
                            Box(
                                Modifier
                                    .fillMaxWidth()
                                    .fillMaxHeight()
                                    .padding(top = 10.dp, bottom = 20.dp, start = 5.dp),
                                contentAlignment = Alignment.BottomStart
                            ) {
                                Column(
                                    verticalArrangement = Arrangement.spacedBy(10.dp)
                                ) {
                                    Text(
                                        text = advice.description,
                                        color = Color(0xFF510B3C),
                                        fontSize = 14.sp,
                                        fontFamily = Roboto,
                                        textAlign = TextAlign.Center
                                    )
                                }
                            }
                        }
                    }
                }

            }
        }

    }
}