// Copyright © 2025 The µcad authors <info@ucad.xyz>
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Processed Model Info, generated by [`Processor`].

use bevy::{
    asset::Asset, color::Color, ecs::component::Component, pbr::StandardMaterial, reflect::TypePath,
};
use bevy_mod_outline::OutlineVolume;
use microcad_lang::model::OutputType;

use crate::*;

#[derive(Clone, Default, Asset, Component, TypePath)]
pub struct ModelViewState {
    info: processor::ModelInfo,
    pub is_selected: bool,
    pub is_hovered: bool,
    pub outline_volume: OutlineVolume,
    pub base_color: Color,
}

impl ModelViewState {
    pub fn new(info: processor::ModelInfo, state: &State) -> Self {
        Self {
            outline_volume: OutlineVolume {
                visible: matches!(info.output_type, OutputType::Geometry2D),
                colour: state.config.theme.darker.make_transparent(0.5).to_bevy(),
                width: 4.0,
            },
            base_color: info.color.unwrap_or(state.config.theme.bright.to_bevy()),
            info,
            ..Default::default()
        }
    }

    /// Generate material from view state.
    pub fn generate_material(&self) -> StandardMaterial {
        use crate::material::*;
        let mut material = match self.info.output_type {
            OutputType::Geometry2D => create_2d_material(self.base_color),
            OutputType::Geometry3D => create_3d_material(self.base_color),
            _ => unreachable!(),
        };
        if self.is_selected {
            material.base_color = Color::srgb(1.0, 0.0, 0.0);
        }
        material
    }

    /// Read-only access to model info.
    pub fn info(&self) -> &processor::ModelInfo {
        &self.info
    }
}
