var express = require('express');
var app = express();

var server = require('http').createServer(app).listen(8080);

var io = require('socket.io').listen(server);

io.set('log level', 1);

var socket_loco;

var monsters = []

var points = [];

var is_playing = 1;

var monster_id = 1;

function Point(_x,_y)
{
	this.x = _x;
	this.y = _y;
}

function createPoint()
{
	points.push(new Point(2770,2770));
	points.push(new Point(1770,2770));
	points.push(new Point(1581,384));
	points.push(new Point(1978,1386));
	points.push(new Point(1372,1391));
	
	points.push(new Point(986,1192));	
	points.push(new Point(2770,2770));
	points.push(new Point(581,1785));
	points.push(new Point(984,1785));
	points.push(new Point(1386,2189));
	
	points.push(new Point(1386,2592));
	points.push(new Point(180,2395));
	points.push(new Point(585,3587));
	points.push(new Point(1183,3986));
	points.push(new Point(1970,3189));
	
	points.push(new Point(2280,2592));
	points.push(new Point(1986,2280));
	points.push(new Point(2276,1998));
	points.push(new Point(2580,2306));
	points.push(new Point(2580,4195));
	
	points.push(new Point(3380,3604));
	points.push(new Point(4385,3180));
	points.push(new Point(4381,2198));
	points.push(new Point(3582,2170));
	points.push(new Point(3181,2183));
	
	points.push(new Point(3181,1775));
	points.push(new Point(2578,1385));
	points.push(new Point(2979,788));
	points.push(new Point(3785,398));
	points.push(new Point(3785,789));
	
	
	points.push(new Point(1570,1770));
	points.push(new Point(1156,1957));
	points.push(new Point(955,2664));
	points.push(new Point(721,2399));
	points.push(new Point(570,2700));
	points.push(new Point(835,3253));
	points.push(new Point(1797,4390));
	points.push(new Point(2069,3911));
	points.push(new Point(504,2303));	
}

createPoint();

var classes = ["A", "T", "I", "P"]

function Balance()
{
	this.blue_disk = 51;
	this.red_disk = 49;

	this.red_die = function(_cant)
	{
		this.blue_disk = this.blue_disk - _cant;
		this.red_disk = this.red_disk + _cant;
		for(var i=0; i < players.length; i++)
		{
			players[i].socket.send("update_balance,"+this.blue_disk+","+this.red_disk);
		}
		console.log("Left: "+this.blue_disk+"   Rigth: "+this.red_disk);
		monster_id--;
	}
	
	this.blue_die = function(_cant)
	{
		this.blue_disk = this.blue_disk + _cant;
		this.red_disk = this.red_disk - _cant;
		for(var i=0; i < players.length; i++)
		{
			players[i].socket.send("update_balance,"+this.blue_disk+","+this.red_disk);
		}
		console.log("Left: "+this.blue_disk+"   Rigth: "+this.red_disk);
		monster_id--;
	}
}

var balance = new Balance();

function desbalanceo()
{
     
	if(balance.blue_disk > balance.red_disk)
	{
		balance.blue_die(1);
	}
	if(balance.red_disk > balance.blue_disk)
	{
		balance.red_die(1);	
	}
	if(balance.blue_disk == balance.red_disk)
	{
		for(var i=0; i < players.length; i++)
		{
			console.log("You Won");
			players[i].socket.send("you_won,"+"won");
			is_playing = 0;
		}
	}
	if(balance.red_disk < 0)
	{
		for(var i=0; i < players.length; i++)
		{
			console.log("You Lose");
			players[i].socket.send("you_lose,"+"rational");
			is_playing = 0;
		}
	}
	if(balance.blue_disk < 0)
	{
		for(var i=0; i < players.length; i++)
		{
			console.log("You Lose");
			players[i].socket.send("you_lose,"+"creative");
			is_playing = 0;
		}
	}
}	

function Monster(_life, _x,_y, _id, _tipe)
{
	this.id = _id;
	this.life = _life;
	this.x = _x;
	this.y = _y;
	console.log("New MOD ID: "+_id+" POS X: "+this.x+" POS Y: "+this.y+"Tipe: "+_tipe);
}

function Player (_socket, _id, _player_enemies) {
	this.socket = _socket;
    this.id = _id;
	this.x = 410;
	this.y = 268;
	this.enemies = _player_enemies
	
	this.setPosition = function(_x,_y)
	{
		this.x = _x;
		this.y = _y;
	}
}

var players = []

console.log('Server Up, Waiting conections...');

function sendPosition(_data)
{
	for(var i=0; i < players.length; i++)
	{
		players[i].socket.send("response,"+_data);
	}
}

function sendMonsterPosition(player)
{
	/**/
}

function newMonster()
{
	if(monster_id < 20)
	{
		var color = Math.floor((Math.random()* 2));
		var id = Math.floor((Math.random()* points.length));
		var pos = points[id];
		var type;
		if(color == 1)
			type = "red";
		else
			type = "blue";
		var	monster = new Monster(5, pos.x,pos.y, monster_id, type);
		monsters.push(monster);
		for(var i=0;i<players.length;i++)
			players[i].socket.send("create_monster,"+pos.x+","+pos.y+","+monster_id+","+type);
		monster_id++;	
	}
}

setInterval(function()
{
	if((players.length >= 1) & (is_playing != 0))
	{
		newMonster();
		desbalanceo();
		}
},2500);

// Server Things
io.sockets.on("connection", function (socket) {

	console.log("New Client Conection whit id: "+socket.id);
	
	if(classes.length > 0)
	{
		var id = Math.floor((Math.random()*classes.length)); 
		var client_id = classes[id];
		
		if (id > -1) 
		{
			classes.splice(id, 1);
		}
		
		var player = new Player(socket,client_id,players);
		players.push(player);
		console.log("Player "+ player.id +" was added to players queue "+ players.length);
		socket.send("setid,"+client_id);
		
		sendMonsterPosition(player);
		
		socket.on("message", function(data){
			if(data.split(",")[1] == "m")
				sendPosition(data);
			if(data.split(",")[1] == "blue_die")
				balance.blue_die(2);
			if(data.split(",")[1] == "red_die")
				balance.red_die(2);
			//console.log(data);	
		});
		
		socket.on("disconnect", function(data){
			console.log("Player off")
			socket.leave(data);
		});
	}
	else
	{
		console.log("Sorry Amego, estamo' lleno... Tira tiroooooo");
	}
});