/*
 * LeMap
 */


export default class LeMap {

  constructor (whole, mapname) {

    this.whole = whole;

    this.game = this.whole.game;

    this.mapname = mapname;

    this.map = null;

    this.obstacles = null;
    this.actions = null;

  }

  preload() {
    this.lodingres = this.game.load.tilemap(this.mapname, 'level/'+this.mapname+'.json', null, Phaser.Tilemap.TILED_JSON);
    console.log( this.lodingres );
  }

  init() {

    this.tilemap = this.game.add.tilemap( this.mapname );

    //    params: 1. The name of the tileset as specified in the map data.
    //            2. The key of the Phaser.Cache image used for this tileset.
    this.tilemap.addTilesetImage('Map Blocks', 'blocks');
    //this.map.addTilesetImage('walls_1x2');
    //this.map.addTilesetImage('tiles2');

    //    params: Name of the Layer as defined in the map data
    this.layer_background = this.tilemap.createLayer('Background Layer');
    this.layer_obstacles  = this.tilemap.createLayer('Obstacle Layer');
    this.layer_action     = this.tilemap.createLayer('Action Layer');

    // get objects
    //var objects = this.game.physics.p2.convertCollisionObjects( this.tilemap, 'Object Layer 1');
    //console.log('NUTEST:', objects);

    this._creatActionObjects('Object Layer 1');

    console.log("Actionobjects: ", this.actionobjects);

    //this.layer.resizeWorld();

    // TODO: make all kinematic (performance)

    //  Set the tiles for collision.
    //  Do this BEFORE generating the p2 bodies below.
    this.tilemap.setCollisionBetween(1, 20, true, this.layer_obstacles);
    //this.tilemap.setCollisionBetween(21, 26, true, this.layer_action);

    //var test = this.tilemap.setTileIndexCallback([21,22,23,24,25,26], this.actionTriggered, this, this.layer_action);
    //console.log(test);

    //  Convert the tilemap layer into bodies. Only tiles that collide (see above) are created.
    //  This call returns an array of body objects which you can perform addition actions on if
    //  required. There is also a parameter to control optimising the map build.

    this.obstacles  = this.game.physics.p2.convertTilemap( this.tilemap, this.layer_obstacles );
    //this.actions    = this.game.physics.p2.convertTilemap( this.tilemap, this.layer_action );

    //console.log("Obstacles:", this.obstacles);
    //console.log("Actions:", this.actions);

    //var obj = this._findObjectsByType('action_exit', this.tilemap, 'Object Layer 1');
    //console.log("OBJECT: ", obj);

  }

  actionTriggered(bodyA, bodyB, shapeA, shapeB, contactEquations) {

    // get the correct button
    var button = this.whole.button1;
    console.log(shapeB.body.parent.my_props.name);
    if (shapeB.body.parent.my_props.name === '2') {
      //console.log('WTO');
      button = this.whole.button2;
    }

    // EXIT
    if (shapeA.body.parent.my_props.type === 'action_exit') {
      //console.log("EXITING", button);
      this.whole.exiting(button);
    }
  }


  initTriggers() {
    for (var i=0; i<this.actionobjects.length; i++){
      //if (this.actionobjects[i].properties.action == 'ramp'){
      this.actionobjects[i].onBeginContact.add( this.actionTriggered, this);
      //console.log("oeuoa",this.actionobjects[i].debug);
      //}
    }
  }


  _creatActionObjects(layer) {

    this.actionobjects = [];

    for (var i = 0, len = this.tilemap.objects[layer].length; i < len; i++)  {
      //console.log(i);
      var object = this.tilemap.objects[layer][i];
      console.log(object);
      // x, y, mass, addToWorld, options, points
      //var sprite = this.add.sprite(0, 0, null);
      var body = this.game.physics.p2.createBody(object.x, object.y, 0, true,  {} , object.polyline);
      //console.log(body, object.polyline);
      if (body){
        body.my_props = {
          name: object.name,
          type: object.type
        };
        //this.game.physics.p2.enable(body, true);
        //body.debug = true;
        this.actionobjects.push(body);
      }
    }

  }

}
