package com.example.shawarma.screens.cart

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.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.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
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.runtime.livedata.observeAsState
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.platform.LocalContext
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 com.example.shawarma.data.models.OrderWithProducts
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.NunitoFamily
import com.example.shawarma.viewmodels.CartViewModel
import com.example.shawarma.widgets.ShawarmaLogo2

@Composable
fun CartScreen() {
    Box(
        contentAlignment = Alignment.TopCenter
    ) {
        CartWidget()
        ShawarmaLogo2()
    }
}

@Composable
fun CartWidget(){
    val preferencesManager = PreferencesManager(LocalContext.current)

    val cartViewModel: CartViewModel = hiltViewModel<CartViewModel>()
    cartViewModel.getOrders(preferencesManager.getData("user_id", "null"))

    var unpaidOrder: OrderWithProducts? = cartViewModel.unpaidOrder.observeAsState().value
    val preparingOrders = cartViewModel.paidOrders.observeAsState().value

    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)
        )
        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            modifier = Modifier
                .fillMaxWidth()
                .padding(top = 55.dp)
                .verticalScroll(rememberScrollState())
        ) {
            Text(
                text = "Оплачено:",
                fontFamily = NunitoFamily,
                fontSize = 20.sp,
                modifier = Modifier.padding(top = 15.dp),
                fontWeight = FontWeight.Bold
            )
            LazyColumn(
                modifier = Modifier
                    .width(340.dp)
                    .height(200.dp)
            ) {
                if (preparingOrders != null) {
                    items(preparingOrders.size) { index ->
                        PaidItem(preparingOrders[index])
                    }
                }
            }
            Text(
                text = "Ожидает оплаты:",
                fontFamily = NunitoFamily,
                fontSize = 20.sp,
                modifier = Modifier.padding(top = 15.dp),
                fontWeight = FontWeight.Bold
            )
            if (unpaidOrder != null) {
                CartItem(order = unpaidOrder)
            }
            Spacer(modifier = Modifier.height(20.dp))
            Button(
                colors = ButtonDefaults.buttonColors(
                    backgroundColor = Color(0xFF91FF87)
                ),
                shape = RoundedCornerShape(20.dp),
                border = BorderStroke(2.dp, Color(0x66000000)),
                modifier = Modifier.size(240.dp, 60.dp),
                onClick = {
                    if (unpaidOrder != null) {
                        cartViewModel.payForOrder(unpaidOrder)
                    }
                }
            ) {
                Text(
                    "Оплатить",
                    fontSize = 20.sp,
                    fontFamily = NunitoFamily,
                    fontWeight = FontWeight(700),
                )
            }
            Spacer(modifier = Modifier.height(70.dp))
        }
    }
}

@Composable
fun PaidItem(order : OrderWithProducts) {
    Card(
        border = BorderStroke(width = 2.dp, color = MyOrange),
        shape = RoundedCornerShape(size = 20.dp),
        backgroundColor = Color.White,
        modifier = Modifier.size(340.dp, 150.dp)
    )  {
        Row(
            horizontalArrangement = Arrangement.SpaceBetween,
            modifier = Modifier
                .fillMaxWidth()
                .padding(start = 20.dp, end = 20.dp)
        ){
            Text(
                text = "Статус: ",
                fontFamily = NunitoFamily,
                fontSize = 20.sp,
                modifier = Modifier.padding(top = 15.dp),
                fontWeight = FontWeight.Bold,
                color = Color.Gray
            )
            Text(
                text = order.order.status,
                fontFamily = NunitoFamily,
                fontSize = 20.sp,
                modifier = Modifier.padding(top = 15.dp),
                fontWeight = FontWeight.Bold,
                color = Color.Gray
            )
        }
        LazyColumn(
            modifier = Modifier.padding(top = 40.dp)
        ) {
            items(order.orderWithProducts.size) { index ->
                if (order.orderWithProducts.isNotEmpty()) {
                    Row(
                        horizontalArrangement = Arrangement.SpaceBetween,
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(horizontal = 20.dp)
                    ) {
                        Text(
                            text = order.orderWithProducts[index].product.title,
                            fontFamily = NunitoFamily,
                            fontSize = 20.sp,
                            modifier = Modifier.padding(top = 15.dp),
                            fontWeight = FontWeight.Bold
                        )
                        Text(
                            text = "x" + order.orderWithProducts[index].orderProductModel.quantity,
                            fontFamily = NunitoFamily,
                            fontSize = 20.sp,
                            modifier = Modifier.padding(top = 15.dp),
                            fontWeight = FontWeight.Bold
                        )
                        Text(
                            text = (order.orderWithProducts[index].orderProductModel.quantity * order.orderWithProducts[index].product.price).toString() + " руб.",
                            fontFamily = NunitoFamily,
                            fontSize = 20.sp,
                            modifier = Modifier.padding(top = 15.dp),
                            fontWeight = FontWeight.Bold
                        )
                    }
                }
            }
        }
    }
    Spacer(modifier = Modifier.height(20.dp))
}

