package com.example.pencilmuse.views.MainActivity.components

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
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.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.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
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.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.TextStyle
import androidx.compose.ui.text.font.FontWeight.Companion.W400
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import coil.compose.AsyncImagePainter
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import coil.size.Size
import com.example.pencilmuse.R
import com.example.pencilmuse.domain.utils.Constants
import com.example.pencilmuse.models.Articles
import com.example.pencilmuse.models.Catalogs
import io.github.jan.supabase.postgrest.from
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Composable
fun Catalogs(navHost: NavHostController)
{
    val search = remember { mutableStateOf("") }

    var catalogs by remember { mutableStateOf<List<Catalogs>>(listOf()) }

    LaunchedEffect(Unit) {
        withContext(Dispatchers.IO){
            catalogs = Constants.supabase.from("Catalogs").select().decodeList<Catalogs>()
            catalogs.forEach{ it-> Log.d("C",it.name_catalog)}
        }
    }
    Column()
    {
        Row(
            horizontalArrangement = Arrangement.SpaceEvenly)
        {
            OutlinedTextField(
                // modifier = Modifier.background(Color(0xFFECECEC)),
                search.value,
                {search.value = it},
                textStyle = TextStyle(fontSize =  14.sp),
                shape = RoundedCornerShape(7.dp),
                placeholder = { Text("поиск") }
            )

            IconButton(onClick = { }) {

                Icon(
                    modifier = Modifier.size(30.dp, 30.dp),
                    painter = painterResource(id = R.drawable.search),
                    contentDescription = "поиск"
                )


            }
        }


        LazyColumn(
            modifier = Modifier
                .fillMaxSize(1f)
                .padding(0.dp, 0.dp, 0.dp, 100.dp),
            verticalArrangement = Arrangement.SpaceBetween

        )
        {
            items(
                catalogs,
                key = { articles -> articles.id_catalog },
            )
            { articles ->
                val imageState = rememberAsyncImagePainter(
                    model = ImageRequest.Builder(LocalContext.current).data(articles.image)
                        .size(Size.ORIGINAL)
                        .build()
                ).state
                if (imageState is AsyncImagePainter.State.Error) {
                    Image(
                        modifier = Modifier
                            .width(90.dp)
                            .height(120.dp)

                            .clip(
                                RoundedCornerShape(10.dp)
                            ),
                        painter = painterResource(id = R.drawable.catalog),
                        contentDescription = "",
                        contentScale = ContentScale.Crop
                    )
                }
                Row(
                    horizontalArrangement = Arrangement.SpaceEvenly,
                    modifier = Modifier.fillMaxWidth().border(1.dp, Color.LightGray).padding(30.dp)

                ) {
                    if (imageState is AsyncImagePainter.State.Success) {
                        Image(
                            modifier = Modifier
                                .width(90.dp)
                                .height(120.dp)

                                .clip(
                                    RoundedCornerShape(10.dp)
                                ),
                            painter = imageState.painter,
                            contentDescription = "",
                            contentScale = ContentScale.Crop
                        )
                    }
                    Text(
                        articles.name_catalog,
                        fontSize = 16.sp,
                        fontWeight = W400,
                        modifier = Modifier.padding(8.dp, 0.dp, 0.dp, 0.dp)
                    )
                }
            }
        }







    }
}