package com.example.shawarma.screens.products

import androidx.compose.foundation.BorderStroke
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.Row
import androidx.compose.foundation.layout.Spacer
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.magnifier
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.Card
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateListOf
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.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import com.example.shawarma.R
import com.example.shawarma.data.db.AppDatabase
import com.example.shawarma.data.models.ProductModel
import com.example.shawarma.screens.home.HomeList
import com.example.shawarma.screens.home.ProductCard
import com.example.shawarma.ui.theme.MarckFamily
import com.example.shawarma.ui.theme.MyLightRed
import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.MyMainBackground
import com.example.shawarma.ui.theme.MyOrange
import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.widgets.ShawarmaLogo2
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Composable
fun ProductsScreen() {
    Box(
        contentAlignment = Alignment.TopCenter
    ) {
        ProductsList()
        ShawarmaLogo2()
    }
}



@Composable
fun ProductsList(){
    val context = LocalContext.current
    val products = remember { mutableStateListOf<ProductModel>() }
    LaunchedEffect(Unit) {
        withContext(Dispatchers.IO) {
            AppDatabase.getInstance(context).productDao().getItems().collect { data ->
                products.clear()
                products.addAll(data)
            }
        }
    }

    Box(
        modifier = Modifier
            .clip(shape = RoundedCornerShape(30.dp))
            .padding(top = 100.dp)
            .fillMaxSize()
            .background(color = MyMainBackground)
            .zIndex(2f),

        contentAlignment = Alignment.TopCenter
    ){
        Text(
            text = "Товары",
            fontFamily = MarckFamily,
            fontSize = 40.sp,
            modifier = Modifier.padding(top = 15.dp)
        )
        Button(
            modifier = Modifier
                .padding(top = 80.dp)
                .size(340.dp, 60.dp),
            colors = ButtonDefaults.buttonColors(
                backgroundColor = MyLightYellow
            ),
            border = BorderStroke(2.dp, color = MyOrange),
            shape = RoundedCornerShape(20.dp),
            onClick = { /*TODO*/ }
        ) {
            Text(
                text = "Добавить товар",
                fontFamily = NunitoFamily,
                fontSize = 24.sp,
                fontWeight = FontWeight.Bold
            )
        }
        LazyColumn(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.padding(top = 160.dp)
        )
        {
            items(products.size) { index ->
                ProductItem(products[index])
                Spacer(modifier = Modifier.height(20.dp))
                if (index == products.size - 1) {
                    Spacer(modifier = Modifier.height(70.dp))
                }
            }
        }

    }
}

@Composable
fun ProductItem(product: ProductModel){
    Card(
        border = BorderStroke(width = 2.dp, color = MyOrange),
        shape = RoundedCornerShape(size = 20.dp),
        backgroundColor = Color.White,
        modifier = Modifier.size(340.dp, 100.dp)
    ) {
        Row(
            modifier = Modifier
                .padding(20.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceAround
        ) {
            Column(
                modifier = Modifier.fillMaxWidth(0.5f)
            ) {
                Text(
                    text = product.title,
                    fontFamily = NunitoFamily,
                    fontSize = 20.sp,
                    fontWeight = FontWeight.Bold
                )
                Text(
                    text = product.price.toString() + " руб.",
                    fontFamily = NunitoFamily,
                    fontSize = 20.sp,
                    fontWeight = FontWeight.Bold
                )
            }

            Button(
                modifier = Modifier.size(60.dp),
                shape = RoundedCornerShape(10.dp),
                colors = ButtonDefaults.buttonColors(
                    backgroundColor = MyLightYellow
                ),
                onClick = { /*TODO*/ }
            ) {
                Icon(
                    painter = painterResource(id = R.drawable.pen_icon),
                    contentDescription = "pen",
                    modifier = Modifier.fillMaxSize()
                )
            }
            Button(
                modifier = Modifier.size(60.dp),
                shape = RoundedCornerShape(10.dp),
                colors = ButtonDefaults.buttonColors(
                    backgroundColor = MyLightRed
                ),
                onClick = { /*TODO*/ }
            ) {
                Icon(
                    painter = painterResource(id = R.drawable.trash),
                    contentDescription = "trash",
                    modifier = Modifier.fillMaxSize()
                )
            }
        }
    }
}