@Composable
fun CartItem(order : OrderWithProducts) {
    val cartViewModel: CartViewModel = hiltViewModel<CartViewModel>()

    Card(
        border = BorderStroke(width = 2.dp, color = MyOrange),
        shape = RoundedCornerShape(size = 20.dp),
        backgroundColor = Color.White,
        modifier = Modifier.size(340.dp, 180.dp)
    ) {
        LazyColumn (
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            items(order.orderWithProducts.size) {index ->
                var count = remember { mutableStateOf(0)}
                count.value = order.orderWithProducts[index].orderProductModel.quantity
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    horizontalArrangement = Arrangement.SpaceBetween,
                    modifier = Modifier
                        .padding(20.dp)
                        .size(340.dp, 80.dp)
                ) {
                    Column(
                        modifier = Modifier.fillMaxWidth()
                    ) {
                        Row(
                            horizontalArrangement = Arrangement.SpaceBetween,
                            modifier = Modifier.fillMaxWidth()
                        ) {
                            Text(
                                text = order.orderWithProducts[index].product.title,
                                fontFamily = NunitoFamily,
                                fontSize = 20.sp,
                                fontWeight = FontWeight.Bold
                            )
                            Text(
                                text = (order.orderWithProducts[index].orderProductModel.quantity * order.orderWithProducts[index].product.price).toString() + " руб.",
                                fontFamily = NunitoFamily,
                                fontSize = 18.sp,
                                fontWeight = FontWeight.Bold
                            )
                        }
                        Row(
                            horizontalArrangement = Arrangement.SpaceBetween,
                            verticalAlignment = Alignment.CenterVertically,
                            modifier = Modifier
                                .fillMaxWidth()
                        ) {
                            Button(
                                colors = ButtonDefaults.buttonColors(
                                    backgroundColor = MyLightYellow
                                ),
                                shape = RoundedCornerShape(size = 20.dp),
                                modifier = Modifier
                                    .fillMaxHeight()
                                    .weight(1f)
                                    .fillMaxSize(0.5f),
                                onClick = {
                                    cartViewModel.removeProductFromOrder(order, index)
                                    count.value -= 1
                                }
                            ) {
                                Text(
                                    text = "-",
                                    fontSize = 18.sp,
                                    fontWeight = FontWeight.Bold
                                )
                            }
                            Spacer(modifier = Modifier.fillMaxWidth(0.2f))
                            Text(
                                text = "x" + count.value + " ",
                                fontFamily = NunitoFamily,
                                fontSize = 20.sp,
                                fontWeight = FontWeight.Bold
                            )
                            Spacer(modifier = Modifier.fillMaxWidth(0.2f))
                            Button(
                                colors = ButtonDefaults.buttonColors(
                                    backgroundColor = MyLightYellow
                                ),
                                shape = RoundedCornerShape(size = 20.dp),
                                modifier = Modifier
                                    .fillMaxHeight()
                                    .weight(1f)
                                    .fillMaxSize(0.5f),
                                onClick = {
                                    cartViewModel.addProductToOrder(order, index)
                                    count.value += 1
                                }
                            ) {
                                Text(
                                    text = "+",
                                    fontSize = 18.sp,
                                    fontWeight = FontWeight.Bold
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}