package com.harshmallow.pilottoolkit.ui.modules

import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.harshmallow.pilottoolkit.data.Aircraft
import com.harshmallow.pilottoolkit.data.FuelBucket
import com.harshmallow.pilottoolkit.data.FuelProfile
import com.harshmallow.pilottoolkit.ui.components.*
import com.harshmallow.pilottoolkit.ui.theme.ToolkitTheme

@Composable
fun FuelBucketsModule(aircraft: Aircraft, fuelProfile: FuelProfile? = null) {
    val colors = ToolkitTheme.colors
    val buckets = fuelProfile?.buckets ?: aircraft.fuelBuckets
    val isCustom = fuelProfile != null

    if (buckets == null || buckets.isEmpty()) {
        UnavailableMessage("${aircraft.label} fuel bucket data is not yet available.\nImport a CSV or fuel profile via the menu.")
        return
    }

    var selectedIndex by remember(aircraft.id, isCustom) { mutableIntStateOf(0) }

    val bucket = buckets[selectedIndex]
    val maxIndex = buckets.size - 1

    Column(verticalArrangement = Arrangement.spacedBy(18.dp)) {
        FieldLabel("Stage Length (hrs)")
        Slider(
            value = selectedIndex.toFloat(),
            onValueChange = { selectedIndex = it.toInt() },
            valueRange = 0f..maxIndex.toFloat(),
            steps = maxIndex - 1,
            colors = SliderDefaults.colors(
                thumbColor = colors.accent,
                activeTrackColor = colors.accent,
                inactiveTrackColor = colors.border
            )
        )

        ResultCard {
            ResultRow(label = "Flight Time", value = "${"%.1f".format(bucket.stageLength)} hr")
            ResultRow(label = "Fuel Burn", value = "${"%.1f".format(bucket.fuelBurn)} lbs/hr")
        }

        Text(
            if (isCustom) "Using \"${fuelProfile!!.name}\" profile. Verify against current company fuel schedules."
            else "Using built-in data. Verify against current company fuel schedules.",
            fontSize = 12.sp, color = colors.textMuted, lineHeight = 16.sp
        )
    }
}
