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() {
    var bg = new EHDI.aka.Sprite(EHDI.Assets.images["title_gm_bg"]);
    this.addChild(bg);
};

EHDI.scene.TitleScene.prototype.screenDidAppear = function() {
    EHDI.GAME.soundManager.sfxVolume = 0.5;

    this.hexagonLeft = new EHDI.aka.Sprite(EHDI.Assets.images["title_gm_hexagons_left"]);
    this.hexagonLeft.anchor.set(0.5, 0.5);
    this.hexagonLeft.alpha = 0;
    this.hexagonLeft.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.35, EHDI.GAME.sceneManager.getStageHeight() * 0.5);

    this.addChild(this.hexagonLeft);

    this.hexagonRight = new EHDI.aka.Sprite(EHDI.Assets.images["title_gm_hexagon_right"]);
    this.hexagonRight.anchor.set(0.5, 0.5);
    this.hexagonRight.alpha = 0;
    this.hexagonRight.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.65, EHDI.GAME.sceneManager.getStageHeight() * 0.5);
    this.addChild(this.hexagonRight);

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

    this.titleContainer = new EHDI.aka.Container();
    this.addChild(this.titleContainer);
    this.titleContainer.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.5, EHDI.GAME.sceneManager.getStageHeight() * 0.5);

    this.gizmo = new EHDI.aka.Sprite(EHDI.Assets.images["title_gm_txt_gizmo"]);

    this.gizmo.anchor.set(0.5, 0.5);
    this.gizmo.position.set(0, -EHDI.GAME.sceneManager.getStageHeight() * 0.1);
    this.titleContainer.addChild(this.gizmo);

    this.matches = new EHDI.aka.Sprite(EHDI.Assets.images["title_gm_txt_matches"]);
    this.matches.anchor.set(0.5, 0.5);
    this.matches.position.set(0, EHDI.GAME.sceneManager.getStageHeight() * 0.05);
    this.titleContainer.addChild(this.matches);



    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.visible = false;
    this.playBtn.alpha = 0;

    this.playBtn.setOnClickFunction(this.startGame.bind(this));

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

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

    this.playBtn.addChild(this.playTxt);

    this.entranceTimeline = new TimelineMax();

    this.entranceTimeline.from(this.explosion.scale, 0.5, {x : 0, y : 0, ease : Back.easeOut});
    this.entranceTimeline.from(this.titleContainer.scale, 0.35, {x : 0, y : 0, ease: Back.easeOut}, 0);
    this.entranceTimeline.add(this.startLoop.bind(this));
    this.entranceTimeline.to(this.playBtn, 0.1, {alpha : 1});


    this.createDragonBones();
    this.createAnimations();

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

    EHDI.GAME.soundManager.playBGM("gizmomatches_bgm", 0.5);

};

EHDI.scene.TitleScene.prototype.startLoop = function() {
    this.loopTimeline = new TimelineMax({repeat : -1, repeatDelay: 0.25});

    this.loopTimeline.to(this.hexagonLeft, 0.1, {alpha : 1});
    this.loopTimeline.to(this.hexagonLeft, 0.2, {alpha : 0}, "+=0.25");
    this.loopTimeline.to(this.hexagonRight, 0.1, {alpha :1}, "-=0.2");
    this.loopTimeline.to(this.hexagonRight, 0.2, {alpha : 0}, "+=0.25");

    EHDI.GAME.updateManager.addFrameListener(this.animateAnimations);

    this.playBtn.visible = true;

    this.playBtnTimeline = new TimelineMax({repeat : -1, repeatDelay : 1.5, delay : 3});
    this.playBtnTimeline.to(this.playBtn.scale, 0.1, {x : 1.1, y: 1.2});
    this.playBtnTimeline.to(this.playBtn.scale, 0.1, {x : 1, y: 1});
    this.playBtnTimeline.to(this.playBtn.scale, 0.1, {x : 1.1, y: 1.2});
    this.playBtnTimeline.to(this.playBtn.scale, 0.1, {x : 1, y: 1});

    this.playBtn.touchstart = (function(touchstart, titlescene) {
        return function() {
            titlescene.playBtn.scale.set(1, 1);
            titlescene.playBtnTimeline.pause();
            return touchstart.apply(this, arguments);
        }
    }(this.playBtn.touchstart, this));

    this.playBtn.mousedown = (function(mousedown, titlescene) {
        return function() {
            titlescene.playBtn.scale.set(1, 1);
            titlescene.playBtnTimeline.pause();
            return mousedown.apply(this, arguments);
        }
    }(this.playBtn.mousedown, this));

    this.playBtn.touchendoutside = (function(touchendoutside, titlescene) {
        return function() {
            titlescene.playBtnTimeline.restart()
            titlescene.playBtnTimeline.play();
            return touchendoutside.apply(this, arguments);
        }
    }(this.playBtn.touchendoutside, this));

    this.playBtn.mouseout = (function(mouseout, titlescene) {
        return function() {
            titlescene.playBtnTimeline.restart()
            titlescene.playBtnTimeline.play();
            return mouseout.apply(this, arguments);
        }
    }(this.playBtn.mouseout, this));
};

