package game.locations.elevatorHall;

import openfl.events.Event;

import game.locations.Interactive;

import base.GameSprite;

import aseprite.Aseprite;

class ElevatorButton extends Interactive {
  var _on: GameSprite;
  var _off: GameSprite;

  var doors: ElevatorDoors;
  var indicator: Aseprite;

  public var onoff(default, set): Bool;

  public function new(doors: ElevatorDoors, indicator: Aseprite) {
    super();

    useTimes = 1;

    this.doors = doors;
    this.indicator = indicator;

    _on = new GameSprite('assets/locations/elevatorHall/button_on.png');
    _off = new GameSprite('assets/locations/elevatorHall/button_off.png');

    addChild(_on);
    addChild(_off);

    onoff = false;
  }

  function set_onoff(value: Bool): Bool {
    _on.visible = value;
    _off.visible = !value;
    return onoff = value;
  }

  override function action() {
    if(!onoff) {
      Game.currentGame.playSound('sound/elevator/button.ogg');

      onoff = true;

      indicator.playTimes(1, function() {
        doors.open(function() {
          dispatchEvent(new Event('opened'));
        });
        onoff = false;
      });
    }
  }
}
