package game.locations.entrance;

import openfl.events.MouseEvent;
import openfl.text.TextField;
import openfl.text.TextFieldAutoSize;
import openfl.text.TextFormat;
import openfl.Assets;

import game.locations.Interactive;

import aseprite.Aseprite;

import base.GameSprite;

class LockButton extends GameSprite {
  var bg: Aseprite;
  var action: Void -> Void = null;

  public function new(?title: String = null, ?color: String = 'grey', ?action: Void -> Void = null) {
    super();

    bg = new Aseprite('assets/locations/entrance/btn-' + color);
    addChild(bg);

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

    if(title != null) {
      var caption = new TextField();
      caption.text = title;
      caption.selectable = false;
      caption.setTextFormat(new TextFormat(Assets.getFont('font/basis33.ttf').fontName, 8, 0x414c51));
      caption.autoSize = TextFieldAutoSize.LEFT;
      caption.x = Math.floor((width - caption.width) / 2);
      caption.y = Math.floor((height - caption.height) / 2);
      caption.mouseEnabled = false;
      addChild(caption);
    }

    buttonMode = true;

    this.action = action;
  }

  function onMouseDown(e: MouseEvent) {
    Game.currentGame.playSound('sound/elevator/button.ogg');
    bg.play('down');

    if(action != null) {
      action();
    }
  }

  function onMouseUp(e: MouseEvent) {
    bg.play('up');
  }

  public function disable() {
    bg.play('down');

    buttonMode = false;

    removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    removeEventListener(MouseEvent.MOUSE_OUT, onMouseUp);
  }
}
