module TFQuiz {
    export class TFQuiz extends Base.BasicGroup {

        Elements: any;
        PREFIX: any;
        numberOfChecked: any;
        infoPremia: any;
        infoScore: any;
        numberOfCorrect: any;

        Questions: any[];

        Color: any;
        
        constructor(game, name, x, y, json) {
            super(game, name, x, y, json);
        }

        preload() {
            super.preload();
            this.game.load.onLoadComplete.addOnce(this.create, this);
            this.PREFIX = this.Elements.TaskPrefix;
            this.loadGraphics();
            this.game.load.start();
        }
        loadGraphics() {

            for (var i = 0; i < this.Elements.Graphics.length; i++)
                this.game.load.image(this.PREFIX + this.Elements.Graphics[i].key, this.Elements.Graphics[i].src);
        }

        create() {
            if (this.json.isStandalone) {
                super.create();
            }
            if (this.Elements.Color) {
                this.Color = this.Elements.Color;
            } else this.Color = "green";
            this.numberOfChecked = 0;
            this.createStaticElements();
            this.createQuestions();
            this.createInfo();
            EU.createSelectableElements(this);
        }

        init(json) {
            super.init(json);
            this.Elements = json.GameData;
        }

        createStaticElements() {
            if (this.Elements.StaticElements)
                for (var i = 0; i < this.Elements.StaticElements.el.length; i++) {
                    var el;
                    switch (this.Elements.StaticElements.el[i].type) {
                        case "image":
                            el = this.game.add.image(0, 0, this.PREFIX + this.Elements.StaticElements.el[i].key);
                            break;
                        case "text":
                            el = this.game.add.text(0, 0, this.Elements.StaticElements.el[i].text, this.Elements.StaticElements.Options.Style);
                            break;
                        case "sound":
                            el = this.game.add.sprite(0, 0, "speaker");
                            el.inputEnabled = true;
                            el.name = this.Elements.StaticElements.el[i].sound;
                            el.events.onInputUp.add(this.soundButtonDown, this);
                            break;
                    }
                    var posX = this.Elements.StaticElements.el[i].pos.x;
                    var posY = this.Elements.StaticElements.el[i].pos.y;
                    if (this.Elements.StaticElements.Options.Just) {
                        switch (this.Elements.StaticElements.Options.Just.x) {
                            case "center":
                                posX += (Global.Main.mainContainerDims.wd - 120) / 2;
                                el.anchor.x = 0.5;
                                break;
                            case "right":
                                posX += Global.Main.mainContainerDims.wd - 120;
                                el.anchor.x = 1;
                                break;
                        }
                        switch (this.Elements.StaticElements.Options.Just.y) {
                            case "center":
                                posY += (Global.Main.mainContainerDims.hg - 65) / 2;
                                el.anchor.y = 0.5;
                                break;
                            case "bottom":
                                posY += Global.Main.mainContainerDims.hg - 65;
                                el.anchor.y = 1;
                                break;
                        }
                    }
                    el.position.set(posX, posY);
                    this.addChild(el);
                }
        }

        soundButtonDown(button) {
            Global.Main.PlayMusic(new Audio(button.name));
        }

        createQuestions() {
            this.Questions = [];
            var positions = [];

            for (var i = 0; i < this.Elements.Questions.length; i++) {
                positions.push(this.Elements.Questions[i].Position);
            }
            positions = EU.shuffleArray(positions);
            for (var i = 0; i < this.Elements.Questions.length; i++) {
               
                var el = this.game.add.text(0, 0, this.Elements.Questions[i].QuestionText, this.Elements.Questions[i].QuestionStyle);
                this.addChild(el);
                var posX = positions[i].x; //this.Elements.Questions[i].Position.x;
                var posY = positions[i].y; //this.Elements.Questions[i].Position.y;
                el.position.set(posX + this.getJustedPositionX(this.Elements.Questions[i].Position.JustX, el), posY + this.getJustedPositionY(this.Elements.Questions[i].Position.JustY, el));
                el.align = <string>(this.Elements.Questions[i].Just).toLowerCase();
                var Answers = [];
                for (var j = 0; j < this.Elements.Questions[i].Answers.length; j++) {
                    var ans = this.game.add.text(0, 0, this.Elements.Questions[i].Answers[j].Text, this.Elements.Questions[i].AnswerFont);
                    this.addChild(ans);
                    ans.type = i;
                    ans.position.set(posX + this.getJustedPositionX(this.Elements.Questions[i].Position.JustX, ans)+ el.width/2 + 40 +j*40,
                        posY + this.getJustedPositionY(this.Elements.Questions[i].Position.JustY, ans));
                    ans.inputEnabled = true;
                    ans.align = <string>(this.Elements.Questions[i].Just).toLowerCase();
                    ans.events.onInputOver.add(this.onInputOver, this);
                    ans.events.onInputOut.add(this.onInputOut, this);
                    ans.events.onInputDown.add(this.onInputDown, this);
                    Answers.push({
                        "Element": ans,
                        "isTrue": this.Elements.Questions[i].Answers[j].isTrue
                    });
                }
                this.Questions.push({
                    "Text": this.Elements.Questions[i].QuestionText,
                    "Answers": Answers
                });
            }
        }

