package com.example.shawarma.screens.home

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
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.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.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 androidx.hilt.navigation.compose.hiltViewModel
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.data.sharedpref.PreferencesManager
import com.example.shawarma.ui.theme.MarckFamily
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.MyPriceBackground
import com.example.shawarma.ui.theme.NunitoFamily
import com.example.shawarma.viewmodels.HomeViewModel
import com.example.shawarma.widgets.ShawarmaLogo2

@Composable
fun HomeScreen() {
    Box(
        contentAlignment = Alignment.TopCenter
    ) {
        HomeList()
        ShawarmaLogo2()
    }
}

@Composable
fun HomeList(){
    val homeViewModel: HomeViewModel = hiltViewModel<HomeViewModel>()

    val productsListUiState = homeViewModel.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)
        )
        LazyColumn(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier.padding(top = 80.dp)
        )
        {
            items(
                count = productsListUiState.itemCount,
                key = productsListUiState.itemKey()
            ) { index ->
                if (index % 2 == 0 && index == productsListUiState.itemCount - 1) {
                    Row(
                        horizontalArrangement = Arrangement.SpaceAround,
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(top = 10.dp)
                    ) {
                        ProductCard(productsListUiState[index]!!)
                    }
                } else {
                    if (index % 2 != 1) {
                        Row(
                            horizontalArrangement = Arrangement.SpaceAround,
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(top = 10.dp)
                        ) {
                            ProductCard(productsListUiState[index]!!)
                            ProductCard(productsListUiState[index + 1]!!)
                        }
                    }
                }
                if (index == productsListUiState.itemCount - 1) {
                    Spacer(modifier = Modifier.height(70.dp))
                }
            }
        }
    }
}

@Composable
fun ProductCard(product: ProductModel){
    val homeViewModel: HomeViewModel = hiltViewModel<HomeViewModel>()
    val preferencesManager = PreferencesManager(LocalContext.current)

    Card(
        shape = RoundedCornerShape(20.dp),
        backgroundColor = Color.White,
        border = BorderStroke(2.dp, MyOrange),
        modifier = Modifier
            .size(160.dp)
    ) {
            Column(
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Text(
                    text = product.title,
                    fontFamily = NunitoFamily,
                    fontSize =  20.sp,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier.padding(top = 10.dp)
                )
                Image(
                    painter = painterResource(id = R.drawable.shawarma2),
                    contentDescription = "Shawarma",
                    Modifier.size(135.dp, 70.dp)
                )
                Spacer(modifier = Modifier.height(16.dp))
                Card(
                    shape = RoundedCornerShape(20.dp),
                    backgroundColor = MyPriceBackground,
                    border = BorderStroke(2.dp, MyOrange),
                    modifier = Modifier
                        .size(160.dp, 36.dp)
                ) {
                    Row(
                        horizontalArrangement = Arrangement.SpaceBetween
                    ){
                        Text(
                            text = product.price.toString() + " руб.",
                            fontFamily = NunitoFamily,
                            fontSize =  16.sp,
                            fontWeight = FontWeight.ExtraBold,
                            modifier = Modifier.padding(start = 12.dp, top = 6.dp)
                        )
                        Button(
                            onClick = {
                                product.id?.let { homeViewModel.addProductToCart(it, preferencesManager.getData("user_id", "null")) }
                            },
                            colors = ButtonDefaults.buttonColors(
                                backgroundColor = MyLightYellow,
                                contentColor = Color.Black
                            ),
                            shape = RoundedCornerShape(20.dp),
                        ) {
                            Text(
                                text = "Купить",
                                fontSize = 14.sp,
                                fontWeight = FontWeight.ExtraBold,
                                fontFamily = NunitoFamily,
                            )
                        }
                    }
                }
            }
    }
}