var EHDI = EHDI || Object.create(null);

EHDI.GAME = EHDI.GAME || Object.create(null);

EHDI.scene = EHDI.scene || Object.create(null);

EHDI.scene.TitleScene = function() {
    EHDI.aka.Container.call(this);
}

EHDI.scene.TitleScene.prototype = Object.create(EHDI.aka.Container.prototype);

EHDI.scene.TitleScene.prototype.screenWillAppear = function() {
    this.bg = new EHDI.aka.Sprite(EHDI.Assets.images["title_bg"]);
    this.frame = new EHDI.aka.Sprite(EHDI.Assets.images["title_frame"]);
    this.addChild(this.bg);
    this.timeDone = false;
};

EHDI.scene.TitleScene.prototype.downHandler = function(){
  if(!this.timeDone){
    this.tl.seek("skip");
    this.addStuff();
    this.interactive = false;
  }
};

EHDI.scene.TitleScene.prototype.screenDidAppear = function() {
  this.interactive = true;
  this.touchstart = this.mousedown = this.downHandler;
  this.cards = [];
  this.tlCard = new TimelineMax();
  this.tlCSpin = new TimelineMax({repeat: 16, yoyo: true});
  for(var i = 0; i < 6; i++){
    this.cards[i] = new EHDI.aka.Sprite(EHDI.Assets.images["title_card" + (i+1)]);
    this.cards[i].anchor.set(0.5,0.5);
    this.addChild(this.cards[i]);
  }

  this.cards[0].position.set(200,200);
  this.cards[1].position.set(130,332);
  this.cards[2].position.set(245,475);
  this.cards[3].position.set(883,205);
  this.cards[4].position.set(837,335);
  this.cards[5].position.set(902,475);

  for(var i = 0; i < 6; i++){
    this.tlCSpin.from(this.cards[i].scale, 0.15, {x: -1, ease: Power2.easeInOut}, 0);
    this.tlCard.from(this.cards[i], 0.2, {y: 700, ease: Power4.easeInOut}, "+=0.1");
  }

  this.tlCard.add(this.tlCSpin, 0.05);

  this.radial = new EHDI.aka.Sprite(EHDI.Assets.images["title_radial"]);
  this.radial.anchor.set(0.5, 0.5);
  this.radial.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.5,EHDI.GAME.sceneManager.getStageHeight() * 0.5);
  this.addChild(this.radial);

  this.particles = new EHDI.aka.Sprite(EHDI.Assets.images["title_particles"]);
  this.particles.anchor.set(0.5, 0.5);
  this.particles.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.5,EHDI.GAME.sceneManager.getStageHeight() * 0.5);
  this.addChild(this.particles);

  this.gizmo = new EHDI.aka.Sprite(EHDI.Assets.images["title_gizmo"]);
  this.gizmo.position.set(579,211);
  this.addChild(this.gizmo);

  this.joy = new EHDI.aka.Sprite(EHDI.Assets.images["title_joy"]);
  this.joy.position.set(198,181);
  this.addChild(this.joy);

  this.chris = new EHDI.aka.Sprite(EHDI.Assets.images["title_chris"]);
  this.chris.position.set(354,107);
  this.addChild(this.chris);

  this.title1 = new EHDI.aka.Sprite(EHDI.Assets.images["title_titlecard1"]);
  this.title1.anchor.set(0.5,0.5);
  this.title1.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.5, 123);
  this.addChild(this.title1);

  this.title2 = new EHDI.aka.Sprite(EHDI.Assets.images["title_titlecard2"]);
  this.title2.anchor.set(0.5,0.5);
  this.title2.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.5, 198);
  this.addChild(this.title2);

  this.addChild(this.frame)

  this.tl = new TimelineMax();

  // var addStuff = this.addStuff.bind(this)

  this.tl.add(this.tlCard, "+=0")
         .addLabel("tada", "+=0")
         .from(this.particles.scale, 0.6, {x: 0, y: 0, ease: Power4.easeOut}, "tada")
         .from(this.radial.scale, 0.3, {x: 0, y: 0, ease: Power3.easeOut}, "tada")
         .addLabel("tadone", "+=0.05")
         .from(this.chris, 0.2, {y: 600, ease: Power4.easeOut}, "tadone")
         .from(this.gizmo, 0.2, {x: 1224, ease: Power4.easeOut}, "tadone")
         .from(this.joy, 0.2, {x: -200, ease: Power4.easeOut}, "tadone")
         .from(this.title1.scale, 0.3, {x: 0, y: 0, ease: Power3.easeIn}, "tadone")
         .from(this.title2.scale, 0.3, {x: 0, y: 0, ease: Power3.easeIn}, "tadone")
         .addCallback(this.addStuff, "=+0", null, this)
         .addLabel("skip", "+=0")

  this.sound = new EHDI.displays.ToggleButton(EHDI.Assets.images["btn_audio1"], EHDI.Assets.images["btn_audio3"], EHDI.Assets.images["btn_audio2"], EHDI.Assets.images["btn_audio4"], EHDI.GAME.soundManager.getMuted());
  this.sound.setOnClickFunction(this.toggleAudio);
  this.sound.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.12 - 3, EHDI.GAME.sceneManager.getStageHeight() * 0.1);

  this.playBtn = new EHDI.displays.Button(EHDI.Assets.images["btn_play"], EHDI.Assets.images["btn_play2"], null, null);

  this.playBtn.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.51, EHDI.GAME.sceneManager.getStageHeight() * 0.88);

  this.playBtn.setOnClickFunction(this.startGame);

  this.playTxt = new EHDI.aka.PixiText("PLAY", {fontFamily: 'proximanova-black', fill: 0xFFFFFF, fontSize : 32});

  this.playTxt.anchor.set(0.75, 0.5);

  // if(!EHDI.GAME.debugUtils) {
  //     EHDI.GAME.debugUtils = new EHDI.debugUtils.debugUtilsContainer("v0.2.5");
  //     EHDI.GAME.sceneManager.addNotification(EHDI.GAME.debugUtils);
  // }
};

