package game.locations.finalExit;

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

import base.Fade;
import base.GameSprite;

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

import aseprite.Aseprite;

class Alarm extends Interactive {
  var anim: Aseprite;

  public function new() {
    super(false, 1);

    anim = new Aseprite('assets/locations/alarmClock/alarm');
    anim.play('6-01-buzz');
    addChild(anim);
  }

  override function action() {
    Assets.getSound('sound/button_toggle.ogg').play();
    anim.play('6-01');
    dispatchEvent(new Event('stop'));
  }
}

class FinalAlarm extends Location {
  var buzzLoop: SoundChannel;
  var alarm: Alarm;

  public function new() {
    super();

    id = 'alarmClock';

    addBmpLayer('bg');

    alarm = new Alarm();
    alarm.x = 70;
    alarm.y = 125;
    alarm.addEventListener('stop', function(e: Event) {
      buzzLoop.stop();

      var titles = new GameSprite('assets/outro/titles.png');
      titles.alpha = 0;
      addChild(titles);
      titles.tween({ alpha: 1 }, 5);
    });
    addChild(alarm);

    addChild(new Fade(3));
  }

  override function enter() {
    var buzzerFadeIn = Game.currentGame.playSound('sound/buzzer_fade_in.ogg');
    buzzerFadeIn.addEventListener(Event.SOUND_COMPLETE, function(e: Event) {
      buzzLoop = Assets.getSound('sound/buzzer.ogg').play(0, 999);
      alarm.actionable = true;
    });
  }
}
