package game.locations.phoneLock;

import openfl.display.DisplayObject;
import openfl.events.Event;
import openfl.events.MouseEvent;

import base.GameSprite;

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

import aseprite.Aseprite;

class TvInfoChannel extends GameSprite {
  public function new() {
    super();

    var anim = new Aseprite('assets/locations/phoneLock/tv/ad');
    anim.play();
    addChild(anim);
  }
}

class TvDisplay extends GameSprite {

  var _showng: DisplayObject = null;

  var _off: GameSprite = new GameSprite('assets/locations/phoneLock/tv/mask.png');
  var _static: Aseprite = new Aseprite('assets/locations/phoneLock/tv/static');

  public function new() {
    super();

    var displayMask = new GameSprite('assets/locations/phoneLock/tv/mask.png');
    mask = displayMask;
    addChild(displayMask);

    off();
  }

  public function off() {
    showChannel(_off);
  }

  public function showStatic() {
    _static.play();
    showChannel(_static);
  }

  public function showChannel(channel: DisplayObject) {
    if(_showng != null) {
      removeChild(_showng);
    }

    _showng = channel == null ? _off: channel;

    addChild(_showng);
  }
}

class TV extends GameSprite {
  var display: TvDisplay = new TvDisplay();

  var button: GameSprite = new GameSprite('assets/locations/phoneLock/tv/button.png');

  var knob: Aseprite = new Aseprite('assets/locations/phoneLock/tv/knob');

  var isOn(default, set): Bool;

  var currentChannel (default, set): Int  = 1;

  public function new() {
    super('assets/locations/phoneLock/tv/frame.png');

    addChild(display);

    button.x = 176;
    button.y = 162;
    button.buttonMode = true;
    button.addEventListener(MouseEvent.MOUSE_DOWN, toggle);
    addChild(button);

    knob.x = 16;
    knob.y = 157;
    knob.buttonMode = true;
    knob.addEventListener(MouseEvent.MOUSE_DOWN, switchChannel);
    addChild(knob);
  }

  function switchChannel(e: MouseEvent = null) {
    currentChannel++;
  }

  function set_currentChannel(value: Int): Int {
    if(value > 4) value = 1;
    if(value < 1) value = 4;

    if(isOn) {
      if(value == 3) {
        display.showChannel(new TvInfoChannel());
      } else {
        display.showStatic();
      }
    }

    knob.play(Std.string(value));

    return currentChannel = value;
  }

  function set_isOn(value: Bool): Bool {
    if(value) {
      display.showStatic();
    } else {
      display.off();
    }

    return isOn = value;
  }

  function toggle(e: MouseEvent = null) {
    isOn = !isOn;
  }

}

class PhoneLockTv extends Location {
  public function new() {
    super();

    id = 'phoneLock/tv';

    addBmpLayer('bg');

    var tv = new TV();
    tv.x = Math.floor((Main.SCREEN_WIDTH - tv.width) / 2);
    tv.y = Math.floor((Main.SCREEN_HEIGHT - tv.height) / 2);
    addChild(tv);

    downLocation = game.locations.phoneLock.PhoneLockRoom;
    rightLocation = game.locations.phoneLock.PhoneLockDoor;
    leftLocation = game.locations.phoneLock.PhoneLockTable;
  }
}
