pub mod drag_math;
mod modal_transform;
mod update;

pub use modal_transform::{
    cancel_modal_transform, confirm_modal_transform, handle_numeric_input_for_transform,
    start_modal_transform, update_modal_transform,
};
pub use update::update_gizmo;

use super::context::{self, EditorContext};
use nightshade::prelude::*;

pub fn recreate_gizmo_for_mode(context: &mut EditorContext, world: &mut World) {
    use nightshade::ecs::gizmos;

    if let Some(old_gizmo) = context.gizmo_interaction.active.take() {
        gizmos::destroy_gizmo(world, &old_gizmo);
    }

    context.gizmo_interaction.hover_axis = None;
    context.gizmo_interaction.drag_mode = nightshade::ecs::gizmos::GizmoDragMode::None;
}

pub fn update_gizmo_mode_for_coordinate_space(context: &mut EditorContext, world: &mut World) {
    let new_mode = match (context.gizmo_interaction.mode, context.coordinate_space) {
        (
            nightshade::ecs::gizmos::GizmoMode::GlobalTranslation
            | nightshade::ecs::gizmos::GizmoMode::LocalTranslation,
            context::CoordinateSpace::World,
        ) => nightshade::ecs::gizmos::GizmoMode::GlobalTranslation,
        (
            nightshade::ecs::gizmos::GizmoMode::GlobalTranslation
            | nightshade::ecs::gizmos::GizmoMode::LocalTranslation,
            context::CoordinateSpace::Local,
        ) => nightshade::ecs::gizmos::GizmoMode::LocalTranslation,
        _ => context.gizmo_interaction.mode,
    };

    if new_mode != context.gizmo_interaction.mode {
        context.gizmo_interaction.mode = new_mode;
        recreate_gizmo_for_mode(context, world);
    }
}
