package com.example.exvesta.Screans

import android.util.Log
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.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.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.SheetValue
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
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.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.example.exvesta.domain.utils.Constants
import com.example.exvesta.model.Season_dublicate
import com.example.exvesta.model.Style_dublicate
import com.example.exvesta.model.Type_dublicate
import com.example.exvesta.model.Type_subtype_dublicate
import io.github.jan.supabase.postgrest.from
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import androidx.compose.foundation.lazy.items
@Composable
public  fun Filters(isVisible: Boolean,  onClose: () -> Unit) {
    var styledub by remember { mutableStateOf<List<Style_dublicate>>(listOf()) }
    var subdubtype by remember { mutableStateOf<List<Type_subtype_dublicate>>(listOf()) }
    var dubtype by remember { mutableStateOf<List<Type_dublicate>>(listOf()) }
    var seasondubs by remember { mutableStateOf<List<Season_dublicate>>(listOf()) }
    LaunchedEffect(Unit) {
        withContext(Dispatchers.IO) {
            subdubtype = Constants.supabase.from("Type_subtype_dublicate")
                .select().decodeList<Type_subtype_dublicate>()
            dubtype = Constants.supabase.from("Type_dublicate")
                .select().decodeList<Type_dublicate>()
            seasondubs = Constants.supabase.from("Season_dublicate")
                .select().decodeList<Season_dublicate>()
            styledub = Constants.supabase.from("Style_dublicate")
                .select().decodeList<Style_dublicate>()
            seasondubs.forEach { it ->
                Log.d("ts", it.title_season)
            }
        }
    }
    if (isVisible) {
        Column(
            modifier = Modifier
                .fillMaxHeight()
                .fillMaxWidth()
                .background(
                    Brush.verticalGradient(
                        colors = listOf(
                            Color(0xFFF4F4F4),
                            Color(0xFFE7D2A9),
                            Color(0xFFC1A88B),
                            Color(0xFF6F4A48)
                        )
                    )
                ),
            verticalArrangement = Arrangement.Top,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Spacer(modifier = Modifier.height(50.dp))

            // Кнопка для закрытия фильтров
            Button(
                onClick = { onClose() }, // Закрыть выбор фильтров
                modifier = Modifier.padding(top = 16.dp)
            ) {
                Text("Закрыть выбор фильтров")
            }

            Spacer(modifier = Modifier.height(30.dp))

            // Белый бокс с закругленными краями
            Box(
                modifier = Modifier
                    .fillMaxWidth() // Бокс на всю ширину
                    .padding(start = 16.dp, end = 16.dp, bottom = 10.dp)
                    .background(
                        color = Color.White,
                        shape = RoundedCornerShape(16.dp) // Закругленные края
                    )
                    .padding(16.dp), // Отступы внутри бокса

            ) {
                // Используем Column для размещения текста и кнопки
                Column( horizontalAlignment = Alignment.CenterHorizontally) {
                    // Текст заголовка
                    Text(
                        text = "Фильтры",
                        fontSize = 16.sp,
                        fontWeight = FontWeight.Medium,
                        color = Color(0xFF92A2B0)
                    )
                    Divider(
                        modifier = Modifier.padding(vertical = 8.dp),
                        color = Color.LightGray,
                        thickness = 1.dp
                    )
                    Spacer(modifier = Modifier.height(10.dp)) // Отступ между текстом и кнопкой
                    Box(
                        modifier = Modifier.align(Alignment.Start) // Выравнивание по левому краю
                    ) {
                        Text("Сезон")
                        Spacer(modifier = Modifier.height(10.dp))
                        LazyColumn(

                            modifier = Modifier
                                .fillMaxSize(1f)
                                .padding(10.dp)
                        ) {
                            items(
                                seasondubs,
                                key = { seasondub -> seasondub.id }
                            ) { seasondub ->

                                Text(
                                    "Название: ${seasondub.title_season}",
                                    fontSize = 18.sp,
                                    modifier = Modifier.padding(start = 8.dp),
                                    color = Color(0xFF92A2B0)
                                )
                            }
                        }
                    }
                    // Кнопка "Применить"
                    Button(
                        onClick = { /* Действие при нажатии */ },
                        modifier = Modifier
                            .fillMaxWidth() // Занять всю ширину
                            .height(50.dp)
                            .padding(horizontal = 20.dp)
                            .background(
                                brush = Brush.horizontalGradient(
                                    colors = listOf(
                                        Color(0xFF6F4A48),
                                        Color(0xFFE7D2A9),
                                    )
                                ),
                                shape = ButtonDefaults.shape
                            ),
                        colors = ButtonDefaults.buttonColors(containerColor = Color.Transparent)
                    ) {
                        Text(
                            text = "Применить",
                            fontSize = 16.sp,
                            fontWeight = FontWeight.Medium
                        )
                    }
                }
            }
        }
    }
}