package game.locations;

import openfl.display.Bitmap;
import openfl.Assets;

import game.ui.NavButton;

import base.GameSprite;

class Location extends GameSprite {
  var id: String = 'location';

  var navLeft: NavButton = null;
  var navRight: NavButton = null;
  var navUp: NavButton = null;
  var navDown: NavButton = null;

  public var navigation (default, set): Bool = true;

  public var upLocation (default, set): Class<Location>;
  public var downLocation (default, set): Class<Location>;
  public var leftLocation (default, set): Class<Location>;
  public var rightLocation (default, set): Class<Location>;

  public function new() {
    super();
  }

  /**
   *  When entering the loaction
   */
  public function enter() {
  }

  /**
   *  When leaving the location
   */
  public function leave() {
  }

  public function bmpPath(name: String, ?ext: String = 'png'): String {
    return 'assets/locations/' + id + '/' + name + '.' + ext;
  }

  public function addBmpLayer(name: String, ?ext: String = 'png', ?cx: Float = 0, ?cy: Float = 0):GameSprite {
    var bmp: GameSprite = new GameSprite(bmpPath(name, ext));
    bmp.x = cx;
    bmp.y = cy;
    bmp.mouseEnabled = false;
    addChild(bmp);
    return bmp;
  }

  // FIXME: DRY

  function set_downLocation(value: Class<Location>): Class<Location> {
    if(navDown != null) {
      removeChild(navDown);
    }

    if(value != null) {
      navDown = new NavButton('down', value);
      addChild(navDown);
    }

    return downLocation = value;
  }

  function set_upLocation(value: Class<Location>): Class<Location> {
    if(navUp != null) {
      removeChild(navUp);
    }

    if(value != null) {
    navUp = new NavButton('up', value);
    addChild(navUp);
    }
    return upLocation = value;
  }

  function set_leftLocation(value: Class<Location>): Class<Location> {
    if(navLeft != null) {
      removeChild(navLeft);
    }

    if(value != null) {
      navLeft = new NavButton('left', value);
      addChild(navLeft);
    }

    return leftLocation = value;
  }

  function set_rightLocation(value: Class<Location>): Class<Location> {
    if(navRight != null) {
      removeChild(navRight);
    }

    if(value != null) {
      navRight = new NavButton('right', value);
      addChild(navRight);
    }

    return rightLocation = value;
  }

  function set_navigation(value: Bool): Bool {
    if(navLeft != null) {
      navLeft.visible = value;
    }

    if(navRight != null) {
      navRight.visible = value;
    }

    if(navUp != null) {
      navUp.visible = value;
    }

    if(navDown != null) {
      navDown.visible = value;
    }

    return navigation = value;
  }
}