EHDI.scene.TitleScene.prototype.createDragonBones = function() {
    EHDI.GAME.dbFactory = new dragonBones.PixiFactory();

    var textureImage = EHDI.Assets.images["gizmomatch_tex"].baseTexture.source;
    var textureData = EHDI.Assets.cjAssets.getResult("gizmomatch_tex");

    EHDI.GAME.dbFactory.addTextureAtlas(new dragonBones.TextureAtlas(textureImage, textureData));

    var skeleton = EHDI.Assets.cjAssets.getResult("gizmomatch_ske");

    EHDI.GAME.dbFactory.addDragonBonesData(dragonBones.DataParser.parseDragonBonesData(skeleton));
};

EHDI.scene.TitleScene.prototype.animateAnimations = function(dt) {
    dragonBones.WorldClock.clock.advanceTime(dt * 0.001)
};

EHDI.scene.TitleScene.prototype.createAnimations = function(dt) {
    this.glow = EHDI.GAME.dbFactory.buildArmature("ambient_an");
    dragonBones.WorldClock.clock.add(this.glow);
    this.glow.animation.gotoAndPlay("glow", -1, -1, 0);

    this.glowSprite = this.glow.getDisplay();
    this.glowSprite.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.185, EHDI.GAME.sceneManager.getStageHeight() * 0.19);
    this.addChild(this.glowSprite);

    this.lowLeft = EHDI.GAME.dbFactory.buildArmature("ambient_an");
    dragonBones.WorldClock.clock.add(this.lowLeft);
    this.lowLeft.animation.gotoAndPlay("low_left_1", -1, -1, 0);

    this.lowLeftSprite = this.lowLeft.getDisplay();
    this.lowLeftSprite.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.0875, EHDI.GAME.sceneManager.getStageHeight() * 0.925);
    this.addChild(this.lowLeftSprite);

    this.lowLeft2 = EHDI.GAME.dbFactory.buildArmature("ambient_an");
    dragonBones.WorldClock.clock.add(this.lowLeft2);
    this.lowLeft2.animation.gotoAndPlay("low_left_2", -1, -1, 0);

    this.lowLeft2Sprite = this.lowLeft2.getDisplay();
    this.lowLeft2Sprite.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.233, EHDI.GAME.sceneManager.getStageHeight() * 0.9405);
    this.addChild(this.lowLeft2Sprite);

    this.lowRight = EHDI.GAME.dbFactory.buildArmature("ambient_an");
    dragonBones.WorldClock.clock.add(this.lowRight);
    this.lowRight.animation.gotoAndPlay("low_right", -1, -1, 0);

    this.lowRightSprite = this.lowRight.getDisplay();
    this.lowRightSprite.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.85, EHDI.GAME.sceneManager.getStageHeight() * 0.925);
    this.addChild(this.lowRightSprite);
};

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, cache);
}


EHDI.scene.TitleScene.prototype.startGame = function() {
    this.loopTimeline.kill();
    this.entranceTimeline.kill();
    this.playBtnTimeline.kill();
    EHDI.GAME.updateManager.removeFrameListener(this.animateAnimations);
    dragonBones.WorldClock.clock.clear();
    EHDI.GAME.soundManager.playSFX("button_sfx");
    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.destroy({children: true});
    delete this;
}