use pns::{State, Tid};
use rfd::FileDialog;

use std::{collections::BTreeMap, path::PathBuf};

pub struct StateInfo {
    pub state_path: Option<PathBuf>,
    pub changes: bool,
    pub callables: BTreeMap<Tid, (bool, bool)>,
}

impl StateInfo {
    pub fn update_state(&mut self, state: &mut State) {
        for added in state.added_transitions().iter() {
            self.callables.get_mut(added).unwrap().0 = true;
        }
        for removed in state.removed_transitions().iter() {
            self.callables.get_mut(removed).unwrap().0 = false;
        }
        for added in state.added_transitions_backwards().iter() {
            self.callables.get_mut(added).unwrap().1 = true;
        }
        for removed in state.removed_transitions_backwards().iter() {
            self.callables.get_mut(removed).unwrap().1 = false;
        }
    }

    pub fn file_dialog(&self) -> FileDialog {
        let dialog = FileDialog::new().add_filter("Petri net interactive files", &["pni"]);

        if let Some(path) = &self.state_path {
            dialog.set_directory(path)
        } else {
            if let Ok(path) = std::env::current_dir() {
                dialog.set_directory(path)
            } else {
                dialog
            }
        }
    }
}
