use std::ops::{Deref, DerefMut};

use bevy::{
    ecs::{
        event::Event,
        system::{Commands, SystemId},
    },
    state::state::{FreelyMutableState, NextState, States},
};

use crate::menus::lib::ToDo;

/// positions the item in the menu
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum ItemPosition {
    #[default]
    MainView,
    North,
    NorthWest,
    West,
    SouthWest,
    South,
    SouthEast,
    East,
    NorthEast,
}

// TODO: replace strings with custom "content"?
#[derive(Clone, Debug)]
pub enum MenuItemType {
    Text(String),
    Button(String),
    // Subsection(Vec<MenuItemType>),
}

#[derive(Clone, Debug)]
pub enum OnPressAction<NavState: States> {
    NavigateTo(NavState),
    DispatchSystem(SystemId),
    Multiple(Vec<OnPressAction<NavState>>),
    Close,
}

impl<S: States + FreelyMutableState> OnPressAction<S> {
    pub fn run(&self, c: &mut Commands, mut nav_state: &mut NextState<S>, closed_state: &S) {
        dbg!(self);
        match self {
            OnPressAction::NavigateTo(to) => nav_state.set(to.clone()),
            OnPressAction::DispatchSystem(system_id) => c.run_system(*system_id),
            OnPressAction::Multiple(actions) => {
                let mut actions = actions.clone();
                while let Some(action) = actions.pop() {
                    action.run(c, nav_state, closed_state);
                }
            }
            OnPressAction::Close => nav_state.set(closed_state.clone()),
        }
    }
}
