mod guard;
mod loading;
mod title;

use sfsm::*;

use crate::utils::context::ContextMut;
use loading::Loading;
use title::TitleScreen;

use super::GameContext;

pub(super) static UPDATE_CONTEXT: ContextMut<GameContext> = ContextMut::new();

add_state_machine!(Machine, Loading, {Loading, TitleScreen}, {
  Loading => TitleScreen,
  TitleScreen => TitleScreen
});

pub(super) struct GameStateMachine(Machine);

impl GameStateMachine {
  pub fn new(context: &mut GameContext) -> Self {
    UPDATE_CONTEXT.scoped(context, || {
      let mut machine = GameStateMachine(Machine::new());

      UPDATE_CONTEXT.with(|context| machine.0.start(Loading::new(context))).unwrap();

      machine
    })
  }

  pub fn update(&mut self, context: &mut GameContext) {
    UPDATE_CONTEXT.scoped(context, || {
      self.0.step().expect("state machine error");
    })
  }
}
