package game.locations.celestialLockBasement;

import openfl.events.Event;
import openfl.events.MouseEvent;

import base.GameSprite;

import game.locations.Location;

import aseprite.Aseprite;

class DistributionBoardSwitch extends GameSprite {
  public var on(default, set): Bool;

  var anim: Aseprite = new Aseprite('assets/locations/celestialLockBasement/distBoard/switch');
  public function new() {
    super();

    addChild(anim);

    buttonMode = true;

    addEventListener(MouseEvent.MOUSE_DOWN, function(e: MouseEvent) {
      on = !on;
    });
  }

  function set_on(value: Bool): Bool {
    Game.currentGame.playSound('sound/button_toggle.ogg');
    if(value) {
      anim.play('on');
    } else {
      anim.play('off');
    }

    on = value;

    dispatchEvent(new Event('toggle'));

    return on;
  }
}

class DistributionBoard extends Location {
  public var on: Bool = false;

  var switches: Array<DistributionBoardSwitch> = [for(i in 0...3) new DistributionBoardSwitch()];
  var ind: Aseprite = new Aseprite('assets/locations/celestialLockBasement/distBoard/ind');

  public function new() {
    super();

    id= 'celestialLockBasement/distBoard';

    addBmpLayer('bg');

    var switchLayer = new GameSprite();

    for(i in 0...switches.length) {
      switches[i].x = i * 20;
      switches[i].addEventListener('toggle', testSwitches);
      switchLayer.addChild(switches[i]);
    }

    switchLayer.x = Math.floor((Main.SCREEN_WIDTH - switchLayer.width) / 2);
    switchLayer.y = Math.floor((Main.SCREEN_HEIGHT - switchLayer.height) / 2);

    addChild(switchLayer);

    ind.x = Math.floor((Main.SCREEN_WIDTH - ind.width) / 2);
    ind.y = 60;
    ind.play('off');
    addChild(ind);

    rightLocation = game.locations.celestialLockBasement.CelestialLockBasement;
    leftLocation = game.locations.celestialLockBasement.BlankWall;
  }

  function testSwitches(e: Event) {
    on = (switches[0].on && switches[1].on && switches[2].on);

    if(on) {
      ind.play('on');
    } else {
      ind.play('off');
    }
  }
}
