var Odplist = [];
var PytList = [];

Game1.Game = function (game) {
    var buttonLeft;
    var buttonRight;
    var int = 0;
    var spriteList = new Array();
    var selectedbox1;
    var selectedbox2;
    var equationTable;
    var playedEquation;
    var isReadyToRead;
    var intro;
    var selectexbox1 = null;
    var selectexbox2 = null;
};
function isPlaying(audelem) {
    return !audelem.paused;
}
function shuffle(array) {
    var currentIndex = array.length, temporaryValue, randomIndex;
		
    while (0 !== currentIndex) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }

    return array;
}
Game1.Game.prototype = {
    create: function () {
        variant1 = [];
        equationTable = [];
        variant2 = [];
       
		for ( var i =0; i<10; i++)
		{
           variant1[i] = i+1;
           equationTable[i] = i+1;
           variant2[i] = i+1;
		}
		
		variant2
        shuffle(variant1);
        shuffle(variant2);
        lektor = new Audio("./sound/OS_g_I_12_1.mp3");
        lektor.load();
        finnish = new Audio("./sound/gratulacje.mp3");
        finnish.load();
        sound = new Audio();
        shuffle(equationTable);
        console.log(variant1);
        console.log(equationTable);
        playedEquation = 0;
        isReadyToRead = false;
        intro = true;
        this.bg = this.game.add.sprite(0, 0, 'bg');
        this.game.stage.backgroundColor = '#ffffff';
        this.game.physics.startSystem(Phaser.Physics.ARCADE);
        this.time.advancedTiming = true;
        this.buttonLeft = this.game.add.sprite(150 / 2, 100 / 2, 'speaker', 0);
        this.buttonLeft.name = "Speaker";
        this.buttonLeft.inputEnabled = true;
        this.buttonLeft.fixedToCamera = true;
        this.buttonLeft.events.onInputDown.add(this.down2, this);
        this.buttonLeft.events.onInputUp.add(this.up, this);
        this.buttonHelp = this.game.add.sprite((1920 - 150 / 2 - 132), 100 / 2, 'help', 0);
        this.buttonHelp.scale.setTo(0.9,0.9);
        this.buttonHelp.name = "Help";
        this.buttonHelp.inputEnabled = true;
        this.buttonHelp.fixedToCamera = true;
        this.buttonHelp.events.onInputDown.add(this.down2, this);
        this.buttonHelp.events.onInputUp.add(this.up, this);
        var item;
        var selectexbox1 = null;
        var selectexbox2 = null;
        var TotalPoints = 0;
        var equal;
        var j;
		
        for (var i = 0; i < 10; i++) {
            this.item = this.game.add.sprite(250 , 50 + (100 * (i)), 'arrow' , 0);
            this.item.scale.setTo(0.8,0.8);
            this.game.physics.arcade.enable(this.item);
            this.item.inputEnabled = true;
            this.item.name = 'rownanie';
            this.item.answer = variant1[i];
            this.item.SoundName = variant1[i];
            this.item.events.onInputDown.add(this.down, this);
            PytList.push(this.item);
        }

        for (var i = 0; i < 10; i++) {   
            this.item = this.game.add.sprite(1400, 50+(100*i), 'odp_' + (equationTable[i]), 0);
            this.item.scale.setTo(0.8,0.8);
            this.game.physics.arcade.enable(this.item);
            this.item.inputEnabled = true;
            this.item.name = 'odpowiedz';
            this.item.gotLink = false;
            this.item.answer = equationTable[i];
            this.item.events.onInputDown.add(this.down, this);
            Odplist.push(this.item);
        }
		
        this.AutoCreateCurve();
        this.showInstruction();
        lektor.play();                            
    },
    up: function (button, pointer) {
        button.frame = 0;
    },
    down2: function (button, pointer) {
        button.frame = 1;
        if (button.name == 'Speaker') {
            if (!isPlaying(sound) || sound.ended)
                this.playSound();
        } else if (!isPlaying(lektor) || lektor.ended) {
            intro = true;
            this.showInstruction();
            lektor.play();
        }
    },
    showInstruction: function () {
        this.guiHelp = this.game.add.sprite(1410, 380, 'guiPomocnik');
        this.guiHelp.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
        this.guiBubble.fixedToCamera = true;
        this.titleText = this.game.add.text(240, 440, 'Find the animal you hear.', {
            font: "68px Arial",
            fill: "#000000",
        });
        this.titleText.fixedToCamera = true;
    },
    hideInstruction: function () {
        this.guiHelp.visible = false;
        this.guiBubble.visible = false;
        this.titleText.visible = false;
    },
    stopDrag: function () {
        this.sprite.body.moves = true;
    },
    playSound: function () {
		sound.pause();
        var int = variant2[playedEquation];
        sound = new Audio("./sound/OB_G_I_12_0" + int + ".mp3");
        sound.load();
        sound.loop = false;
        sound.play();
    },
    playWrong: function () {
		sound.pause();
        sound = new Audio("./sound/z" + (Math.floor(Math.random() * 2) + 1) + ".mp3");
        sound.load();
        sound.loop = false;
        sound.play();    
    },
    playGood: function () {
		sound.pause();
        sound = new Audio("./sound/g1.mp3");
        sound.load();
        sound.loop = false;
        sound.play();
    },
    showFinnish: function () {
        this.guiHelp = this.game.add.sprite(1410, 380, 'guiPomocnik');
        this.guiHelp.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
        this.guiBubble.fixedToCamera = true;
        this.titleText = this.game.add.text(250, 400, 'Well done!', {
            font: "68px Arial",
            fill: "#000000",
        });
        this.titleText.fixedToCamera = true;
        finnish.play();
    },
    hideFinnish: function()
    {
		this.guiHelp.kill();    
		this.guiBubble.kill();
		this.state.start('Menu');
		location.reload();
    },
    down: function (button, pointer) { 
        console.log('equetion = '+equationTable[playedEquation]);
        if (this.selectexbox1 == null && ( !isPlaying(lektor) || lektor.ended)) {
            if (button.SoundName == variant2[playedEquation]) {
                this.selectexbox1 = button;
                this.selectexbox1.frame = 1;
                this.TotalPoints++;        
				playedEquation++;
                this.playGood();
                isReadyToRead = true;
            } else {
                console.log("error!");
                this.playWrong();
                this.selectexbox1 = null;
            }
        }
    },
    fadePicture: function (button) {
        var tween = this.game.add.tween(button).to({
            alpha: 0
        }, 2000, Phaser.Easing.Linear.None, true);
    },
    createCurve: function (place1, place2){   
        var a= place1.x+(140*0.8);
        var b= place1.y+((70*0.8)/2);
        var graphics = this.game.add.graphics(place1.x+(140*0.8), place1.y+((70*0.8)/2));
        graphics.lineStyle(10,(Math.random()*16777215), 1);
        graphics.moveTo(0,0);
        graphics.bezierCurveTo((Math.random()*(place2.x-a)),(Math.random()*(place2.y-b)),(Math.random()*(place2.x-a)),(Math.random()*(place2.y-b)),(place2.x-a),((place2.y-b)+(134*0.8)/2));       
    },  
    AutoCreateCurve: function (){
        
       for (var i =0 ; i<10; i++){
           for (var j = 0; j<10; j++)
           {
               if ((PytList[i].answer == Odplist[j].answer) && Odplist[j].gotLink == false)
               {
					this.createCurve(PytList[i],Odplist[j]);
					Odplist[j].gotLink = true;       
					break;
               }
           }
		}
    },
    update: function () {
        if (playedEquation == 10 && (!isPlaying(sound) || sound.ended)) {
            playedEquation = 13;
            this.showFinnish();
        }
        if (playedEquation == 13 && (!isPlaying(finnish) || finnish.ended)) {
            this.hideFinnish();
        }
        if (playedEquation < 10) {
            if ((!isPlaying(lektor)||  lektor.ended) && intro) {
                intro = false;
                this.hideInstruction();
                isReadyToRead = true;
            }
            if (isReadyToRead && (!isPlaying(sound) || sound.ended)) {
                if (this.selectexbox1 != null){
					this.selectexbox1.frame = 0;
					this.selectexbox1 = null;
				}
                isReadyToRead = false;
                this.playSound();
            }
        }
    },
    render: function () {
    }
};