package com.syaroful.agrilinkvocpro.plant_disease_detection_feature.presentation.history

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.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.Card
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.syaroful.agrilinkvocpro.core.components.DefaultErrorComponent
import com.syaroful.agrilinkvocpro.plant_disease_detection_feature.core.extention.toBitmap
import com.syaroful.agrilinkvocpro.plant_disease_detection_feature.data.local.entity.PlantDiagnosisEntity
import com.syaroful.agrilinkvocpro.plant_disease_detection_feature.presentation.detail.TextCardComponent


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DetailHistoryScreen(
    navController: NavHostController,
    diagnosisId: Int,
    viewModel: HistoryViewModel
) {
    var plantDiagnosis by remember { mutableStateOf<PlantDiagnosisEntity?>(null) }

    LaunchedEffect(diagnosisId) {
        viewModel.getDiagnosisById(diagnosisId).collect {
            plantDiagnosis = it
        }
    }

    Scaffold(
        topBar = {
            TopAppBar(
                title = {
                    Text(
                        "Riwayat Deteksi",
                        modifier = Modifier.fillMaxWidth(),
                        textAlign = TextAlign.Center
                    )
                },
                navigationIcon = {
                    IconButton(onClick = {
                        navController.popBackStack()
                    }) {
                        Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
                    }
                }
            )
        }
    ) { innerPadding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .verticalScroll(rememberScrollState())
                .padding(innerPadding)
                .padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(
                10.dp,
                Alignment.Top
            )
        ) {
            when (val data = plantDiagnosis) {
                null -> {
                    DefaultErrorComponent(
                        label = "Waduh!",
                        message = "Data Tidak Ditemukan",
                    )
                }

                else -> {
                    val bitmap = try {
                        data.image.toBitmap().asImageBitmap()
                    } catch (e: Exception) {
                        null
                    }

                    bitmap?.let {
                        Card(
                            modifier = Modifier
                                .fillMaxWidth()
                                .heightIn(max = 180.dp),
                            shape = RoundedCornerShape(8.dp),
                        ) {
                            Image(
                                modifier = Modifier.fillMaxSize(),
                                bitmap = it,
                                contentDescription = "Gambar Tanaman",
                                contentScale = ContentScale.Crop,
                            )
                        }
                    }
                    Box(
                        modifier = Modifier
                            .background(
                                color = Color.Red,
                                shape = RoundedCornerShape(4.dp)
                            )
                            .padding(vertical = 4.dp, horizontal = 8.dp)
                    ) {
                        Text(
                            text = "Terkena Penyakit",
                            color = Color.White,
                            style = MaterialTheme.typography.labelMedium
                        )
                    }
                    Text(
                        text = "Penyakit Terdeteksi :",
                        style = MaterialTheme.typography.bodySmall,
                        color = Color(0xFFFF3D3D)
                    )
                    Text(
                        plantDiagnosis?.diagnosis ?: "-",
                        style = MaterialTheme.typography.headlineSmall
                    )
                    Text(
                        text = "Penyebab :",
                        style = MaterialTheme.typography.bodySmall,
                        color = Color(0xFFFF3D3D)
                    )
                    Text(
                        plantDiagnosis?.cause ?: "-",
                        style = MaterialTheme.typography.titleSmall
                    )
                    TextCardComponent(
                        label = "Informasi Umum",
                    ) {
                        Text(
                            modifier = Modifier.fillMaxWidth(),
                            text = plantDiagnosis?.description ?: "-",
                            style = MaterialTheme.typography.bodyMedium
                        )
                    }
                    TextCardComponent(
                        label = "Pengobatan",
                    ) {
                        Text(
                            modifier = Modifier.fillMaxWidth(),
                            text = plantDiagnosis?.treatment ?: "-",
                            style = MaterialTheme.typography.bodyMedium
                        )
                    }
                    TextCardComponent(
                        label = "Pencegahan",
                    ) {
                        Text(
                            modifier = Modifier.fillMaxWidth(),
                            text = plantDiagnosis?.prevention ?: "-",
                            style = MaterialTheme.typography.bodyMedium
                        )
                    }
                }
            }


        }
    }
}