package com.example.tasks.screens

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
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.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.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.Card
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
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.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import coil.compose.AsyncImagePainter
import coil.compose.rememberAsyncImagePainter
import coil.request.ImageRequest
import com.example.tasks.navigation.Screen
import com.example.tasks.viewModels.CardViewModel
import com.example.tasks.viewModels.DatabaseViewModel

@Composable
fun CatalogSellers(viewModel: DatabaseViewModel,navController: NavController,cardViewModel: CardViewModel) {
    val flag = remember { mutableStateOf(true) }
    val products by viewModel.products
    if (flag.value) {
        viewModel.sellerProducts()
        flag.value = false
    }

    if (products.isNotEmpty()) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(Color(0xFF951DDD))
                .clip(RoundedCornerShape(10.dp))
                .padding(bottom = 10.dp),
            contentAlignment = Alignment.BottomCenter

        ) {
            Button(
                modifier = Modifier
                    .width(width = 500.dp)
                    .height(height = 56.dp),
                onClick = {
                    navController.navigate(Screen.AddProduct.route)
                },
                shape = RoundedCornerShape(size = 10.dp),
                colors = ButtonColors(
                    contentColor = Color(0xFF951DDD),
                    containerColor = Color.White,
                    disabledContentColor = Color.LightGray,
                    disabledContainerColor = Color.White
                )
            ) {
                Text(
                    text = "Создать карточку",
                    fontSize = 20.sp,
                    fontWeight = FontWeight.W900,
                )
            }
        }



        LazyColumn(Modifier.fillMaxWidth().fillMaxHeight(0.9f).background(Color(0xFF951DDD))) {
            items(products) { product ->
                val avatar = rememberAsyncImagePainter(
                    model = ImageRequest.Builder(LocalContext.current).data(product.image)
                        .size(200, 200).build()
                ).state
                Card(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(20.dp)
                        .height(350.dp)
                        .clickable { cardViewModel.manualEx(product)
                        navController.navigate(Screen.Card.route)}

                ) {
                    Column(
                        Modifier.fillMaxSize(),
                        horizontalAlignment = Alignment.CenterHorizontally,
                        verticalArrangement = Arrangement.Center
                    ) {
                        if (avatar is AsyncImagePainter.State.Success) {
                            Box(
                                modifier = Modifier
                                    .width(200.dp)
                                    .height(200.dp),
                                contentAlignment = Alignment.Center
                            ) {
                                Image(
                                    modifier = Modifier
                                        .fillMaxWidth()
                                        .height(200.dp),
                                    painter = avatar.painter,
                                    contentDescription = "",
                                    contentScale = ContentScale.Crop
                                )
                            }
                        }
                        if (avatar is AsyncImagePainter.State.Loading) {
                            Box(
                                modifier = Modifier
                                    .fillMaxWidth()
                                    .height(250.dp),
                                contentAlignment = Alignment.Center
                            ) {
                                CircularProgressIndicator()
                            }
                        }

                        Text(product.title, fontSize = 20.sp, fontWeight = FontWeight.Black)
                        Text("${product.price} рублей", fontSize = 20.sp)
                        if (!product.description.isNullOrEmpty()) {
                            Text(product.description, fontSize = 14.sp)
                        }


                    }


                }

            }
        }
    }
}