EHDI.scene.TitleScene.prototype.addStuff = function(){
  this.timeDone = true;
  EHDI.GAME.soundManager.playBGM("bgm_parabuilder");
  this.addChild(this.sound);
  this.highScoreHolder = new EHDI.components.HighScoreHolder(this, EHDI.GAME.saveData.highScore);
  this.highScoreHolder.setXY(EHDI.GAME.sceneManager.getStageWidth() * 0.715 + 2, EHDI.GAME.sceneManager.getStageHeight() * 0.05);
  this.addChild(this.playBtn);
  this.playBtn.addChild(this.playTxt);
  this.tlPlay = new TimelineMax({repeat: -1, yoyo: true});
  this.tlPlay.to( this.playBtn.scale, 0.2, {x: 0.96, y: 0.96, ease: Power4.easeInOut});

  this.tlCard.kill();
  this.tlCSpin.kill();

  this.tlCard = new TimelineMax({repeat: -1, repeatDelay: 0.2, yoyo: true});
  this.tlCSpin = new TimelineMax();
  for(var i = 0; i < 3; i++){
      this.tlCSpin.addLabel("done"+i, "+=0")
                  .to(this.cards[i].scale, 0.15, {x: -1, ease: Power2.easeInOut, repeat: 5, yoyo: true}, "done"+i)
                  .to(this.cards[5-i].scale, 0.15, {x: -1, ease: Power2.easeInOut, repeat: 5, yoyo: true}, "done"+i)
  }
  this.tlCard.add(this.tlCSpin, "+=1");
}

EHDI.scene.TitleScene.prototype.toggleAudio = function(enable) {
    EHDI.GAME.soundManager.setMute(enable);
    EHDI.GAME.soundManager.playSFX("button_sfx");
    var cache = EHDI.GAME.storageManager.getLocalInfo(EHDI.GAME.id);
    cache.isMuted = enable;
    EHDI.GAME.storageManager.setLocalInfo(EHDI.GAME.id, enable);
}

EHDI.scene.TitleScene.prototype.startGame = function() {
    EHDI.GAME.soundManager.playSFX("button_sfx");
		EHDI.GAME.sceneManager.changeScene(new EHDI.GAME.scene.GameScene(), {x : new EHDI.scene.TransitionParameter(-EHDI.GAME.sceneManager.getStageWidth(), 0), duration : 0.25});
    // EHDI.GAME.sceneManager.changeScene(new EHDI.scene.GameScene(), {x : new EHDI.scene.TransitionParameter(-EHDI.GAME.sceneManager.getStageWidth(), 0), duration : 0.25});
};

EHDI.scene.TitleScene.prototype.screenDidDisappear = function() {
    this.tlCard.kill();
    this.tlCSpin.kill();
    this.tl.kill();
    this.tlPlay.kill();
    while(this.cards.length > 0){
      var pop = this.cards.pop();
      pop.destroy();
    }
    this.destroy({children: true});
}