module Fiche {
    export class Fiche extends Base.BasicGroup {
        private GameData: any;
        private currentFiche: number;
        private currentAudio: HTMLAudioElement;
        private FicheImage: Phaser.Image;
        private Description: Phaser.Text;
        private Background: Phaser.Image;
        private Loader: Phaser.Loader;
        private isPolish: boolean;
        private BackgroundWidth: number;
        private BackgroundStartingPosition: number;

        constructor(game, name, x, y, json) {
            super(game, name, x, y, json);
            this.currentFiche = 1;
        }
        init(json) {
            super.init(json);
            this.GameData = json.GameData;
        }
        preload() {
            super.preload();
            this.game.load.onLoadComplete.addOnce(this.create, this);
            this.loadGraphics();
            this.game.load.start();
        }

        create() {
            if (this.json.isStandalone) {
                super.create();
            }
            this.isPolish = true;
            this.Loader = new Phaser.Loader(this.game);
            this.Background = this.game.add.image(50, -150, "bck");
            var prev = this.game.add.button(120, 820, "prevF", this.prevFiche, this, 0, 0, 1, 0);
            var next = this.game.add.button(1150, 820, "nextF", this.nextFiche, this, 0, 0, 1, 0);
            var fSpeaker = this.game.add.button(100, 50, "fSpeaker", this.readLector, this, 0, 0, 1, 0);
            var Assistant = this.game.add.button(1350, 300, "assistant", this.assistantClick, this, 0, 0, 0, 0);
            var flip = this.game.add.button(1150, 70, "flip", this.flip, this, 0, 0, 1, 0);
            this.FicheImage = this.game.add.image(300, 100, "firstImage");
            this.FicheImage.x = (this.Background.x + this.Background.width / 2) - this.FicheImage.width / 2 -50;
            this.Description = this.game.add.text(400, 720, this.GameData.words[0].term_PL, { font: "30pt Arial", wordWrap: true, wordWrapWidth: this.FicheImage.width * 2 }, this);

            this.Description.x = (this.Background.x + this.Background.width / 2) - this.Description.width / 2 - 50;
            this.Description.addColor("#2d842d", 0);
            this.addChild(this.Background);
            
            this.addChild(Assistant);
            this.Background.addChild(fSpeaker);
            this.Background.addChild(flip);
            this.Background.addChild(prev);
            this.Background.addChild(next);
            this.Background.addChild(this.Description);
            this.Background.addChild(this.FicheImage);
            this.BackgroundWidth = this.Background.width;
            this.BackgroundStartingPosition = this.Background.x;

        }
        assistantClick() {
        }
        animateFlip() {           
            var tw = this.game.add.tween(this.Background);
            tw.to({ width: this.BackgroundWidth,x:this.BackgroundStartingPosition}, 700, Phaser.Easing.Exponential.Out);
            tw.onComplete.add(function () { this.game.input.enabled = true; }, this);
            tw.start();
            this.readLector();
        }
        flip() {
            this.game.input.enabled = false;
            var tw = this.game.add.tween(this.Background);
            tw.to({ width: 0,x:this.Background.x+this.Background.width/2 }, 500, Phaser.Easing.Exponential.Out);
            tw.onComplete.add(this.animateFlip, this);
            tw.start();
            if (this.isPolish) {
                this.Description.setText(this.GameData.words[this.currentFiche-1].term_EN);
                this.isPolish = false;
            }
            else {
                this.Description.setText(this.GameData.words[this.currentFiche-1].term_PL);
                this.isPolish = true;
            }
            this.Description.x = (this.Background.x + this.Background.width / 2) - this.Description.width / 2 -50;
        }
        readLector() {
            var fNumber = this.currentFiche;
            if (fNumber > 1)
                fNumber - 1;
            if(this.isPolish)
                this.currentAudio = new Audio("Sounds/" + MODULE + "/"+ CardChooser.getCardNum().match(/\d+/) +"_" + fNumber + "_PL.mp3");
            else
                this.currentAudio = new Audio("Sounds/" + MODULE + "/" + CardChooser.getCardNum().match(/\d+/) +"_" + fNumber + "_EN.mp3");           
            Global.Main.PlayMusic(this.currentAudio);
        }
        nextFiche() {
            this.isPolish = true;
            if (this.currentFiche < this.GameData.FicheQuantity)
                this.currentFiche++;
            this.readLector();
            this.Description.setText(this.GameData.words[this.currentFiche - 1].term_PL);
            this.Description.x = (this.Background.x + this.Background.width / 2) - this.Description.width / 2-50;
            this.loadFiche();
        }
        prevFiche() {
            this.isPolish = true;
            if (this.currentFiche > 0)
                this.currentFiche--;
            this.readLector();
            this.Description.setText(this.GameData.words[this.currentFiche - 1].term_PL);
            this.Description.x = (this.Background.x + this.Background.width / 2) - this.Description.width / 2-50;
            this.loadFiche();
        }
        loadFiche() {
            this.Loader.image(this.currentFiche + "", "Graphics/Games/" + MODULE + "/" + CardChooser.getCardNum().match(/\d+/) + "_" + this.currentFiche + ".jpg");
            this.Loader.onLoadComplete.addOnce(this.setFiche, this);
            this.Loader.start();
        }
        setFiche() {
            this.FicheImage.loadTexture(this.currentFiche, 0);
        }
        loadGraphics() {
            this.game.load.image("bck", "Graphics/GUI/" + CardChooser.getModule() + "/" + "modul1.png");
            this.game.load.spritesheet("prevF", "Graphics/GUI/OS/prev1.png", 100, 100, 2);
            this.game.load.spritesheet("flip", "Graphics/GUI/OS/flipButton1.png", 115, 115, 2);
            this.game.load.spritesheet("fSpeaker", "Graphics/GUI/OS/speaker1.png", 115, 115, 2);
            this.game.load.image("firstImage", "Graphics/Games/" + MODULE + "/" + CardChooser.getCardNum().match(/\d+/) +"_1.jpg");
            this.game.load.spritesheet("nextF", "Graphics/GUI/OS/next1.png", 100, 100, 2);
            this.game.load.spritesheet("assistant", "Graphics/GUI/" + CardChooser.getModule() + "/Assistant.png",480,560,1);
        }


    }
}