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.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.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
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 androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemKey
import com.example.shawarma.R
import com.example.shawarma.data.models.ProductModel
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.utils.ScreenPaths
import com.example.shawarma.viewmodels.ProductsViewModel
import com.example.shawarma.widgets.ShawarmaLogo2

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



@Composable
fun ProductsList(navHostController: NavHostController){
    val productsViewModel: ProductsViewModel = hiltViewModel<ProductsViewModel>()

    val products = productsViewModel.productListUiState.collectAsLazyPagingItems()

    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 = {
                navHostController.navigate(ScreenPaths.product.name)
            }
        ) {
            Text(
                text = "Добавить товар",
                fontFamily = NunitoFamily,
                fontSize = 24.sp,
                fontWeight = FontWeight.Bold
            )
        }
        LazyColumn(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.padding(top = 160.dp)
        )
        {
            items(
                products.itemCount,
                key = products.itemKey()
            ) { index ->
                ProductItem(products[index]!!, navHostController)
                Spacer(modifier = Modifier.height(20.dp))
                if (index == products.itemCount - 1) {
                    Spacer(modifier = Modifier.height(70.dp))
                }
            }
        }

    }
}

@Composable
fun ProductItem(product: ProductModel, navHostController: NavHostController){
    val productsViewModel: ProductsViewModel = hiltViewModel<ProductsViewModel>()

    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
                )
                if (product.oldPrice != null) {
                    Text(
                        text = product.price.toString() + " руб.",
                        fontFamily = NunitoFamily,
                        fontSize = 20.sp,
                        fontWeight = FontWeight.Bold,
                        color = Color.Red
                    )
                }
                else {
                    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 = {
                    navHostController.navigate(
                        route = ScreenPaths.product.name + "?productId=" + product.id.toString()
                    )
                }
            ) {
                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 = {
                    productsViewModel.deleteProduct(product)
                }
            ) {
                Icon(
                    painter = painterResource(id = R.drawable.trash),
                    contentDescription = "trash",
                    modifier = Modifier.fillMaxSize()
                )
            }
        }
    }
}
