var textR;
var TIME_END_SCREEN = 2000;

var GameMgr = {
    preload: function() {
        console.log('game prepared');
        WorldMgr.prepare();
        gameData.stateGame = stateGame.NOTHING;
        gameData.timeStartState = 0;
    },
    
    create: function() {
        console.log('created');
        WorldMgr.init();
        
        //Add static background
        game.add.sprite(0, 0, 'background');
        game.add.sprite(0, 330, 'roi');
        game.add.sprite(0, MAX_SCREEN_Y - game.cache.getImage('castle').height, 'castle');
        game.add.sprite(MAX_SCREEN_X /2 - 80, 25, 'dragonCorpse');
        game.add.sprite((1280-600)/2, 15, 'life');
        gameData.life_barre = game.add.sprite((1280-553)/2+12, 15, 'life_in');
        game.add.sprite((1280-600)/2, 15, 'diamondLife');
        myCache.dragonHead = game.add.sprite(MAX_SCREEN_X /2 - 60, 120, 'dragonHeadStand', 1);
        myCache.dragonHead.anchor.setTo(0.5,0.5);
        //myCache.dragonHead.scale.set(0.5);
        myCache.dragonHead.smoothed = false;
        myCache.dragonHead.visible = false;
        myCache.dragonHead.bringToTop();
        myCache.dragonHead.visible = true;
        anim = myCache.dragonHead.animations.add('stand');
        
        anim.play(25, true);
        
        WorldMgr.createColumns();
        
        bmd = game.make.bitmapData(MAX_SCREEN_X, MAX_SCREEN_Y);
        bmd.fill(0, 0, 0, 0);
        blackScreen = game.add.sprite(0, 0, bmd);
        
        prepareKing();
        
        sound_epee = game.add.audio('epee');
        sound_arrow = game.add.audio('fleche');
        sound_arrow_leg = game.add.audio('fleche_leg');
        sound_Clairon = game.add.audio('Clairon');
        sound_FireBall = game.add.audio('FireBall');
        sound_hmm = game.add.audio('hmm');
        sound_hmm2 = game.add.audio('hmm2');
        sound_Explosion = game.add.audio('Explosion');
        sound_cri = game.add.audio('Cri');
    },

    update: function() {
        updateKing(game.time.elapsed);
        music_menu.volume = music_menu.volume - 0.01;
        if (music_menu.volume <= 0 && !MusicDestroyed) {
            music_menu.destroy();
            music_game_calm = game.add.audio('sound_game_calm', 1, true);
            music_game_calm.play();
            music_game_calm.volume = 0.3;
            MusicDestroyed = true;
        }
        gameData.timeStartState += game.time.elapsed; // update time count
        switch (gameData.stateGame) {
            case stateGame.NOTHING:
                if (gameData.timeStartState > 1000) {
                    printAssault();
                }
                break;
            case stateGame.PRINTSTART_R:
                if (gameData.timeStartState > 3000 && gameData.timeStartState <= 4000) {
                    textR.alpha = 1 - (gameData.timeStartState - 3000) / 1000;
                }
                if (gameData.timeStartState > 5000) {
                    if (textR != null) {
                        textR.destroy();
                        textR = null;
                    }
                    gameData.stateGame = stateGame.ROUND;
                    gameData.timeStartState = 0;
                    gameData.userPlaying = true;
                    WorldMgr.create3Archers();
                    if (gameData.round == 1)
                        King_say(translation.get(texts.NPC_TXT_SALUTATIONS), 12000, 12);
                }
                break;
            case stateGame.ROUND:
                if (gameData.timeStartState > 12000 && gameData.timeStartState < 12060 && gameData.round == 1)
                    King_say(translation.get(texts.NPC_TXT_START), 10000, 12);
                else if (gameData.timeStartState > 2000 && gameData.timeStartState < 2060 && gameData.round == 2) {
                    King_say(translation.get(texts.NPC_TXT_START_ROUND_2), 10000, 12);
                } else if (gameData.timeStartState > 2000 && gameData.timeStartState < 2060 && gameData.round == 3) {
                    King_say(translation.get(texts.NPC_TXT_START_ROUND_3), 10000, 12);
                }
                if (gameData.userPlaying == false) {
                    gameData.stateGame = stateGame.R_FIGHT_DRAGON;
                    gameData.timeStartState = 0;
                    textR = game.add.text(MAX_SCREEN_X/2 ,MAX_SCREEN_Y/2, translation.get('FIGHT !'), game_text_style);
                    textR.anchor.setTo(0.5);
                    if (gameData.round == 1)
                        King_say(translation.get(texts.NPC_TXT_ATTAQUE_D), 4000, 14);
                    sound_epee.play();
                }
                break;
            case stateGame.R_FIGHT_DRAGON:
                if (gameData.timeStartState > 3000 && gameData.timeStartState <= 4000) {
                    textR.alpha = 1 - (gameData.timeStartState - 3000) / 1000;
                }
                if (gameData.timeStartState > 5000) {
                    if (textR != null) {
                        textR.destroy();
                        textR = null;
                    }
                    if (WorldMgr.fireDragon()) {
                        gameData.stateGame = stateGame.R_FIGHT_ARCHER;
                        gameData.timeStartState = 0;
                        if (gameData.round == 1)
                            King_say(translation.get(texts.NPC_TXT_ATTAQUE_A), 4000, 14);
                    }
                }
                break;
            case stateGame.R_FIGHT_ARCHER:
                if (gameData.timeStartState > 1000) {
                    if (WorldMgr.fireArcher()) {
                        gameData.stateGame = stateGame.R_FIGHT_ARCHER_LEGENDARY;
                        gameData.timeStartState = 0;
                        if (exist_leg(gameData.columns) && gameData.round == 1)
                            King_say(translation.get(texts.NPC_TXT_LEGENDARY), 5000, 14);
                    }
                }
                break;
            case stateGame.R_FIGHT_ARCHER_LEGENDARY:
                if (gameData.timeStartState > 1000) {
                    if (WorldMgr.fireArcherLegendary()) {
                        if (gameData.round == 3) {
                            printEndGame();
                            gameData.stateGame = stateGame.END_GAME;
                            gameData.timeStartState = 0;
                            this.timeLoadOk = TIME_END_SCREEN;
                        } else {
                            gameData.round += 1;
                            printAssault();
                        }
                    }
                }
                break;
            case stateGame.END_GAME:
                if (gameData.timeStartState > 5000 && gameData.timeStartState < 5000 + TIME_END_SCREEN) {
                    this.timeLoadOk -= game.time.elapsed;
                    var alpha = 0.8 - (0.8/TIME_END_SCREEN * this.timeLoadOk);
                    blackScreen.bringToTop();
                    bmd.clear();
                    bmd.fill(0,0,0, alpha);
                } else if (gameData.timeStartState > 5000 + TIME_END_SCREEN) {
                    if (montant >= 1000) {
                        coffre = game.add.sprite(MAX_SCREEN_X/2, MAX_SCREEN_Y/2, 'coffrePlein');
                    } else if (montant != 0) {
                        coffre = game.add.sprite(MAX_SCREEN_X/2, MAX_SCREEN_Y/2, 'coffreMiPlein');
                    } else {
                        coffre = game.add.sprite(MAX_SCREEN_X/2, MAX_SCREEN_Y/2, 'coffreVide');
                    }
                    coffre.anchor.set(0.5,0.5);
                    var text_End = game.add.text(MAX_SCREEN_X/2, MAX_SCREEN_Y/2 - 130, montant + '€',  { font: "bold 56px Wood_Stevens_Bold", fill: "#ffcc00", boundsAlignH: "center", boundsAlignV: "middle" });
                    text_End.anchor.set(0.5,0.5);
                }
                if (gameData.timeStartState > 15000) {
                    music_game_calm.destroy();
                    MusicDestroyed = false;
                    //redirection, donc pas de reset nécessaire
                    //gameData = new GameData();
                    document.location.href="/dragon.archers/";
                }
                break;
            default:
                mLog('Hors event détecté');
        }
        WorldMgr.update();
    }
    
};

