
var clickPerSecond = 0;
var counter = 0;

var HIGH_SCORE = 0;

var VEL_LOW 	= 2;
var VEL_MED 	= 3;
var VEL_HI 	= 6;

var HErz 	= 0;

var COLOR_LOW	= '#FF2222';
var COLOR_MED	= '#00EE00';
var COLOR_HI	= '#55DDFF';

var COLOR_LOW_number	= 0xFF2222;
var COLOR_MED_number	= 0x00EE00;
var COLOR_HI_number	= 0x55DDFF;

var TARGET_CRASH	= 0;
var TARGET_KILL		= 1;

//var GOAL_CRASHES = 1;
var GOAL_CRASHES = 7;
var MONSTERS = 0;


var targetGroup1;
var targetGroup2;
var targetGroup3;

PrincipalGame.Main = function(game){

	this.game = game;
}

PrincipalGame.Main.prototype = {
	init: function(){

	},
	preload: function(){
	},
	create: function(){
		this.enemies = [];

		this.ciudadCounter = 0;

		counter = 0;

		this.totalCrashes = 0;
		this.totalKills = 0;
		
		totalTimeSurvived = 0;

        	var self = this;
		
		this.player = new Player(this.game);
		this.onda = new Onda(this.game);

		this.game.world.setBounds(0, 0, 800, 600);

		this.GO_sound = this.game.add.audio('gameOver');
		this.GO_sound.volume = 3;

		this.Alarm_sound = this.game.add.audio('Alarm');

		this.Music_sound = this.game.add.audio('InGame');
		this.Music_sound.loop = true;

		this.game.debug.pointer();

		this.game.input.addPointer(); //El mouse
		this.game.input.addPointer(); //El primer Touch
		this.cursor = this.game.input.keyboard.createCursorKeys(); //Por si necesitamos

		this.posicionesAntena = [];
		this.posicionesAntena.push( {x: 88, y: 273} );
		this.posicionesAntena.push( {x: 88, y: 273} );
		this.posicionesAntena.push( {x: 88, y: 273} );
		this.posicionesAntena.push( {x: 88, y: 273} );
		this.posicionesAntena.push( {x: 88, y: 273} );
		this.posicionesAntena.push( {x: 88, y: 273} );
		this.posicionesAntena.push( {x: 110, y: 287} );
		this.posicionesAntena.push( {x: 110, y: 287} );
		this.posicionesAntena.push( {x: 110, y: 287} );

		this.fondos = [];
		this.fondos.push(this.game.add.sprite(0, 0, 'fondo'));
		this.fondos.push(this.game.add.sprite(0, 0, 'fondo1'));
		this.fondos.push(this.game.add.sprite(0, 0, 'fondo2'));
		this.fondos.push(this.game.add.sprite(0, 0, 'fondo3'));
		this.fondos.push(this.game.add.sprite(0, 0, 'fondo4'));
		this.fondos.push(this.game.add.sprite(0, 0, 'fondo5'));
		this.fondos.push(this.game.add.sprite(0, 0, 'fondo6'));
		this.fondos.forEach(function(x, i){x.visible=(i==0)});


		this.gOndas = game.add.graphics(0, 0);

		this.ciudades = [];
		this.ciudades.push(this.game.add.sprite(0, 275, 'ciudad'));
		this.ciudades.push(this.game.add.sprite(0, 275, 'ciudad1'));
		this.ciudades.push(this.game.add.sprite(0, 275, 'ciudad2'));
		this.ciudades.push(this.game.add.sprite(0, 275, 'ciudad3'));
		this.ciudades.push(this.game.add.sprite(0, 275, 'ciudad4'));
		this.ciudades.push(this.game.add.sprite(0, 275, 'ciudad5'));
		this.ciudades.push(this.game.add.sprite(0, 275, 'ciudad6'));
		this.ciudades.forEach(function(x, i){x.visible=(i==0)});

		this.explosion = this.game.add.sprite(this.game.width / 2, this.game.height / 2, 'explosion');
		this.explosion.anchor.setTo(0.5, 0.5);
		this.explosion.alpha = 0;

		this.game.physics.startSystem(Phaser.Physics.ARCADE);
		this.game.physics.arcade.gravity.y = 100;//250;


		this.player.Add();

		if (0 == 0)
		{
			this.Alarm_sound.play();
			this.Music_sound.play();
		}
	
		function drawpulse(){
		    self.osciloscopio.moveTo(self.osciloscopioX,self.osciloscopioY);
		    self.osciloscopio.lineTo(self.osciloscopioX,-self.osciloscopioH);
		    self.osciloscopioX++;
		    self.osciloscopio.lineTo(self.osciloscopioX,self.osciloscopioH);
		    self.osciloscopioX++;
		    self.osciloscopio.lineTo(self.osciloscopioX,0);
		    self.osciloscopioX++;
		}

		//osciloscopio
		this.osciloscopioBg = this.game.add.sprite(0, 0, 'osciloscopiobg');
		this.osciloscopioBg.y = /*this.botonera.y*/ this.game.height - this.osciloscopioBg.height;


		this.game.physics.arcade.enable(this.osciloscopioBg);
		this.osciloscopioBg.body.collideWorldBounds = true;

		this.osciloscopio = game.add.graphics(3, 0);
		//this.osciloscopio.lineStyle(1, 0x00FF00);
		this.osciloscopio.lineStyle(1, COLOR_MED_number);
		this.osciloscopio.moveTo(0,0);

		//this.osciloscopio.lineTo(200, 0);
		this.osciloscopioX = 0;
		this.osciloscopioY = 0;
		this.osciloscopioW = 699;

		this.osciloscopioFront = this.game.add.sprite(0, 0, 'osciloscopioFront');
		this.osciloscopioFront.y = /*this.botonera.y*/ this.game.height - this.osciloscopioFront.height;
		this.osciloscopio.x = 3;
		this.osciloscopio.y = this.osciloscopioFront.y + this.osciloscopioFront.height/2;
		this.osciloscopioH = this.osciloscopioFront.height/2 * 0.35;

	   
		this.txtOsciliscopio = game.add.text(this.osciloscopioX + this.osciloscopioW + 50, this.osciloscopio.y, '0 Hz', { font: "24px Arial", fill: "#00FF00", align: "center" });
		this.txtOsciliscopio.anchor.setTo(0.5, 0.5);

		game.time.events.loop(Phaser.Timer.SECOND, this.updateCounter, this);

		this.time_text = game.add.text(50, 15, 'Time      00:00', { font: "15px Arial", fill: "#aacc44", align: "center" });
		this.time_text.anchor.setTo(0.5, 0.5);

		this.strength_text = game.add.text(50, 35, 'Strength       ' + GOAL_CRASHES,
				{ font: "15px Arial", fill: "#aacc44", align: "center" });
		this.strength_text.anchor.setTo(0.5, 0.5);


		this.monsters_text = game.add.text(50, 55, 'Monsters      ' + MONSTERS,
				{ font: "15px Arial", fill: "#aacc44", align: "center" });
		this.monsters_text.anchor.setTo(0.5, 0.5);


		this.onda.AddEmitter();

		this.gameOver = false;

		this.game.input.onDown.add(
			    function(){
				if (self.gameOver) return;
				drawpulse();
				clickPerSecond++;
			    }
			, this);

		this.game.input.keyboard.onDownCallback = function (e)
		{
				if (self.gameOver) return;
				drawpulse();
				clickPerSecond++;
		};

        //this.imgGameOver = this.game.add.sprite(this.game.width / 2, this.game.height / 2, 'gameover');
        //this.imgGameOver.anchor.setTo(0.5, 0.5);
        //this.imgGameOver.alpha = 0;
	},

	updateCounter:function() {
          var self=this;

	      counter++;

	      if (clickPerSecond == 0){
		      HErz= 0;
		      this.osciloscopio.lineStyle(1, COLOR_MED_number);
		      this.txtOsciliscopio.addColor(COLOR_MED, 0);
	      }
	      else if (clickPerSecond < VEL_MED && clickPerSecond > 0){
		      HErz= 10;
		      this.osciloscopio.lineStyle(1, COLOR_LOW_number);
		      this.txtOsciliscopio.addColor(COLOR_LOW, 0);
	      }
	      else if (clickPerSecond >= VEL_MED && clickPerSecond<= VEL_HI){
		      HErz= 40;
		      this.osciloscopio.lineStyle(1, COLOR_MED_number);
		      this.txtOsciliscopio.addColor(COLOR_MED, 0);
	      }
	      else if (clickPerSecond > VEL_HI){
		      HErz= 80;
		      this.osciloscopio.lineStyle(1, COLOR_HI_number);
		      this.txtOsciliscopio.addColor(COLOR_HI, 0);
	      }


	      clickPerSecond = 0;


	      if (!self.gameOver)
	      {
		      this.time_text.setText('Time      ' + counterPrint(counter));
		      this.strength_text.setText('Strength       ' + (GOAL_CRASHES - this.totalCrashes));
		      this.monsters_text.setText('Monsters      ' + MONSTERS);
	      }

	      /*	
			this.text3.setText('HErz: ' + HErz );
	       */
	      this.txtOsciliscopio.setText(HErz + 'Hz');

            if(!self.gameOver){
                if(counter % 2 == 0){
                    var i = this.game.rnd.integerInRange(1, 3);

                    var target = new Target(this.game);
                    target.Add(i);
                    target.init(this.osciloscopioBg);
                    this.enemies.push(target);

                    MONSTERS++;
		    this.game.physics.arcade.gravity.y += 40;
                }
            }
	},

	update:function(){
		var self = this;
		var game = this.game;

		self.osciloscopio.moveTo(self.osciloscopioX,self.osciloscopioY);
		var newX = self.osciloscopioX + self.game.time.elapsed * 50/1000;
		if(newX>self.osciloscopioW) {
		    newX -= self.osciloscopioW;
		    self.osciloscopio.clear();
		    self.osciloscopio.lineStyle(1, 0x00FF00);
		    self.osciloscopio.moveTo(newX,0);
		};
		self.osciloscopioX = newX;
		self.osciloscopio.lineTo(self.osciloscopioX , self.osciloscopioY);

		if (this.explosion.alpha > 0)
		{
			this.explosion.alpha -= 0.01;
		}

		for(var i = 0; i<this.enemies.length; i++){
                if(self.gameOver){
                    this.enemies[i].alive = false;
                    this.enemies[i].sprite.alpha = 0;
                }

				//console.log(i);
				r = this.enemies[i].update();
				if (r == TARGET_CRASH)
				{
					this.totalCrashes++;
					if (this.totalCrashes == GOAL_CRASHES)
					{
                        self.gameOver=true;

                        //self.imgGameOver.alpha=1;

						this.Alarm_sound.stop();
						this.Music_sound.stop();
						this.onda.stfu();

						for (i = 0; i < this.enemies.length; i++)
						{
							this.enemies[i].stfu();
						}

						totalTimeSurvived = counter;

						game.state.start('GameOver');

						//this.GO_sound.play();
					}
					else
					{
						// Explosion
						this.explosion.alpha = 1;

						// Cambio de fondo
                        this.fondos.forEach(function(x, i){
                            x.visible=(i==self.ciudadCounter+1)
                        });
                        this.ciudades.forEach(function(x, i){
                            x.visible=(i==self.ciudadCounter)
                        });
                        /*
                        this.fondos[this.fondos.length - 1 - this.ciudadCounter].alpha = 0;
						this.ciudades[this.ciudades.length - 1 - this.ciudadCounter].alpha = 0;
						*/
						this.ciudadCounter++;
					}
				}
				else if (r == TARGET_KILL)
				{
					// Algo?
				}
			}

		//this.onda.Update();


        this.gOndas.clear();

	if (!self.gameOver)
	{
		switch (HErz){
		    case 10:
			this.gOndas.lineStyle(1, COLOR_LOW_number);
			break;
		    case 40:
			this.gOndas.lineStyle(1, COLOR_MED_number);
			break;
		    case 80:
			this.gOndas.lineStyle(1, COLOR_HI_number);
			break;
		}

		var pos = this.posicionesAntena[this.ciudadCounter];
		for(var i=this.game.rnd.integerInRange(0, 5); i<this.game.width*2; i+=this.game.rnd.integerInRange(50, 60)){
		    this.gOndas.drawCircle(pos.x, pos.y, i);
		}
	}

		//this.target.update();
		//this.text2.setText('ClicksPorSegundo: ' + clickPerSecond );
	},

	render: function(){
		//this.game.debug.pointer(this.game.input.pointer1.isDown);
	}
}
