package com.syaroful.agrilinkvocpro.presentation.screen.detail.component

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.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment.Companion.CenterHorizontally
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.syaroful.agrilinkvocpro.R
import com.syaroful.agrilinkvocpro.core.components.DefaultErrorComponent
import com.syaroful.agrilinkvocpro.core.placeholder.shimmerEffect
import com.syaroful.agrilinkvocpro.core.utils.ResultState
import com.syaroful.agrilinkvocpro.core.utils.extention.getValuesForSensorNpk
import com.syaroful.agrilinkvocpro.data.model.NpkGraphicDataResponse
import com.syaroful.agrilinkvocpro.data.model.SensorDataResponse
import com.syaroful.agrilinkvocpro.presentation.screen.detail.DetailViewModel

@Composable
fun DetailNpkContent(
    modifier: Modifier = Modifier,
    viewModel: DetailViewModel,
    npkDataState: ResultState<NpkGraphicDataResponse?>,
    currentData: SensorDataResponse?,
    sensorId: String,
    isRefreshing: MutableState<Boolean>,
    date: MutableState<String>,
    currentDate: MutableState<String>,
) {
    val selectedSensor = remember { mutableStateOf("Nitrogen") }
    val options = listOf(
        "Nitrogen",
        "Pospor",
        "Kalium",
        "Suhu Tanah",
        "PH Tanah",
        "Kelembapan",
        "Konduktivitas"
    )

    Column(
        modifier = modifier
            .padding(16.dp)
            .verticalScroll(rememberScrollState())
    ) {
        when (npkDataState) {
            is ResultState.Loading -> {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(200.dp)
                        .shimmerEffect()
                )
            }

            is ResultState.Error -> {
                isRefreshing.value = false
                DefaultErrorComponent(
                    modifier = Modifier.padding(vertical = 20.dp),
                    label = "Oops!",
                    message = npkDataState.message,
                    painter = painterResource(id = R.drawable.mascot_confused)
                )
            }

            is ResultState.Success -> {
                isRefreshing.value = false
                val dataList = npkDataState.data?.data?.get(sensorId).orEmpty()

                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    DynamicBottomSheet(
                        options = options
                    ) { selected ->
                        selectedSensor.value = selected
                    }
                    DatePickerComponent() {
                        date.value = it
                        viewModel.fetchNpkData(date = date.value, sensor = sensorId)
                        currentDate.value = date.value
                    }
                }

                Spacer(modifier = Modifier.height(16.dp))

                val hours = dataList.mapNotNull { it.hour?.toInt() }
                val values = getValuesForSensorNpk(selectedSensor.value, dataList)

                LineChart(
                    hours = hours,
                    values = values,
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(180.dp)
                        .align(CenterHorizontally)
                )
            }

            ResultState.Idle -> {}
        }

        Text(
            "Grafik ini adalah grafik per tanggal ${currentDate.value}",
            style = MaterialTheme.typography.bodySmall,
            textAlign = TextAlign.Center,
            fontStyle = FontStyle.Italic,
            modifier = Modifier
                .fillMaxWidth()
                .alpha(0.5f)
                .padding(vertical = 8.dp)
        )

        DetailSensorData(currentData = currentData, sensorId = sensorId)
    }
}
