package game.locations.elevatorPhone;

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

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

class ElevatorPhone extends Location {
  var phoneClosed:Interactive = new Interactive();
  var phoneOpened:Interactive = new Interactive();
  var phoneListen:Interactive = new Interactive();
  var transmission: SoundChannel = null;
  var listened: Bool = false;

  public function new() {
    super();

    id = 'elevatorPhone';
    addBmpLayer('bg');

    phoneClosed.initBitmap(bmpPath('phone_closed'));
    phoneClosed.x = 73;
    phoneClosed.y = 42;
    phoneClosed.addEventListener('action', function(e: Event) {
      phoneOpened.visible = true;
      phoneClosed.inAction = false;
      Game.currentGame.playSound('sound/elevator/phone_box_open.ogg');
    });
    addChild(phoneClosed);

    phoneOpened.initBitmap(bmpPath('phone_opened'));
    phoneOpened.x = 42;
    phoneOpened.y = 54;
    phoneOpened.visible = false;
    phoneOpened.addEventListener('action', function(e: Event) {
      phoneListen.visible = true;
      phoneOpened.inAction = false;
      navigation = false;

      Game.currentGame.stopSoundTrack();

      Game.currentGame.playSound('sound/elevator/phone_pick_up.ogg');

      if(!listened) {
        Game.currentGame.say('Алло?', 1);
        Game.currentGame.say('Меня кто-нибудь слышит?', function() {
        });
      } else {
      }

      transmission = Game.currentGame.playSound('sound/entrance/transmission.ogg');
      transmission.addEventListener(Event.SOUND_COMPLETE, function(e: Event) {
        Game.currentGame.startSoundTrack();
        Game.currentGame.playSound('sound/elevator/phone_drop.ogg');
        phoneListen.visible = false;
        navigation = true;
        Game.currentGame.say('Так... это было немного странно.');
        Game.currentGame.say('Что это были за цифры? Может быть это код от двери?');
        Game.currentGame.startSoundTrack();
      });
      listened = true;
    });
    addChild(phoneOpened);

    phoneListen.initBitmap(bmpPath('phone_listen'));
    phoneListen.x = 42;
    phoneListen.y = 54;
    phoneListen.visible = false;
    phoneListen.addEventListener('action', function(e: Event) {
      phoneListen.inAction = false;
      if(transmission != null) {
        transmission.stop();
      }
      Game.currentGame.sayCancel();
      navigation = true;
      Game.currentGame.playSound('sound/elevator/phone_drop.ogg');
      phoneListen.visible = false;
      Game.currentGame.startSoundTrack();
    });
    addChild(phoneListen);

    upLocation = game.locations.elevatorStoppedInside.ElevatorStoppedInside;
  }
}
