package com.example.shawarma.screens.orders

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.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.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
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 com.example.shawarma.R
import com.example.shawarma.data.models.OrderStatus
import com.example.shawarma.data.models.OrderWithProducts
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.viewmodels.OrdersViewModel
import com.example.shawarma.widgets.ShawarmaLogo2
import java.text.SimpleDateFormat

@Composable
fun OrdersScreen() {
    Box(
        contentAlignment = Alignment.Center
    ) {
        ShawarmaLogo2()
        OrdersList()
    }
}

@Composable
fun OrdersList(){
    val ordersViewModel = hiltViewModel<OrdersViewModel>()

    val preparingOrders = ordersViewModel.preparingOrders.observeAsState().value
    val preparedOrders = ordersViewModel.preparedOrders.observeAsState().value
    val processedOrders = ordersViewModel.processedOrders.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(250.dp)
            ) {
                if (preparingOrders != null) {
                    if (preparingOrders.isNotEmpty()) {
                        items(preparingOrders.size) { index ->
                            PreparingItem(preparingOrders[index])
                        }
                    }
                }
            }
            Text(
                text = "Готово:",
                fontFamily = NunitoFamily,
                fontSize = 20.sp,
                modifier = Modifier.padding(top = 15.dp),
                fontWeight = FontWeight.Bold
            )
            LazyColumn(
                modifier = Modifier
                    .width(340.dp)
                    .height(250.dp)
            ) {
                if (preparedOrders != null) {
                    if (preparedOrders.isNotEmpty()) {
                        items(preparedOrders.size) { index ->
                            PreparedItem(preparedOrders[index])
                        }
                    }
                }
            }
            Text(
                text = "Выдано:",
                fontFamily = NunitoFamily,
                fontSize = 20.sp,
                modifier = Modifier.padding(top = 15.dp),
                fontWeight = FontWeight.Bold
            )
            LazyColumn(
                modifier = Modifier
                    .width(340.dp)
                    .height(250.dp)
            ) {
                if (processedOrders != null) {
                    if (processedOrders.isNotEmpty()) {
                        items(processedOrders.size) {index ->
                            ProcessedItem(processedOrders[index])
                        }
                    }
                }
            }
            Spacer(modifier = Modifier.height(70.dp))
        }


    }
}

@Composable
fun PreparingItem(order : OrderWithProducts){
    val ordersViewModel = hiltViewModel<OrdersViewModel>()

    Card(
        border = BorderStroke(width = 2.dp, color = MyOrange),
        shape = RoundedCornerShape(size = 20.dp),
        backgroundColor = Color.White,
        modifier = Modifier.size(340.dp, 200.dp)
    ){
        Column(

        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween,
                modifier = Modifier
                    .padding(20.dp)
                    .size(340.dp, 60.dp)
            ){
                Column(
                    modifier = Modifier.fillMaxWidth(0.5f)
                ){
                    val localDateFormat = SimpleDateFormat("HH:mm")
                    val time = localDateFormat.format(order.order.date)
                    Text(
                        text = time,
                        fontFamily = NunitoFamily,
                        fontSize = 20.sp,
                        fontWeight = FontWeight.Bold
                    )

                }
                Button(
                    colors = ButtonDefaults.buttonColors(
                        backgroundColor = MyLightYellow
                    ),
                    shape = RoundedCornerShape(size = 10.dp),
                    modifier = Modifier
                        .size(170.dp, 80.dp)
                        .fillMaxSize(),
                    onClick = {
                        ordersViewModel.changeOrderStatus(order, OrderStatus.Готово.name)
                    }
                ) {
                    Text(
                        text = "Готово!",
                        fontFamily = NunitoFamily,
                        fontSize = 14.sp,
                        fontWeight = FontWeight.Bold
                    )
                }

            }

            LazyColumn(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 20.dp)
            ){
                items(order.orderWithProducts.size) {index ->
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween,

                    ) {
                        Text(
                            text = order.orderWithProducts[index].product.title,
                            fontFamily = NunitoFamily,
                            fontSize = 20.sp,
                            fontWeight = FontWeight.Bold
                        )
                        Text(
                            text = "x" + order.orderWithProducts[index].orderProductModel.quantity,
                            fontFamily = NunitoFamily,
                            fontSize = 20.sp,
                            fontWeight = FontWeight.Bold
                        )
                    }
                }
            }
        }
    }

    Spacer(modifier = Modifier.height(20.dp))
}

