package game.ui;

import openfl.events.MouseEvent;

import base.GameSprite;

import game.locations.Location;

class NavButton extends GameSprite {
  static inline var gap: Float = 2;
  static inline var dimAlpha: Float = 0.3;

  var to: Class<Location>;

  public function new(dir: String, to: Class<Location>) {
    super('assets/ui/a_' + dir + '.png', true);

    buttonMode = true;
    alpha = dimAlpha;

    this.to = to;

    addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
    addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
    addEventListener(MouseEvent.MOUSE_UP, onMouseUp);

    switch(dir) {
      case 'up':
        x = Math.floor(Main.SCREEN_WIDTH / 2);
        y = Math.floor(height / 2 + gap);
      case 'down':
        x = Math.floor(Main.SCREEN_WIDTH / 2);
        y = Math.floor(Main.SCREEN_HEIGHT - height / 2 - gap);
      case 'left':
        x = Math.floor(width / 2 + gap);
        y = Math.floor(Main.SCREEN_HEIGHT / 2);
      case 'right':
        x = Math.floor(Main.SCREEN_WIDTH - width / 2 - gap);
        y = Math.floor(Main.SCREEN_HEIGHT / 2);
    }
  }

  function onMouseDown(e: MouseEvent) {
    scaleX = scaleY = 0.9;
    Game.currentGame.setLocation(to);
  }

  function onMouseOver(e: MouseEvent) {
    alpha = 1;
  }

  function onMouseOut(e: MouseEvent) {
    alpha = dimAlpha;
  }

  function onMouseUp(e: MouseEvent) {
    scaleX = scaleY = 1;
  }
}
