// import Geom from '../utils/geom';
import ML from './scenario/map_logic';
import VFX from './vfx';


const SPEED = 10;
const CAM_DELAY = 0.5;

export default class Scenario extends Phaser.Group {
  constructor(game, parent, name) {
    super(game, parent, name);

    this.advance = 0;
    this.timeElapsed = 0;
    this.timeTouch = 0;
    this.firstTouch = false;
    this.create();
    this.vfx = new VFX(this.game, this, 'vfx');

    this.game.myEvents.onBeginOutro.add(() => {
      this.firstTouch = false;
    })
  }

  create() {
    const { game } = this;
    // const bg = new Geom(this.game, 0, 0, 'rectangle', [0, 0, 5760, 640], { bgColor: 0x808080, aX: 0, aY: 0 });
    // this.add(bg);

    this.strings = game.add.audio('strings');
    this.strings.play();

    this.perc = game.add.audio('perc');
    this.perc.play();
    this.perc.volume = 0.5;

    const sprMap = this.game.add.sprite(0, 0, 'map1');
    this.add(sprMap);

    this.map = new ML(this.game, null, 'Map', this.parent);
    this.map.createMap('l01');
    this.add(this.map);
  }





  // Separate player from wall so no infinity collision, called on map_logic
  separate(player, wall) {
    


    // const localWall = this.toLocal(wall, this.map);

    // const A = this.ninjaGrp.x - localWall.x; // x axis
    // const B = this.ninjaGrp.y - localWall.y; // y axis
    
    // if (A < 0) {
    //   this.ninjaGrp.x -= 25;
    // } else {
    //   this.ninjaGrp.x += 25;
    // }

    // if (B < 0) {
    //   this.ninjaGrp.y -= 25;
    // } else {
    //   this.ninjaGrp.y += 25;
    // }
    
    
  }

  render() {
    this.map.render();
  }

  update() {
    const { game } = this;

    this.timeElapsed =  this.game.time.totalElapsedSeconds() - this.timeTouch;
    this.map.update();
    
    if (game.input.activePointer.isDown && this.map.isHoldingHands) {

      if (this.timeElapsed) this.x -= SPEED;
    } else if (this.x < 0) {
      if (this.timeElapsed) this.x += SPEED * 0.6;
      if (this.x > 0) this.x = 0;
    }
    if (!this.map.isHoldingHands && this.timeElapsed > 1 && this.firstTouch) this.game.myEvents.onGameOver.dispatch();
  }
}
