use crate::editorgui::Packages;
use keeshond::gameloop::GameControl;
use std::any::Any;

pub mod sheetdocument;
pub mod costumedocument;
pub mod tilesetdocument;

pub struct DocumentState
{
    pub active : bool
}

impl DocumentState
{
    pub fn new() -> DocumentState
    {
        DocumentState
        {
            active : false
        }
    }
}

pub struct ActionHistory
{
    queue : Vec<Box<dyn Any>>
}

impl ActionHistory
{
    pub fn new() -> ActionHistory
    {
        ActionHistory
        {
            queue : Vec::new()
        }
    }
}

pub trait Document
{
    fn load(game : &mut GameControl, package_name : &str, item_name : &str) -> Self where Self : Sized;
    fn do_ui(&mut self, packages : &mut Packages, game: &mut GameControl, window_builder : imgui::Window, ui : &mut imgui::Ui) -> DocumentState;
    fn action_history(&mut self) -> &mut ActionHistory;
    fn queue_action(&mut self, action : Box<dyn Any>)
    {
        let history = self.action_history();
        history.queue.push(action);
    }
    fn apply_action(&mut self, game: &mut GameControl, action : &Box<dyn Any>) -> bool;
    fn do_action_queue(&mut self, game: &mut GameControl) -> bool
    {
        let mut dirty = false;
        let history = self.action_history();
        let mut queue = Vec::new();

        std::mem::swap(&mut history.queue, &mut queue);

        for action in queue
        {
            dirty |= self.apply_action(game, &action);
        }

        dirty
    }
}