@Composable
fun PreparedItem(order : OrderWithProducts){
    val ordersViewModel = hiltViewModel<OrdersViewModel>()

    Card(
        border = BorderStroke(width = 2.dp, color = MyOrange),
        shape = RoundedCornerShape(size = 20.dp),
        backgroundColor = Color.White,
        modifier = Modifier.size(340.dp, 200.dp)
    ){
        Column(

        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween,
                modifier = Modifier
                    .padding(20.dp)
                    .size(340.dp, 60.dp)
            ){
                Column(
                    modifier = Modifier.fillMaxWidth(0.5f)
                ){
                    val localDateFormat = SimpleDateFormat("HH:mm")
                    val time = localDateFormat.format(order.order.date)
                    Text(
                        text = time,
                        fontFamily = NunitoFamily,
                        fontSize = 20.sp,
                        fontWeight = FontWeight.Bold
                    )

                }
                Button(
                    colors = ButtonDefaults.buttonColors(
                        backgroundColor = MyLightYellow
                    ),
                    shape = RoundedCornerShape(size = 10.dp),
                    modifier = Modifier
                        .size(170.dp, 80.dp)
                        .fillMaxSize(),
                    onClick = {
                        ordersViewModel.changeOrderStatus(order, OrderStatus.Выдано.name)
                    }
                ) {
                    Text(
                        text = "Выдано!",
                        fontFamily = NunitoFamily,
                        fontSize = 14.sp,
                        fontWeight = FontWeight.Bold
                    )
                }

            }

            LazyColumn(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 20.dp)
            ){
                items(order.orderWithProducts.size) {index ->
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween,

                        ) {
                        Text(
                            text = order.orderWithProducts[index].product.title,
                            fontFamily = NunitoFamily,
                            fontSize = 20.sp,
                            fontWeight = FontWeight.Bold
                        )
                        Text(
                            text = "x" + order.orderWithProducts[index].orderProductModel.quantity,
                            fontFamily = NunitoFamily,
                            fontSize = 20.sp,
                            fontWeight = FontWeight.Bold
                        )
                    }
                }
            }
        }
    }

    Spacer(modifier = Modifier.height(20.dp))
}

@Composable
fun ProcessedItem(order : OrderWithProducts){
    val ordersViewModel = hiltViewModel<OrdersViewModel>()

    Card(
        border = BorderStroke(width = 2.dp, color = MyOrange),
        shape = RoundedCornerShape(size = 20.dp),
        backgroundColor = Color.White,
        modifier = Modifier.size(340.dp, 200.dp)
    ){
        Column(

        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween,
                modifier = Modifier
                    .padding(20.dp)
                    .size(340.dp, 60.dp)
            ){
                Column(
                    modifier = Modifier.fillMaxWidth(0.3f)
                ){
                    val localDateFormat = SimpleDateFormat("HH:mm")
                    val time = localDateFormat.format(order.order.date)
                    Text(
                        text = time,
                        fontFamily = NunitoFamily,
                        fontSize = 20.sp,
                        fontWeight = FontWeight.Bold
                    )

                }
                Column(
                    modifier = Modifier.fillMaxWidth(0.3f)
                ){
                    Text(
                        text = order.order.date.toString(),
                        fontFamily = NunitoFamily,
                        fontSize = 16.sp,
                        fontWeight = FontWeight.Bold
                    )
                }
                Button(
                    modifier = Modifier
                        .size(80.dp)
                    ,
                    colors = ButtonDefaults.buttonColors(MyLightRed),
                    shape = RoundedCornerShape(20.dp),
                    onClick = {
                        ordersViewModel.deleteOrder(order)
                    }
                ) {
                    Icon(
                        painter = painterResource(id = R.drawable.trash),
                        contentDescription = "trash",
                        modifier = Modifier.size(80.dp)
                    )
                }

            }

            LazyColumn(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 20.dp)
            ){
                items(order.orderWithProducts.size) {index ->
                    Row(
                        modifier = Modifier.fillMaxWidth(),
                        horizontalArrangement = Arrangement.SpaceBetween,

                        ) {
                        Text(
                            text = order.orderWithProducts[index].product.title,
                            fontFamily = NunitoFamily,
                            fontSize = 20.sp,
                            fontWeight = FontWeight.Bold
                        )
                        Text(
                            text = "x" + order.orderWithProducts[index].orderProductModel.quantity,
                            fontFamily = NunitoFamily,
                            fontSize = 20.sp,
                            fontWeight = FontWeight.Bold
                        )
                    }
                }
            }
        }
    }

    Spacer(modifier = Modifier.height(20.dp))
}