package com.example.lectionsupabase.view.mainActivity.components

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
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.Spacer
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.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
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.layout.ContentScale
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
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.navigation.NavHostController
import coil.compose.rememberImagePainter
import com.example.lectionsupabase.R
import com.example.lectionsupabase.domain.utils.Constants
import com.example.lectionsupabase.model.Admin
import com.example.lectionsupabase.model.Movie
import io.github.jan.supabase.postgrest.from

@Composable
fun Cinema(navHost: NavHostController) {
    var movies by remember { mutableStateOf<List<Movie>>(listOf()) }
    var loadingMovie by remember { mutableStateOf(true) }
    var loadingUsers by remember { mutableStateOf(true) }
    var users by remember { mutableStateOf<List<Admin>>(listOf()) }
    val montserratSemiBold = FontFamily(Font(R.font.mon_semibold))
    val montserratExtraBold = FontFamily(Font(R.font.mon_extrabold))
    val montserratRegular = FontFamily(Font(R.font.mon_regular))
    val montserratMedium = FontFamily(Font(R.font.mon_medium))
    val montserratLigth = FontFamily(Font(R.font.mon_light))

    LaunchedEffect(Unit) {
        try {
            movies = Constants.supabase.from("Movie")
                .select()
                .decodeList<Movie>()
            loadingMovie = false
        } catch (e: Exception) {
            println("Error loading movies: ${e.message}")
            loadingMovie = false
        }
    }

    LaunchedEffect(Unit) {
        try {
            users = Constants.supabase.from("Admin")
                .select()
                .decodeList<Admin>()
            loadingUsers = false
        } catch (e: Exception) {
            println("Error loading users: ${e.message}")
            loadingUsers = false
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {

        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 16.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            // Spacer для создания пространства между текстом и иконкой
            Spacer(modifier = Modifier.weight(1f))

            Text(
                text = "Киноафиша",
                style = MaterialTheme.typography.headlineMedium,
                fontWeight = FontWeight.Bold,
                color = MaterialTheme.colorScheme.onBackground,
                textAlign = TextAlign.Center,
                fontSize = 20.sp,
                fontFamily = montserratSemiBold
            )

            // Стрелка с отступом
            IconButton(
                onClick = { navHost.navigate("Concerts") },
                modifier = Modifier.padding(start = 8.dp) // Отступ слева для стрелки
            ) {
                Icon(
                    imageVector = Icons.Default.ArrowForward,
                    contentDescription = "Перейти к киноафише",
                    tint = Color.Black
                )
            }
        }


        // Загрузка ресторанов
        if (loadingMovie) {
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ) {
                CircularProgressIndicator(modifier = Modifier.size(72.dp))
            }
        } else {
            // Список ресторанов
            LazyColumn(
                modifier = Modifier
                    .fillMaxHeight() // Оставляем немного места для кнопки
                    .padding(bottom = 16.dp),
                verticalArrangement = Arrangement.spacedBy(16.dp)
            ) {
                items(movies, key = { movie -> movie.id }) { movie ->
                    Card(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(8.dp),
                        elevation = CardDefaults.cardElevation(0.dp),
                        shape = RoundedCornerShape(20.dp),
                        colors = CardDefaults.cardColors(containerColor = Color.White),
                        border = BorderStroke(0.5.dp, Color.LightGray) // Светло-серая окантовка
                    ) {
                        Column(modifier = Modifier.fillMaxWidth()) {
                            val painter = rememberImagePainter(
                                data = movie.picture,
                                builder = {
                                    crossfade(true)
                                }
                            )

                            Image(
                                painter = painter,
                                contentDescription = "Фото ресторана",
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .height(190.dp),
                                contentScale = ContentScale.Crop
                            )
                            Spacer(modifier = Modifier.height(10.dp))
                            // Информация о ресторане
                            Text(
                                text = "Название: ${movie.name}",
                                style = MaterialTheme.typography.titleMedium,
                                fontSize = 14.sp,
                                modifier = Modifier.padding(horizontal = 10.dp)
                                    .padding(start = 3.dp),
                                fontFamily = montserratRegular
                            )
                            Text(
                                text = "Жанр: ${movie.genre}",
                                style = MaterialTheme.typography.titleMedium,
                                fontSize = 14.sp,
                                modifier = Modifier.padding(horizontal = 10.dp)
                                    .padding(start = 3.dp),
                                fontFamily = montserratRegular
                            )
                            Text(
                                text = "Цена: ${movie.price}",
                                style = MaterialTheme.typography.titleMedium,
                                fontSize = 14.sp,
                                modifier = Modifier.padding(horizontal = 10.dp)
                                    .padding(start = 3.dp),
                                fontFamily = montserratRegular,

                                )
                            Text(
                                text = "Дата: ${movie.date}",
                                style = MaterialTheme.typography.titleMedium,
                                fontSize = 14.sp,
                                modifier = Modifier.padding(horizontal = 10.dp)
                                    .padding(start = 3.dp),
                                fontFamily = montserratRegular,

                                )
                            Text(
                                text = "Посмотреть на карте",
                                style = MaterialTheme.typography.titleMedium,
                                fontWeight = FontWeight.SemiBold,
                                fontSize = 14.sp,
                                modifier = Modifier.padding(horizontal = 10.dp)
                                    .padding(start = 3.dp),
                                fontFamily = montserratMedium,
                                color = Color(0xFF72C3F1)
                            )
                            Spacer(modifier = Modifier.height(10.dp))
                        }
                    }
                }
            }
        }
    }
}