package com.example.shawarma.screens.products

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
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.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.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
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 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.utils.ScreenPaths
import com.example.shawarma.viewmodels.ProductViewModel
import com.example.shawarma.widgets.MyTextField
import com.example.shawarma.widgets.ShawarmaLogo2

@Composable
fun ProductScreen(navHostController: NavHostController, productId: Int?) {
    Box(
        contentAlignment = Alignment.TopCenter
    ) {
        ProductWidget(navHostController, productId)
        ShawarmaLogo2()
    }
}

@Composable
fun ProductWidget(navHostController: NavHostController, productId: Int?) {
    val title = remember { mutableStateOf(TextFieldValue(""))}
    val price = remember { mutableStateOf(TextFieldValue(""))}
    val oldPrice = remember { mutableStateOf(TextFieldValue(""))}

    val productViewModel: ProductViewModel = hiltViewModel<ProductViewModel>()
    val product = productViewModel.product.observeAsState().value

    if (productViewModel.addingProductState.observeAsState().value == true) {
        navHostController.navigate(ScreenPaths.products.name) {
            popUpTo(ScreenPaths.product.name) {
                inclusive = true
            }
        }
    }

    if (productId != null) {
        productViewModel.getProduct(productId)
    }

    if (product != null) {
        title.value = TextFieldValue(text = product.title)
        price.value = TextFieldValue(text = product.price.toString())
        oldPrice.value = TextFieldValue(text = product.oldPrice.toString())
    }

    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(
            modifier = Modifier
                .padding(top = 60.dp)
                .verticalScroll(rememberScrollState()),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            MyTextField(
                text = title,
                onTextChanged = {title.value = it},
                modifier = Modifier
                    .padding(
                        top = (20).dp
                    )
                    .size(300.dp, (60).dp),
                placeholder = "Наименование",
                singleLine = true
            )
            MyTextField(
                text = price,
                onTextChanged = {price.value = it},
                modifier = Modifier
                    .padding(
                        top = (20).dp
                    )
                    .size(300.dp, (60).dp),
                placeholder = "Цена",
                singleLine = true
            )
            MyTextField(
                text = oldPrice,
                onTextChanged = {oldPrice.value = it},
                modifier = Modifier
                    .padding(
                        top = (20).dp
                    )
                    .size(300.dp, (60).dp),
                placeholder = "Старая цена",
                singleLine = true
            )
            Button(
                modifier = Modifier
                    .padding(top = 80.dp)
                    .size(300.dp, 50.dp),
                colors = ButtonDefaults.buttonColors(
                    backgroundColor = MyLightYellow
                ),
                border = BorderStroke(2.dp, color = MyOrange),
                shape = RoundedCornerShape(20.dp),
                onClick = {
                    if (product == null)
                        productViewModel.addProduct(title.value.text, price.value.text.toInt(), if (!oldPrice.value.text.isNullOrEmpty()) oldPrice.value.text.toInt() else null)
                    else
                        productViewModel.updateProduct(title.value.text, price.value.text.toInt(), if (!oldPrice.value.text.isNullOrEmpty()) oldPrice.value.text.toInt() else null)
                }
            ) {
                Text(
                    text = if (product != null) "Изменить" else "Добавить",
                    fontFamily = NunitoFamily,
                    fontSize = 24.sp,
                    fontWeight = FontWeight.Bold
                )
            }
        }

    }
}