use sfsm::{State, Transition};
use winit::keyboard::KeyCode;

use crate::game::{
  anm::{Vm, VmLocation}, GameContext
};

use super::{loading::Loading, UPDATE_CONTEXT};

pub struct TitleScreen {
  _logo: Vm,
  _splash: Vm,
}

impl TitleScreen {
  fn new(context: &mut GameContext, from_startup: bool) -> Self {
    let title = context.load_anm("title.anm");

    // 79, 83
    let splash = context.anm_manager.new_vm(title.clone(), None, false, false, 79, VmLocation::new_ui());
    let logo = context.anm_manager.new_vm(title.clone(), None, false, true, 83, VmLocation::new_ui());

    Self {
      _logo: logo,
      _splash: splash,
    }
  }
}

impl State for TitleScreen {
  fn execute(&mut self) {}
}

impl From<Loading> for TitleScreen {
  fn from(_: Loading) -> Self {
    UPDATE_CONTEXT.with(|context| TitleScreen::new(context, true))
  }
}

impl Transition<TitleScreen> for TitleScreen {
  fn action(&mut self) {
    *self = UPDATE_CONTEXT.with(|context| TitleScreen::new(context, true))
  }
  fn guard(&self) -> sfsm::TransitGuard {
    UPDATE_CONTEXT.with(|context| context.engine.keys.was_key_pressed(KeyCode::Space)).into()
  }
}
