mod inspector;

use sdl3_sys::events::SDL_Event;

use crate::entity::EntityHandle;
use crate::systems::camera_noclip_system;
use crate::utility::input::InputState;
use crate::world::World;

pub use inspector::FrameStats;
use inspector::Inspector;

pub struct EditorState
{
    pub active: bool,
    pub right_mouse_held: bool,
    pub selected_entity: Option<EntityHandle>,
    pub show_player_state: bool,
    inspector: Inspector,
}

impl EditorState
{
    pub fn new(
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        surface_format: wgpu::TextureFormat,
    ) -> Self
    {
        Self {
            active: false,
            right_mouse_held: false,
            selected_entity: None,
            show_player_state: false,
            inspector: Inspector::new(device, queue, surface_format),
        }
    }

    pub fn init_platform(&mut self, window: &sdl3::video::Window)
    {
        self.inspector.init_platform(window);
    }

    pub fn begin_frame(&mut self)
    {
        self.inspector.begin_frame();
    }

    pub fn process_event(&mut self, raw_event: &SDL_Event) -> bool
    {
        self.inspector.process_event(raw_event);
        false
    }

    pub fn wants_keyboard(&self) -> bool
    {
        self.inspector.wants_keyboard()
    }

    pub fn wants_mouse(&self) -> bool
    {
        self.inspector.wants_mouse()
    }

    pub fn build_hud(&mut self, world: &World)
    {
        self.inspector.build_minimal_ui(world);
    }

    pub fn render(&mut self, encoder: &mut wgpu::CommandEncoder, view: &wgpu::TextureView)
    {
        self.inspector.render(encoder, view);
    }
}

pub fn editor_loop(
    editor: &mut EditorState,
    world: &mut World,
    input_state: &InputState,
    stats: &FrameStats,
    delta: f32,
)
{
    if editor.right_mouse_held
    {
        camera_noclip_system(world, input_state, delta);
    }
    let selected = editor.selected_entity;
    let show_player_state = editor.show_player_state;
    editor
        .inspector
        .build_ui(stats, world, selected, show_player_state);
}
