var GA = [];
var playedEquationGame0;
var player1;
var player2;
var player1tween;
var player2tween;
var npc;
var arrowleft = 0;
var arrowright = 0;
var arrowup = 0;
var arrowdown = 0;
var CurrentState = 0;
var GoodSound;
var BadSound;
var WalkSound;
Game1.Game0 = function (game) {
    var buttonLeft;
    var buttonRight;
    var int = 0;
    var spriteList = new Array();
    var selectedbox1;
    var selectedbox2;
    var equationTable;  
    var isReadyToRead;
    var intro;
    var input = 0;
    var calc_value;
    var text ;
};

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;
}
function AudioStop()
{
	WalkSound.pause();
}
function section1()
{
	WalkSound.loop = true;    
	WalkSound.play();    
	console.log('sec1');
	player1tween.to({
        x: 1450
    }, 1377, Phaser.Easing.Quintic.InOut, true, 0, 0);   
	player2tween.to({
        x: 1430
    }, 1377, Phaser.Easing.Quintic.InOut, true, 0, 0);
	player2tween.onComplete.addOnce(AudioStop, this);       
}
function section2()
{
	WalkSound.loop = true;    
	WalkSound.play();    
	console.log('sec2');            
	player1tween.to({
        y: 440
    }, 1070, Phaser.Easing.Quintic.InOut, true, 0, 0);
	player2tween.to({
        y: (410)
    }, 1170, Phaser.Easing.Quintic.InOut, true, 0, 0);
	player2tween.onComplete.addOnce(AudioStop, this); 
}
function section20()
{
	WalkSound.loop = true;    
	WalkSound.play();    
	console.log('sec2'); 
	player1tween.to({
        y: 440
    }, 1070, Phaser.Easing.Quintic.InOut, true, 0, 0);
}
function section3()
{
	WalkSound.loop = true;    
	WalkSound.play();
	console.log('sec3');
	player1tween.to({
        x: 530
    }, 2970, Phaser.Easing.Quintic.InOut, true, 0, 0);    
	player2tween.to({
        x: 540
    }, 2970, Phaser.Easing.Quintic.InOut, true, 0, 0);    
	player2tween.onComplete.addOnce(AudioStop, this);     
}
function section4()
{
	WalkSound.loop = true;    
	WalkSound.play();
	console.log('sec4');  
	player1tween.to({
        x:480, y:190
    }, 1070, Phaser.Easing.Quintic.InOut, true, 0, 0);  
	player2tween.to({
        x:470, y:180
    }, 1070, Phaser.Easing.Quintic.InOut, true, 0, 0);
	player2tween.onComplete.addOnce(AudioStop, this);     
}
function section40()
{
	console.log('sec2');
}
function section5()
{
	WalkSound.loop = true;    
	WalkSound.play();
	console.log('sec5');  
	player1tween.to({
        x:320, y:190
    }, 1070, Phaser.Easing.Quintic.InOut, true, 0, 0);       
	player2tween.to({
        x:300, y:180
    }, 1070, Phaser.Easing.Quintic.InOut, true, 0, 0); 
	player2tween.onComplete.addOnce(AudioStop, this);     
}
function section6()
{
	WalkSound.loop = true;    
	WalkSound.play();
	player1tween.to({
        x:320, y:400
    }, 1070, Phaser.Easing.Quintic.InOut, true, 0, 0);       
	player2tween.to({
        x:300, y:380
    }, 1070, Phaser.Easing.Quintic.InOut, true, 0, 0); 
	player2tween.onComplete.addOnce(section7,this);    
}
function section7()
{
	WalkSound.pause();    
    npc.bringToTop();
	console.warn("koniec!");  
	playedEquationGame0 = 4;
}
Game1.Game0.prototype = {
    create: function () {
        WalkSound = new Audio('./sound/walk.mp3');
        GoodSound = new Audio('./sound/click.mp3');
        BadSound = new Audio('./sound/error.mp3');
        GA = [];
        GA = [1,2,3,4];
        calc_value ="";
        var input = "0";
        lektor = new Audio("./sound/ZIP_g_I_2_2.mp3");
        lektor.load();
        finnish = new Audio("./sound/ZIP_g_I_2_3.mp3");
        finnish.load();
        sound = new Audio();
        variant1 = [1,2,3,4,5,6];
        shuffle(variant1);
        playedEquationGame0 = 0;
        isReadyToRead = false;
        intro = true;
        this.bg = this.game.add.sprite(0, 0, 'bg_ingame');
        this.game.stage.backgroundColor = '#ffffff';
        this.game.physics.startSystem(Phaser.Physics.ARCADE);
        this.time.advancedTiming = true;
        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;
        this.bg2 = this.game.add.sprite(239, 94, 'game_bg');
        
		for (var i = 0; i<4; i++)
        {   
			this.item = this.game.add.sprite(439+((129+167)*(i)), 896, "arrow_"+i);   
			this.game.physics.arcade.enable(this.item);
			this.item.inputEnabled = true;
			this.item.name = 'arrow';
			this.item.value = i;
			this.item.events.onInputDown.add(this.down2, this);
			this.item.events.onInputUp.add(this.up, this);  
        }

        player2 = this.game.add.sprite(730,210,'gracz2');
        player1 = this.game.add.sprite(750,240, 'gracz1');
        player1tween =  this.game.add.tween(player1);
        player2tween = this.game.add.tween(player2);   
        npc = this.game.add.sprite(260,500, 'npc2'); 
        this.showInstruction();
        lektor.play();                       
    },
    up: function (button, pointer) {
        button.frame = 0;
    },    
    down2: function (button, pointer) {
        button.frame = 1;
		if (button.name == 'arrow')
        {
            GoodSound.play();
			if (button.value == 0) arrowdown++;
			if (button.value == 1) arrowleft++;
			if (button.value == 2) arrowright++;
			if (button.value == 3) arrowup++;
        }
        else if (!isPlaying(lektor) || lektor.ended) {
            intro = true;
            this.showInstruction();
            lektor.play();
        }
    },
    showInstruction: function () {
        this.guiHelp = this.game.add.sprite(1410, 450, 'guiPomocnik_good');
        this.guiHelp.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
        this.guiBubble.scale.setTo(1.6,1.6); 
        this.guiBubble.fixedToCamera = true;
        this.titleText = this.game.add.text(300, 300, 'Pomóż koledze który się zgubił.\nZaprowadź go do przewodnika.\nAby tam dotrzeć musisz wykonać:\n- 5 kroków w prawo\n- 2 kroki w dół\n- 7 kroków w lewo\n- 2 kroki do góry\n- 1 krok w lewo\ni 3 kroki na dół.', {
			font: "50px 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;
    },
	showFinnish: function () {
	    this.guiHelp = this.game.add.sprite(1410, 450, 'guiPomocnik_good');
        this.guiHelp.fixedToCamera = true;
        this.guiBubble = this.game.add.sprite(135, 96, 'Finish');
        this.guiBubble.scale.setTo(1.6,1.6); 
        this.guiBubble.fixedToCamera = true;
        this.titleText = this.game.add.text(250, 400, 'Dziękujemy za pomoc koledze.\nIdąc dalej napotkasz kolejne zadania.', {
            font: "58px Arial",
            fill: "#000000",
        });
        this.titleText.fixedToCamera = true;
        finnish.play();
    },
    hideFinnish: function () {
		setTimeout(function(){
        playedEquationGame0 = 14;
		}, 500);    
    },
	update: function () {
		if (CurrentState == 0) {
			if (arrowup != 0 || arrowleft != 0 || arrowdown != 0) {
				console.log("! blad!!");              
				GoodSound.pause();             
				BadSound.play();
				arrowdown = 0;
				arrowright = 0;
				arrowup = 0;
				arrowleft = 0;
			}
			if (arrowright == 5) {
				console.log("Dobrze");
				CurrentState = 1;
				arrowdown = 0;
				arrowright = 0;
				arrowup = 0;
				arrowleft = 0;
				section1();
			}
		}
		section2
        if (CurrentState == 1) {
			if (arrowup != 0 || arrowright != 0 || arrowleft != 0) {
				console.log("! blad!!"); 
				GoodSound.pause();
				BadSound.play();
				arrowdown = 0;
				arrowright = 0;
				arrowup = 0;
				arrowleft = 0;
			}
			if (arrowdown == 2) {
				player1tween = null;
				player2tween = null;
				player1tween = this.game.add.tween(player1);
				player2tween = this.game.add.tween(player2);
				console.log("Dobrze");
				CurrentState = 2;
				arrowdown = 0;
				arrowright = 0;
				arrowup = 0;
				arrowleft = 0;
				section2();
			}
        }
		if (CurrentState == 2) {
			if (arrowup != 0 || arrowright != 0 || arrowdown != 0) {
				console.log("! blad!!"); 
				GoodSound.pause();
				BadSound.play();
				arrowdown = 0;
				arrowright = 0;
				arrowup = 0;
				arrowleft = 0;
			}
			if (arrowleft == 7) {
				player1tween = null;
				player2tween = null;
				player1tween = this.game.add.tween(player1);
				player2tween = this.game.add.tween(player2);
				console.log("Dobrze");
				CurrentState = 3;
				arrowdown = 0;
				arrowright = 0;
				arrowup = 0;
				arrowleft = 0;
				section3();
			}
		}
		if (CurrentState == 3) {
			if (arrowleft != 0 || arrowright != 0 || arrowdown != 0) {
				console.log("! blad!!"); 
				GoodSound.pause();
				BadSound.play();
				arrowdown = 0;
				arrowright = 0;
				arrowup = 0;
				arrowleft = 0;
			}
			if (arrowup == 2) {
				player1tween = null;
				player2tween = null;
				player1tween = this.game.add.tween(player1);
				player2tween = this.game.add.tween(player2);
				console.log("Dobrze");
				CurrentState = 4;
				arrowdown = 0;
				arrowright = 0;
				arrowup = 0;
				arrowleft = 0;
				section4();
			}
		}
		if (CurrentState == 4) {
			if (arrowup != 0 || arrowright != 0 || arrowdown != 0) {
				console.log("! blad!!"); 
				GoodSound.pause();
				BadSound.play();
				arrowdown = 0;
				arrowright = 0;
				arrowup = 0;
				arrowleft = 0;
			}
			if (arrowleft == 1) {
				player1tween = null;
				player2tween = null;
				player1tween = this.game.add.tween(player1);
				player2tween = this.game.add.tween(player2);
				console.log("Dobrze");
				CurrentState = 5;
				arrowdown = 0;
				arrowright = 0;
				arrowup = 0;
				arrowleft = 0;
				section5();
			}
		}	
		if (CurrentState == 5) {
			if (arrowup != 0 || arrowright != 0 || arrowleft != 0) {
				console.log("! blad!!"); 
				GoodSound.pause();
				BadSound.play();
				arrowdown = 0;
				arrowright = 0;
				arrowup = 0;
				arrowleft = 0;
			}
			if (arrowdown == 3) {
				player1tween = null;
				player2tween = null;
				player1tween = this.game.add.tween(player1);
				player2tween = this.game.add.tween(player2);
				console.log("Dobrze");
				CurrentState = 6;
				arrowdown = 0;
				arrowright = 0;
				arrowup = 0;
				arrowleft = 0;
				section6();
			}
		}	
		if (playedEquationGame0 == 4 && (!isPlaying(sound) || sound.ended)) {
			sound.pause();
            playedEquationGame0 = 13; 
            this.showFinnish();
        }
        if (playedEquationGame0 == 13 && (!isPlaying(finnish) || finnish.ended)) {
           this.hideFinnish();
        }
        if (playedEquationGame0 < 4) {
            if ((!isPlaying(lektor) || lektor.ended) && intro) {
                intro = false;
                this.hideInstruction();
                isReadyToRead = true;
            }
        }
        if (playedEquationGame0 ==14)
        {
           this.game.state.start('Game1');  
        }
    },
    render: function () {
    }
};