use super::super::super::*;
use std::collections::BTreeMap;
use std::sync::OnceLock;

pub(in crate::inspect::smp) fn recovered_locomotive_availability_descriptor_metadata(
    descriptor_id: u32,
) -> Option<RealGroupedEffectDescriptorMetadata> {
    if let Some(loco_id) = recovered_locomotive_availability_loco_id(descriptor_id) {
        let label = recovered_locomotive_availability_label(loco_id);
        let executable_in_runtime = (loco_id as usize) <= GROUNDED_LOCOMOTIVE_PREFIX.len();
        return Some(RealGroupedEffectDescriptorMetadata {
            descriptor_id,
            label,
            target_mask_bits: 0x08,
            parameter_family: "locomotive_availability_scalar",
            runtime_key: None,
            runtime_status: if executable_in_runtime {
                RealGroupedEffectRuntimeStatus::Executable
            } else {
                RealGroupedEffectRuntimeStatus::EvidenceBlocked
            },
            executable_in_runtime,
        });
    }
    (457..=474)
        .contains(&descriptor_id)
        .then(|| RealGroupedEffectDescriptorMetadata {
            descriptor_id,
            label: upper_band_locomotive_availability_label(descriptor_id),
            target_mask_bits: 0x08,
            parameter_family: "locomotive_availability_scalar",
            runtime_key: None,
            runtime_status: RealGroupedEffectRuntimeStatus::EvidenceBlocked,
            executable_in_runtime: false,
        })
}

pub(in crate::inspect::smp) fn recovered_locomotive_availability_loco_id(
    descriptor_id: u32,
) -> Option<u32> {
    if (241..=351).contains(&descriptor_id) {
        return Some(descriptor_id - 240);
    }
    None
}

pub(in crate::inspect::smp) fn grounded_locomotive_name(loco_id: u32) -> Option<&'static str> {
    let index = loco_id.checked_sub(1)? as usize;
    GROUNDED_LOCOMOTIVE_PREFIX.get(index).copied()
}

pub(in crate::inspect::smp) fn recovered_locomotive_availability_label(
    loco_id: u32,
) -> &'static str {
    static LABELS: OnceLock<BTreeMap<u32, &'static str>> = OnceLock::new();
    LABELS
        .get_or_init(|| {
            (1..=111)
                .map(|loco_id| {
                    let label = grounded_locomotive_name(loco_id)
                        .map(|name| format!("{name} Availability"))
                        .unwrap_or_else(|| {
                            format!("Lower-Band Locomotive Availability Slot {loco_id}")
                        });
                    (loco_id, Box::leak(label.into_boxed_str()) as &'static str)
                })
                .collect()
        })
        .get(&loco_id)
        .copied()
        .expect("lower-band locomotive availability label should exist")
}

pub(in crate::inspect::smp) fn upper_band_locomotive_availability_label(
    descriptor_id: u32,
) -> &'static str {
    static LABELS: OnceLock<BTreeMap<u32, &'static str>> = OnceLock::new();
    LABELS
        .get_or_init(|| {
            (457..=474)
                .map(|descriptor_id| {
                    let label = format!(
                        "Upper-Band Locomotive Availability Slot {}",
                        descriptor_id - 456
                    );
                    (
                        descriptor_id,
                        Box::leak(label.into_boxed_str()) as &'static str,
                    )
                })
                .collect()
        })
        .get(&descriptor_id)
        .copied()
        .expect("upper-band locomotive availability label should exist")
}

pub(in crate::inspect::smp) fn recovered_locomotive_cost_loco_id(
    descriptor_id: u32,
) -> Option<u32> {
    if (352..=451).contains(&descriptor_id) {
        return Some(descriptor_id - 351);
    }
    None
}

pub(in crate::inspect::smp) fn recovered_locomotive_cost_label(
    descriptor_id: u32,
) -> Option<&'static str> {
    static LABELS: OnceLock<BTreeMap<u32, &'static str>> = OnceLock::new();
    LABELS
        .get_or_init(|| {
            (352..=451)
                .filter_map(|descriptor_id| {
                    recovered_locomotive_cost_loco_id(descriptor_id).map(|loco_id| {
                        let label = grounded_locomotive_name(loco_id)
                            .map(|name| format!("{name} Cost"))
                            .unwrap_or_else(|| {
                                format!("Lower-Band Locomotive Cost Slot {loco_id}")
                            });
                        let label = Box::leak(label.into_boxed_str()) as &'static str;
                        (descriptor_id, label)
                    })
                })
                .collect()
        })
        .get(&descriptor_id)
        .copied()
        .or_else(|| upper_band_locomotive_cost_label(descriptor_id))
}

pub(in crate::inspect::smp) fn upper_band_locomotive_cost_label(
    descriptor_id: u32,
) -> Option<&'static str> {
    static LABELS: OnceLock<BTreeMap<u32, &'static str>> = OnceLock::new();
    LABELS
        .get_or_init(|| {
            (475..=502)
                .map(|descriptor_id| {
                    let label = format!("Upper-Band Locomotive Cost Slot {}", descriptor_id - 474);
                    (
                        descriptor_id,
                        Box::leak(label.into_boxed_str()) as &'static str,
                    )
                })
                .collect()
        })
        .get(&descriptor_id)
        .copied()
}

pub(in crate::inspect::smp) fn recovered_locomotive_cost_descriptor_metadata(
    descriptor_id: u32,
) -> Option<RealGroupedEffectDescriptorMetadata> {
    recovered_locomotive_cost_label(descriptor_id).map(|label| {
        let executable_in_runtime = recovered_locomotive_cost_loco_id(descriptor_id)
            .is_some_and(|loco_id| (loco_id as usize) <= GROUNDED_LOCOMOTIVE_PREFIX.len());
        RealGroupedEffectDescriptorMetadata {
            descriptor_id,
            label,
            target_mask_bits: 0x08,
            parameter_family: "locomotive_cost_scalar",
            runtime_key: None,
            runtime_status: if executable_in_runtime {
                RealGroupedEffectRuntimeStatus::Executable
            } else {
                RealGroupedEffectRuntimeStatus::EvidenceBlocked
            },
            executable_in_runtime,
        }
    })
}

pub(in crate::inspect::smp) fn recovered_locomotive_policy_descriptor_metadata(
    descriptor_id: u32,
) -> Option<RealGroupedEffectDescriptorMetadata> {
    match descriptor_id {
        454 => Some(RealGroupedEffectDescriptorMetadata {
            descriptor_id,
            label: "All Steam Locos Avail.",
            target_mask_bits: 0x08,
            parameter_family: "world_flag_toggle",
            runtime_key: Some("world.all_steam_locos_available"),
            runtime_status: RealGroupedEffectRuntimeStatus::Executable,
            executable_in_runtime: true,
        }),
        455 => Some(RealGroupedEffectDescriptorMetadata {
            descriptor_id,
            label: "All Diesel Locos Avail.",
            target_mask_bits: 0x08,
            parameter_family: "world_flag_toggle",
            runtime_key: Some("world.all_diesel_locos_available"),
            runtime_status: RealGroupedEffectRuntimeStatus::Executable,
            executable_in_runtime: true,
        }),
        456 => Some(RealGroupedEffectDescriptorMetadata {
            descriptor_id,
            label: "All Electric Locos Avail.",
            target_mask_bits: 0x08,
            parameter_family: "world_flag_toggle",
            runtime_key: Some("world.all_electric_locos_available"),
            runtime_status: RealGroupedEffectRuntimeStatus::Executable,
            executable_in_runtime: true,
        }),
        _ => None,
    }
}
