package game.locations.finalExit;

import openfl.events.Event;
import openfl.media.SoundChannel;

import game.locations.Location;
import game.locations.Interactive;

import base.GameSprite;
import base.Fade;

import aseprite.Aseprite;

class FinalButton extends Interactive {
  var anim: Aseprite;

  public function new() {
    super(1);

    anim = new Aseprite('assets/locations/finalExit/button');
    anim.play('off');
    addChild(anim);
  }

  override function action() {
    anim.play('on');
    Game.currentGame.playSound('sound/elevator/button.ogg');
    var sound = Game.currentGame.playSound('sound/elevator/move_shorter.ogg');
    sound.addEventListener(Event.SOUND_COMPLETE, function(e: Event) {
      anim.play('off');
      dispatchEvent(new Event('done'));
    });
  }
}

class FinalBeach extends Interactive {
  public function new() {
    super(false, 1);
    initBitmap('assets/locations/finalExit/beach.png', true);
  }

  override function action() {
    dispatchEvent(new Event('exit'));
  }
}

class FinalExit extends Location {
  var beachSound: SoundChannel;
  public function new() {
    super();

    id = 'finalExit';

    var beach = new FinalBeach();
    beach.x = Main.SCREEN_WIDTH / 2;
    beach.y = Main.SCREEN_HEIGHT / 2;
    addChild(beach);

    var doorLeft = new GameSprite(bmpPath('door_left'));
    doorLeft.x = 128;
    doorLeft.y = 56;
    addChild(doorLeft);

    var doorRight = new GameSprite(bmpPath('door_right'));
    doorRight.x = 158;
    doorRight.y = 56;
    addChild(doorRight);

    var walls = addBmpLayer('walls');
    walls.mouseEnabled = false;

    var light = new GameSprite('assets/locations/finalExit/light.png', true);
    light.mouseEnabled = false;
    light.x = Main.SCREEN_WIDTH / 2;
    light.y = Main.SCREEN_HEIGHT / 2;
    light.scaleX = 0;
    walls.addChild(light);

    var button = new FinalButton();
    button.x = 205;
    button.y = 90;
    addChild(button);

    beach.addEventListener('exit', function(e: Event) {
      beach.tween({ scaleX: 1.1, scaleY: 1.1 }, 9);
      walls.tween({ alpha: 0 }, 9);
      button.tween({ alpha: 0 }, 9);

      var bigDoorLeft = new GameSprite(bmpPath('big_door_left'));
      bigDoorLeft.x = -bigDoorLeft.width;
      addChild(bigDoorLeft);

      var bigDoorRight = new GameSprite(bmpPath('big_door_right'));
      bigDoorRight.x = Main.SCREEN_WIDTH;
      addChild(bigDoorRight);

      timer(9, function() {
        beachSound.stop();
        Game.currentGame.stopSoundTrack();
        Game.currentGame.playSound('sound/final/slam.ogg');
          bigDoorLeft.tween({ x: 0 }, 1);
          bigDoorRight.tween({ x: Main.SCREEN_WIDTH - bigDoorRight.width }, 1).then(function() {
            addChild(new Fade('out', 3, function() {
              Game.currentGame.setLocation(game.locations.finalExit.FinalAlarm);
            }));
        });
      });
    });

    button.addEventListener('done', function(e: Event) {
      var sound = Game.currentGame.playSound('sound/elevator/doors_open.ogg');
      beachSound = Game.currentGame.playSound('sound/final/beach.ogg');
      light.tween({scaleX: 1}, 2);
      doorLeft.tween({x: doorLeft.x - doorLeft.width}, 2);
      doorRight.tween({x: doorRight.x + doorRight.width}, 2).then(function() {
        removeChild(doorLeft);
        removeChild(doorRight);
        beach.actionable = true;
      });
    });
  }
}
