using UnityEngine;
using System;
using System.Collections;
using InControl;

public class ScoreManager
{
	public static Player p1;
	public static Player p2;
	public static Player p3;
	public static Player p4;
	public static string winner;
	public static int playerCount = 0;

	private static ScoreManager _instance;

	public ScoreManager()
	{
		
	}
	
	public static ScoreManager getInstance()
	{	
		return _instance != null ? _instance : _instance = new ScoreManager();
	}
	
	public bool checkForWin()
	{
		playerCount = InputManager.Devices.Count;
		if(playerCount > 1){

			var otherLives = 0;

			switch (playerCount)
			{
				case 2:
					otherLives = 0;
					if(p2.livesCounter == 0){ winner = "player1"; return true; }
					if(p1.livesCounter == 0){ winner = "player2"; return true; }
					break;
				case 3:
					otherLives = 0;
					otherLives = p2.livesCounter + p3.livesCounter;
					if(p1.livesCounter > 0 && otherLives == 0){ winner = "player1"; return true; }

					otherLives = 0;
					otherLives = p1.livesCounter + p3.livesCounter;
					if(p2.livesCounter > 0 && otherLives == 0){ winner = "player2"; return true; }

					otherLives = 0;
					otherLives = p1.livesCounter + p2.livesCounter;
					if(p3.livesCounter > 0 && otherLives == 0){ winner = "player3"; return true; }
					break;
				case 4:
					otherLives = 0;
					otherLives = p2.livesCounter + p3.livesCounter + p3.livesCounter + p4.livesCounter;
					if(p1.livesCounter > 0 && otherLives == 0){ winner = "player1"; return true; }
					
					otherLives = 0;
					otherLives = p1.livesCounter + p3.livesCounter + p4.livesCounter;
					if(p2.livesCounter > 0 && otherLives == 0){ winner = "player2"; return true; }
					
					otherLives = 0;
					otherLives = p1.livesCounter + p2.livesCounter + p4.livesCounter;
					if(p3.livesCounter > 0 && otherLives == 0){ winner = "player3"; return true; }

					otherLives = 0;
					otherLives = p1.livesCounter + p2.livesCounter + p3.livesCounter;
					if(p4.livesCounter > 0 && otherLives == 0){ winner = "player4"; return true; }
					break;
				default:
					break;
			}
		}

		return false;
	}

	public void resetWinner()
	{
		winner = null;
	}

	public int countPlayers()
	{
		return InputManager.Devices.Count;
	}
}
