use crate::ecs::EditorWorld;
use crate::systems::retained_ui::{Action, UiHandles};
use nightshade::prelude::*;

#[derive(Default, Clone)]
pub struct CommandPaletteHandles {
    pub entity: Entity,
}

pub fn build(tree: &mut UiTreeBuilder) -> CommandPaletteHandles {
    let entity = tree.add_command_palette(8);
    for command in commands() {
        ui_command_palette_register(
            tree.world_mut(),
            entity,
            command.label,
            command.shortcut,
            command.category,
        );
    }
    CommandPaletteHandles { entity }
}

pub fn poll(editor_world: &mut EditorWorld, world: &mut World, handles: &UiHandles) {
    let palette_entity = handles.command_palette.entity;
    let commands = commands();
    for event in ui_events(world) {
        if let UiEvent::CommandPaletteExecuted {
            entity,
            command_index,
        } = event
            && *entity == palette_entity
            && let Some(command) = commands.get(*command_index)
        {
            editor_world
                .resources
                .ui_interaction
                .actions
                .push(command.action.clone());
        }
    }
}

struct PaletteCommand {
    label: &'static str,
    shortcut: &'static str,
    category: &'static str,
    action: Action,
}

fn commands() -> Vec<PaletteCommand> {
    vec![
        PaletteCommand {
            label: "Add Cube",
            shortcut: "",
            category: "Add",
            action: Action::AddCube,
        },
        PaletteCommand {
            label: "Add Sphere",
            shortcut: "",
            category: "Add",
            action: Action::AddSphere,
        },
        PaletteCommand {
            label: "Add Cylinder",
            shortcut: "",
            category: "Add",
            action: Action::AddCylinder,
        },
        PaletteCommand {
            label: "Add Cone",
            shortcut: "",
            category: "Add",
            action: Action::AddCone,
        },
        PaletteCommand {
            label: "Add Torus",
            shortcut: "",
            category: "Add",
            action: Action::AddTorus,
        },
        PaletteCommand {
            label: "Add Plane",
            shortcut: "",
            category: "Add",
            action: Action::AddPlane,
        },
        PaletteCommand {
            label: "Add Instanced Cube",
            shortcut: "",
            category: "Add",
            action: Action::AddInstancedCube,
        },
        PaletteCommand {
            label: "Add Instanced Sphere",
            shortcut: "",
            category: "Add",
            action: Action::AddInstancedSphere,
        },
        PaletteCommand {
            label: "Add Instanced Cylinder",
            shortcut: "",
            category: "Add",
            action: Action::AddInstancedCylinder,
        },
        PaletteCommand {
            label: "Add Instanced Cone",
            shortcut: "",
            category: "Add",
            action: Action::AddInstancedCone,
        },
        PaletteCommand {
            label: "Add Instanced Torus",
            shortcut: "",
            category: "Add",
            action: Action::AddInstancedTorus,
        },
        PaletteCommand {
            label: "Add Instanced Plane",
            shortcut: "",
            category: "Add",
            action: Action::AddInstancedPlane,
        },
        PaletteCommand {
            label: "Convert similar meshes to instanced",
            shortcut: "",
            category: "Edit",
            action: Action::ConvertSimilarToInstanced,
        },
        PaletteCommand {
            label: "Add Point Light",
            shortcut: "",
            category: "Add",
            action: Action::AddPointLight,
        },
        PaletteCommand {
            label: "Add Spot Light",
            shortcut: "",
            category: "Add",
            action: Action::AddSpotLight,
        },
        PaletteCommand {
            label: "Add Directional Light",
            shortcut: "",
            category: "Add",
            action: Action::AddDirectionalLight,
        },
        PaletteCommand {
            label: "Delete Selected",
            shortcut: "Del",
            category: "Edit",
            action: Action::DeleteSelectedEntity,
        },
        PaletteCommand {
            label: "Duplicate Selected",
            shortcut: "Ctrl+D",
            category: "Edit",
            action: Action::DuplicateSelectedEntity,
        },
        PaletteCommand {
            label: "Undo",
            shortcut: "Ctrl+Z",
            category: "Edit",
            action: Action::Undo,
        },
        PaletteCommand {
            label: "Redo",
            shortcut: "Ctrl+Y",
            category: "Edit",
            action: Action::Redo,
        },
        PaletteCommand {
            label: "Frame Scene",
            shortcut: "F",
            category: "View",
            action: Action::FrameScene,
        },
        PaletteCommand {
            label: "Toggle Tree Panel",
            shortcut: "",
            category: "View",
            action: Action::ToggleTreePanel,
        },
        PaletteCommand {
            label: "Toggle Inspector Panel",
            shortcut: "",
            category: "View",
            action: Action::ToggleInspectorPanel,
        },
        PaletteCommand {
            label: "Toggle Ground Grid",
            shortcut: "",
            category: "View",
            action: Action::ToggleGroundGrid,
        },
        PaletteCommand {
            label: "Toggle Lit/Unlit",
            shortcut: "",
            category: "View",
            action: Action::ToggleLitUnlit,
        },
        PaletteCommand {
            label: "Toggle Skeleton View",
            shortcut: "",
            category: "View",
            action: Action::ToggleSkeletonView,
        },
        PaletteCommand {
            label: "Toggle Normals",
            shortcut: "",
            category: "View",
            action: Action::ToggleShowNormals,
        },
        PaletteCommand {
            label: "Normals Settings",
            shortcut: "",
            category: "View",
            action: Action::OpenNormalsSettings,
        },
        PaletteCommand {
            label: "Reset Viewport Layout",
            shortcut: "",
            category: "View",
            action: Action::ResetViewportLayout,
        },
        PaletteCommand {
            label: "New Map",
            shortcut: "",
            category: "Map",
            action: Action::NewProject,
        },
        PaletteCommand {
            label: "Open Map",
            shortcut: "Ctrl+O",
            category: "Map",
            action: Action::OpenProject,
        },
        PaletteCommand {
            label: "Save Map",
            shortcut: "Ctrl+S",
            category: "Map",
            action: Action::SaveProject,
        },
        PaletteCommand {
            label: "Save Map As",
            shortcut: "",
            category: "Map",
            action: Action::SaveProjectAs,
        },
        PaletteCommand {
            label: "Save Selected As Prefab",
            shortcut: "",
            category: "Prefab",
            action: Action::SaveSelectedAsPrefab,
        },
        PaletteCommand {
            label: "Import Prefab",
            shortcut: "",
            category: "Prefab",
            action: Action::ImportPrefab,
        },
        PaletteCommand {
            label: "Refresh Selected From Source",
            shortcut: "",
            category: "Prefab",
            action: Action::RefreshSelectedPrefab,
        },
        PaletteCommand {
            label: "Break Selected Prefab Link",
            shortcut: "",
            category: "Prefab",
            action: Action::BreakSelectedPrefabLink,
        },
        PaletteCommand {
            label: "Open Khronos Browser",
            shortcut: "",
            category: "Browse",
            action: Action::OpenKhronosBrowser,
        },
        PaletteCommand {
            label: "Open Polyhaven Browser",
            shortcut: "",
            category: "Browse",
            action: Action::OpenPolyhavenBrowser,
        },
        PaletteCommand {
            label: "Open Materials Browser",
            shortcut: "",
            category: "Browse",
            action: Action::OpenMaterialsBrowser,
        },
        PaletteCommand {
            label: "Open Tag Manager",
            shortcut: "",
            category: "Browse",
            action: Action::OpenTagManager,
        },
        PaletteCommand {
            label: "Open Prefab Browser",
            shortcut: "",
            category: "Browse",
            action: Action::OpenPrefabBrowser,
        },
        PaletteCommand {
            label: "Clear Scene",
            shortcut: "",
            category: "Edit",
            action: Action::ClearScene,
        },
    ]
}
