import { Vector } from "p5";
import { p, state } from "./utils";
import { OsuRenderer, OsuRendererEvents } from "./osu/OsuRenderer";
import { Drawer } from "./osu/Drawer";

export class Renderer {

  static areImagesLoaded = false;
  static mouse: Vector;
  static OsuRenderer: OsuRenderer = OsuRenderer;

  static async setup() {
    Renderer.registerEvents();
    Drawer.setP(p);

    await Drawer.loadDefaultImages().then(
        () => {
          Renderer.areImagesLoaded = true;
          OsuRenderer.setPlaying(true);
        }
    );
  }

  static draw() {
    if (!OsuRenderer.beatmap || !this.areImagesLoaded) return;

    OsuRenderer.render();
  }

  static registerEvents() {
    // Sync UI with datapath classes
    OsuRenderer.event.on(OsuRendererEvents.UPDATE, () => {
      const options = OsuRenderer.getOptions();
      state.setState({
        beatmap: options.beatmap,
        replay: options.replay,
        mods: options.mods,
      });
    });

    OsuRenderer.event.on(OsuRendererEvents.LOAD, () => {

    });
    OsuRenderer.event.on(OsuRendererEvents.PLAY, () => {
      state.setState({
        playing: OsuRenderer.playing,
      });
    });
    OsuRenderer.event.on(OsuRendererEvents.TIME, () => {
      state.setState({
        time: OsuRenderer.time,
      });
    });
    OsuRenderer.event.on(OsuRendererEvents.SPEED, () => {
      state.setState({
        speed: OsuRenderer.speedMultiplier,
      });
    });
  }
}