        private createInfo() {
            this.infoPremia = this.game.add.text(1300, 760, "Premia: 0 zł", this.Elements.Style);
            this.addChild(this.infoPremia);

            this.infoScore = this.game.add.text(100, 760, "X stwierdzeń zostało oznaczonych błędnie.", this.Elements.Style);
            this.infoScore.visible = false;
            this.addChild(this.infoScore);
        }

        private getJustedPositionX(just, element) {
            var posX = 0;
            switch (just) {
                case "Center":
                    posX += (Global.Main.mainContainerDims.wd - 120) / 2 - element.width / 2;
                    break;
                case "Right":
                    posX += Global.Main.mainContainerDims.wd - 120 - element.width;
                    break;
            }
            return posX;
        }

        private getJustedPositionY(just, element) {
            var posY = 0;
            switch (just) {
                case "Center":
                    posY += (Global.Main.mainContainerDims.hg - 65) / 2 - element.height / 2;
                    break;
                case "Bottom":
                    posY += Global.Main.mainContainerDims.hg - 65 - element.height;
                    break;
            } 
            return posY;
        }


        private clearOfType(k) {
            EU.Log(k);
            for (var i = 0; i < this.Questions[k].Answers.length; i++) {
                EU.Log(this.Questions[k].Answers[i]);
                if (this.Questions[k].Answers[i].Element.name == "checked") {
                    EU.Log(k);
                    this.Questions[k].Answers[i].Element.setShadow(0, 0, "black");
                    this.numberOfChecked--;
                    this.Questions[k].Answers[i].Element.scale.set(1, 1);
                    this.Questions[k].Answers[i].Element.name = "unchecked";
                }

            }

        }

        //input

        private onInputOver(el: Phaser.Text, pointer) {
            el.alpha = 0.5;
        }

        private onInputOut(el: Phaser.Text, pointer) {
            el.alpha = 1.0;
        }

        private onInputDown(el: Phaser.Text, pointer) {
            if (el.name == "checked") {
                el.setShadow(0, 0, "black");
                this.numberOfChecked--;
                el.scale.set(1, 1);
                el.name = "unchecked";
            }
            else {
                this.clearOfType(el.type);
                el.setShadow(el.height / 15, el.height / 15, this.Color);
                this.numberOfChecked++;
                el.name = "checked";
                el.scale.set(1.3, 1.3);
            }

           this.checkIfEverythingIsCorrect()
        }


        private checkIfEverythingIsCorrect() {
            var srt = "Points: " + this.calculatePlayerPoints()
                + "/" + this.calculateMaxPoints();
            EU.Log(srt);
            EU.Log("Checked: " + this.numberOfChecked);

            this.infoPremia.text = "Premia: " + this.calculatePlayerPoints() + " zł";
            this.infoScore.text = "Oznaczono błędnie: " + (this.calculateMaxPoints()-this.calculatePlayerPoints());

            if (this.calculateMaxPoints() == this.numberOfChecked) this.infoScore.visible = true;

            if (this.calculatePlayerPoints() == this.calculateMaxPoints()
                && this.numberOfChecked == this.calculateMaxPoints() && EU.checkIfSelectablesAreCorrect(this)) {
                EU.setGameCompletion(this.parent, 1);
                EU.Log("Correct!!!");
                if (TYPE === "gm") {
                    super.nextTask();
                }
                return true;
            }
            EU.setGameCompletion(this.parent, 0);
            return false;
          
        }

        private calculateMaxPoints() {
            var points = 0;
            for (var i = 0; i < this.Questions.length; i++) {
                for (var j = 0; j<this.Questions[i].Answers.length; j++) {
                    if (this.Questions[i].Answers[j].isTrue) points++;
                }
            }    
            return points;      
        }

        private calculatePlayerPoints() {
            var points = 0;
            this.numberOfCorrect = 0;
            for (var i = 0; i < this.Questions.length; i++) {
                for (var j = 0; j < this.Questions[i].Answers.length; j++) {
                    if (this.Questions[i].Answers[j].isTrue && this.Questions[i].Answers[j].Element.name == "checked") points++;
                }
            }    
            return points;
        }

    }
}