function printAssault() {
    gameData.stateGame = stateGame.PRINTSTART_R;
    gameData.timeStartState = 0;
    if (gameData.round == 1)
        textR = game.add.text(MAX_SCREEN_X/2 ,MAX_SCREEN_Y/2, translation.get('Assault 1'), game_text_style);
    else if (gameData.round == 2)
        textR = game.add.text(MAX_SCREEN_X/2 ,MAX_SCREEN_Y/2, translation.get('Assault 2'), game_text_style);
    else
        textR = game.add.text(MAX_SCREEN_X/2 ,MAX_SCREEN_Y/2, translation.get('Assault 3'), game_text_style);
    textR.anchor.setTo(0.5);
}

function printEndGame() {
    gameData.stateGame = stateGame.END_GAME;
    gameData.timeStartState = 0;
    textR = game.add.text(MAX_SCREEN_X/2 ,MAX_SCREEN_Y/2, translation.get('End of fight !'), game_text_style);
    textR.anchor.setTo(0.5);
    sound_Clairon.play();
    if (gameData.dragonLife <= 0)
        King_say(translation.get(texts.NPC_TXT_VICTORY), 5000, 14);
    else if (gameData.dragonLife <= 60)
        King_say(translation.get(texts.NPC_TXT_MIDDLE_WIN), 5000, 14);
    else if (gameData.dragonLife <= 100)
        King_say(translation.get(texts.NPC_TXT_LOSE), 5000, 14);
}