package org.hirw.game;

import java.util.EnumMap;
import java.util.Map;
import lombok.Getter;
import org.hirw.game.util.Log;

public final class SceneManager {
  private static final SceneType DEFAULT_SCENE_TYPE = SceneType.SPLASH;

  private static final EnumMap<SceneType, Scene> SCENES =
      new EnumMap<>(
          Map.of(
              SceneType.SPLASH, new SplashScene(),
              SceneType.MENU, new SplashScene(),
              SceneType.GAME, new SplashScene()));

  @Getter private static Scene scene;

  public static void init() {
    setScene(DEFAULT_SCENE_TYPE);
  }

  public static void setScene(SceneType sceneType) {
    Scene newScene = SCENES.get(sceneType);

    if (scene == newScene) {
      Log.warning("SceneManager", sameSceneWarningString());
      return;
    } else {
      scene = newScene;
    }

    scene.init();
    logSceneChange();
  }

  public static void update() {
    if (scene.getSceneType() == null) return;

    getScene().update();
  }

  private static void logSceneChange() {
    SceneType newSceneType = getScene().getSceneType();
    String loadedSceneMessage = String.format("Changed to scene '%s'", newSceneType.toString());

    Log.success("SceneManager", loadedSceneMessage);
  }

  private static String sameSceneWarningString() {
    return String.format(
        "Tried to switch to scene '%s' but that scene is already loaded",
        scene.getSceneType().toString());
  }
}
