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.prototype = Object.create(EHDI.aka.Container.prototype);

EHDI.scene.TitleScene.prototype.screenWillAppear = function() {
  this.bg = new EHDI.aka.Sprite(EHDI.Assets.images["title_sheepprotector_bg"]);
  DSP.DBoneFactory.enableClock = true;
  this.addChild(this.bg);
  this.timeDone = false;

  // alert(EHDI.checkDeviceQuality(DSP.SceneMgr.getRenderer()));
};

EHDI.scene.TitleScene.prototype.downHandler = function(){
  if(!this.timeDone){
    this.tl.seek("skip");
    DSP.SoundMgr.playBGM("gizmo_bgm_01", 0.6);
    this.onAnimationDone();
    this.interactive = false;
  }
}

EHDI.scene.TitleScene.prototype.screenDidAppear = function() {
  this.interactive = true;
  this.touchstart = this.mousedown = this.downHandler;

  this.sheep = new EHDI.aka.Sprite(EHDI.Assets.images["title_sheepprotector_sheep"]);
  this.sheep.y = DSP.SceneMgr.getStageHeight() - this.sheep.height;
  this.addChild(this.sheep);

  this.david = new EHDI.aka.Sprite(EHDI.Assets.images["title_sheepprotector_dav"]);
  this.david.anchor.set(0.1, 0.9);
  this.david.x = 30;
  this.david.y = DSP.SceneMgr.getStageHeight() + this.david.height * 0.03;
  this.addChild(this.david);

  this.title1 = new EHDI.aka.Sprite(EHDI.Assets.images["title_sheepprotector_titlecard1"]);
  this.title1.x = (DSP.SceneMgr.getStageWidth() - this.title1.width) * 0.5;
  this.title1.y = (DSP.SceneMgr.getStageHeight() - this.title1.height) * 0.42;

  this.title2 = new EHDI.aka.Sprite(EHDI.Assets.images["title_sheepprotector_titlecard2"]);
  this.title2.x = (DSP.SceneMgr.getStageWidth() - this.title2.width) * 0.5;
  this.title2.y = this.title1.y + this.title1.height * 0.65;

  this.logo = new EHDI.aka.Sprite(EHDI.Assets.images["title_sheepprotector_logo"]);
  this.logo.x = (DSP.SceneMgr.getStageWidth() - this.logo.width) * 0.5;
  this.logo.y = this.title1.y - this.logo.height * 0.45;

  this.glow = new EHDI.aka.Sprite(EHDI.Assets.images["title_sheepprotector_glow"]);
  this.glow.x = (DSP.SceneMgr.getStageWidth() - this.glow.width) * 0.5;
  this.glow.y = (DSP.SceneMgr.getStageHeight() - this.glow.height) * 0.46;
  this.addChild(this.glow);
  this.addChild(this.logo);
  this.addChild(this.title1);
  this.addChild(this.title2);

  this.tl = new TimelineMax({onComplete: this.onAnimationDone, callbackScope: this});

  this.tl.from(this.sheep, 1.2, {x: -this.sheep.width, ease: SlowMo.ease.config(0.1,0.7, false)})
         .addCallback(DSP.SoundMgr.playVO, "+=0.3", ["sfx_sheep"])
         .from(this.david, 1, {x: -this.david.width, ease: Power4.easeOut}, "1.15")
         .addCallback(DSP.SoundMgr.playBGM, "+=0", ["gizmo_bgm_01", 0.6])
         .from(this.logo, 0.5, {alpha: 0}, "2.2")
         .from(this.title1, 0.5, {alpha: 0}, "2.2")
         .from(this.title2, 0.5, {alpha: 0}, "+=0.2")
         .from(this.glow, 0.5, {alpha: 0}, "-=0.7")
         .addLabel("skip", "+=0")

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

EHDI.scene.TitleScene.prototype.onAnimationDone = function(){
  this.timeDone = true;
  this.tl.kill();
  this.tl = new TimelineMax();

  this.tlSheep = new TimelineMax({repeat: -1, yoyo: true});
  this.tlSheepShake = new TimelineMax({repeat: 8, yoyo: true});
  this.tlSheepShake.to(this.sheep, 0.07, {x: this.sheep.x + 3, ease: Power4.easeInOut})
  this.tlSheep.add(this.tlSheepShake, "+=0.2");

  this.tlDavid = new TimelineMax({repeat: -1, yoyo: true})
  this.tlDavid.to(this.david, 1, {rotation: -0.02, ease: Power1.easeInOut});

  this.tl.add(this.tlSheep)
         .add(this.tlDavid, 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.1, EHDI.GAME.sceneManager.getStageHeight() * 0.09);

  this.addChild(this.sound);

  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.addChild(this.playBtn);

  this.playBtn.setOnClickFunction(this.startGame);

  this.playTxt = new EHDI.displays.TextSprite(EHDI.GAME.JSONMgr.getLocale("BTN_PLAY"));

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

  this.playBtn.addChild(this.playTxt);
}

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

EHDI.scene.TitleScene.prototype.screenDidDisappear = function() {
  this.tlDavid.kill();
  this.tlSheepShake.kill();
  this.tlSheep.kill();
  this.tl.kill();
  this.touchstart = this.mousedown = null;
  this.playTxt.destroy();
  this.playBtn.dispose();
  this.destroy({children: true});
}