import Phaser from 'phaser';
import Scenario from '../objects/scenario';
import LU from '../display/layout';
import Cinematic from '../objects/cinematic'
import GoreOverlay from '../objects/gore-overlay'
import Letterbox from '../objects/letterbox'

export default class extends Phaser.State {
  init() { }
  preload() { }

  create() {
    this.game.time.desiredFps = 60;
    this.game.time.advancedTiming = false;

    this.game.input.touch.preventDefault = false;

    this.game.myEvents = {
      onCinematicEnd: new Phaser.Signal(),
      onGameInputBegin: new Phaser.Signal(),
      onGameplayEnd: new Phaser.Signal(),
      onWallCollision: new Phaser.Signal(),
      onGirlDrop: new Phaser.Signal(),
      onGirlPick: new Phaser.Signal(),
      onBeginOutro: new Phaser.Signal(),
      onGameOver: new Phaser.Signal(),
      onStartGore: new Phaser.Signal(),
    };

    this.view = this.game.add.group();

    window.addEventListener('resize', x => this.onResize(x));

    this.onResize(null);
    this.scenario = new Scenario(this.game, this.view, 'Scenario');
    this.cinematic = new Cinematic(this.game, this.view, "Cinematic");
    // this.game.myEvents.onCinematicEnd.dispatch();
    this.letterBox = new Letterbox(this.game, this.view, "Letterbox");

    // this.game.myEvents.onGameInputBegin.add(() => {
    //   this.game.add.tween(this.letterBox).to({ alpha: 0 }, 350, null, true);
    // }, this);

    // this.game.myEvents.onBeginOutro.add(() => {
    //   this.game.add.tween(this.letterBox).to({ alpha: 1 }, 350, null, true);
    // }, this);

    this.game.myEvents.onStartGore.add(() => {
      this.goreOverlay = new GoreOverlay(this.game, this.view, 'GoreOverlay');
    })
    this.onResize(null);

    // this.game.time.events.add(9000, () => { console.log("asdasda"); this.game.myEvents.onBeginOutro.dispatch(); }, this);

    game.input.keyboard.addKey(Phaser.Keyboard.ESC).onDown.add(() => {
      this.game.myEvents.onBeginOutro.dispatch();
    });

    // End screen
    this.endScr = this.game.add.sprite(0, 0, 'endGame');
    this.endScr.visible = false;
    this.view.add(this.endScr);

    this.game.myEvents.onGameOver.add(() => {
      this.endScr.visible = true;
    });
  }

  render() {
    // this.scenario.render();
  }

  update() {

  }

  onResize() {
    LU.RefreshSizes();
    LU.letterBoxView(this.view);
    if (this.scenario) this.scenario.map.onResize();
  }
}
