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

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

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

EHDI.scene.TitleScene.doneTransition = false;

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

EHDI.scene.TitleScene.prototype.screenWillAppear = function() {
    //var bg = new PIXI.extras.TilingSprite(EHDI.Assets.images["gfx_bg"], EHDI.GAME.sceneManager.getStageWidth(), EHDI.GAME.sceneManager.getStageHeight());
    var bg = new EHDI.aka.Sprite(EHDI.Assets.images["title_gf_bg"])
    this.seabed = new EHDI.aka.Sprite(EHDI.Assets.images["title_gf_seabed"])
    this.seabed.y = bg.height - this.seabed.height;
    this.addChild(bg);
    this.addChild(this.seabed);
    this.seabed.alpha = 0;

    if(!ITW.isIP4){
      this.fishes = [];
      for(var i = 0; i < 6; i++){
        var num = i % 2;
        this.fishes[i] = new EHDI.aka.Sprite(EHDI.Assets.images["title_gf_fish_small" + num])
        this.fishes[i].x = Math.random() * ITW.SceneMgr.getStageWidth();
        this.fishes[i].y = Math.random() * ITW.SceneMgr.getStageHeight() * 0.75;
      }

      var loop = function( dt ){
        for(var i = 0; i < this.fishes.length; i++){
          this.fishes[i].x -= 0.2 * dt;
          if(this.fishes[i].x < 0){
            this.fishes[i].x = ITW.SceneMgr.getStageWidth() + this.fishes[i].width;
            this.fishes[i].y = Math.random() * ITW.SceneMgr.getStageHeight() * 0.75;
          }
        }
      }
      this.bindedloop = loop.bind(this);

      ITW.UpdateMgr.addFrameListener(this.bindedloop);
    }
};

EHDI.scene.TitleScene.prototype.screenDidAppear = function() {
  if(!ITW.isIP4){
    for(var i = 0; i < this.fishes.length; i++){
      this.addChild(this.fishes[i]);
    }
  }

  this.gfish = new EHDI.aka.Sprite(EHDI.Assets.images["title_gf_fish_big"]);

  this.gfish.x = 128;
  this.gfish.y = 114;
  this.seabed.alpha = 1;
  this.addChild(this.gfish);

  if(!ITW.isIP4){
    this.bubbles = [];
    for(var i = 0; i < 6; i++){
      var num = i % 2;
      this.bubbles[i] = new EHDI.aka.Sprite(EHDI.Assets.images["title_gf_bubbles" + num]);
      this.bubbles[i].y = 660;
      this.addChild(this.bubbles[i]);
    }

    this.tlBubbles = new TimelineMax({repeat: -1});
    var relocateBubble = function(bubble){
      bubble.x = Math.random() * ITW.SceneMgr.getStageWidth();
    }
    for(var i = 0; i < this.bubbles.length; i ++){
      this.bubbles[i].x = Math.random() * ITW.SceneMgr.getStageWidth();
      this.tlBubbles.to(this.bubbles[i], 3, {y: -this.bubbles[i].height , alpha: 0.3, ease: Power3.easeOut,
        onComplete: relocateBubble, onCompleteParams: [this.bubbles[i]]}, i*0.2);
    }
  }

  this.fgReefLeft = new EHDI.aka.Sprite(EHDI.Assets.images["title_gf_fgreef_left"]);
  this.fgReefLeft.y = ITW.SceneMgr.getStageHeight() - this.fgReefLeft.height;

  this.addChild(this.fgReefLeft);

  this.fgReefRight = new EHDI.aka.Sprite(EHDI.Assets.images["title_gf_fgreef_right"]);
  this.fgReefRight.x = ITW.SceneMgr.getStageWidth() - this.fgReefRight.width;
  this.fgReefRight.y = ITW.SceneMgr.getStageHeight() - this.fgReefRight.height;

  this.addChild(this.fgReefRight);

  this.txtTitle1 = new EHDI.aka.Sprite(EHDI.Assets.images["title_gf_txt_the"]);
  this.txtTitle1.x = ( ITW.SceneMgr.getStageWidth() - this.txtTitle1.width ) * 0.5;
  this.txtTitle1.y = ( ITW.SceneMgr.getStageHeight() - this.txtTitle1.height ) * 0.25;

  this.addChild(this.txtTitle1);

  this.txtTitle2 = new EHDI.aka.Sprite(EHDI.Assets.images["title_gf_txt_greatfish"]);
  this.txtTitle2.x = ( ITW.SceneMgr.getStageWidth() - this.txtTitle2.width ) * 0.5;
  this.txtTitle2.y = this.txtTitle1.y + 106;

  this.addChild(this.txtTitle2);

  if(!EHDI.scene.TitleScene.doneTransition){
    this.tl = new TimelineMax({onComplete: this.onAnimationDone, callbackScope: this});
    this.tl.from(this.seabed, 2, {y: 490, ease: Power2.easeOut})
           .addCallback(ITW.SoundMgr.playBGM,"+=0",["thegf_bgm", 0.6])
           .from(this.fgReefLeft, 2, {y: 490, ease: Power2.easeOut}, 0)
           .from(this.fgReefRight, 2, {y: 490, ease: Power2.easeOut}, 0);
    this.tl.from(this.txtTitle1, 0.4, {alpha: 0}, 2.1)
           .from(this.txtTitle2, 0.4, {alpha: 0}, 2.1)
  }else{
    ITW.SoundMgr.playBGM("thegf_bgm", 0.6);
    this.onAnimationDone();
  }

  /*
  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.onAnimationDone = function(){
  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.1, EHDI.GAME.sceneManager.getStageHeight() * 0.09);

  this.highScoreHolder = new EHDI.components.HighScoreHolder(this, EHDI.GAME.saveData.highScore);

  this.highScoreHolder.setXY(EHDI.GAME.sceneManager.getStageWidth() * 0.723, EHDI.GAME.sceneManager.getStageHeight() * 0.0417);

  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.5, EHDI.GAME.sceneManager.getStageHeight() * 0.85);

  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);

  this.addChild(this.sound);
  this.addChild(this.playBtn);
  this.playBtn.addChild(this.playTxt);
  if(EHDI.scene.TitleScene.doneTransition) return;
  EHDI.scene.TitleScene.doneTransition = true;
}

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 ITW.scene.GameScene(), {alpha : new EHDI.scene.TransitionParameter(0, 1), duration : 0.25});
};

EHDI.scene.TitleScene.prototype.screenDidDisappear = function() {
  // this.tl.stop();
	if(this.tl) this.tl.kill();
	this.playTxt.destroy();
	this.playBtn.dispose();
  this.highScoreHolder.dispose();
  if(this.bindedloop) ITW.UpdateMgr.removeFrameListener(this.bindedloop);
  if(this.tlBubbles) this.tlBubbles.kill();
  if(!ITW.isIP4){
    while(this.bubbles.length > 0){
      this.bubbles.pop().destroy({children: true});
    }
    while(this.fishes.length > 0){
      this.fishes.pop().destroy({children: true});
    }
  }
	this.destroy({children: true});
}