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

public class PlayerManager : MonoBehaviour
{
	public GameObject playerPrefab;
    public int NumberOfPlayers { get; set; }

    public List<Player> Players
    {
        get
        {
            return m_Players;
        }

        set
        {
            m_Players = value;
        }
    }

    public List<Vector3> playerStartPositions = new List<Vector3>() {
        new Vector3( -1, 1, -5 ),
        new Vector3( 1, 1, -5 ),
        new Vector3( -1, -1, -5 ),
        new Vector3( 1, -1, -5 ),
    };

    List<Player> m_Players = new List<Player>();

    public Player GetPlayer(int index)
    {
        if(m_Players.Count > 0)
        { 
            return m_Players[index];
        }
        return null;
    }

    public int GetPlayerListSize()
    {
        return m_Players.Count;
    }

    public Vector3 GetPlayerStartPosition(int playerIndex)
    {
        if(playerIndex > playerStartPositions.Count)
        { return Vector3.zero; }

        return playerStartPositions[playerIndex];
    }

    void Start()
	{
        foreach(PersistantPlayerInfo player in GameManager.GetPersistantGameInfo().ActivePlayers)
        {
            CreatePlayer(player);
        }
    }

	void Update()
	{

	}

    public bool CanTransmitDisease()
    {
        bool canTrasmit = true;
        int diseasedPlayers = 0;

        //if playing alone you can always get a disease
        if(m_Players.Count == 1)
        {
            return canTrasmit;
        }

        for (int i = 0; i < m_Players.Count; i++)
        {
            if (m_Players[i].GetComponent<PlayerGameplay>().IsDiseased)
            {
                diseasedPlayers++;
            }
        }
        if(diseasedPlayers == m_Players.Count - 1)
        {
            canTrasmit = false;
        }
        return canTrasmit;
    }

    Player FindPlayerUsingDevice( InputDevice inputDevice )
	{
		var playerCount = m_Players.Count;
		for (var i = 0; i < playerCount; i++)
		{
			var player = m_Players[i];
			if (player.Device == inputDevice)
			{
				return player;
			}
		}

		return null;
	}

    public Player FindPlayerWithIndex(int index)
    {
        var playerCount = m_Players.Count;
        for (var i = 0; i < playerCount; i++)
        {
            var player = m_Players[i];
            if (player.m_PlayerIndex == index)
            {
                return player;
            }
        }

        return null;
    }

    void CreatePlayer(PersistantPlayerInfo persPlayer )
	{
        var playerPosition = playerStartPositions[NumberOfPlayers];

        var gameObject = (GameObject) Instantiate( playerPrefab, playerPosition, Quaternion.identity );
        var player = gameObject.GetComponent<Player>();
        player.Device = persPlayer.Device;
        player.DefaultColor = persPlayer.Color;
        player.m_PlayerName = persPlayer.Name;
        player.m_PlayerIndex = NumberOfPlayers;
        player.m_PersistentInfo = persPlayer;
        m_Players.Add( player );
        NumberOfPlayers++;

    }

	void RemovePlayer( Player player )
	{
        //playerStartPositions.Insert( 0, player.transform.position );
		m_Players.Remove( player );
		player.Device = null;
		Destroy( player.gameObject );
	}
    
    public void RegisterSpawnPoint(Transform t, int index)
    {
        playerStartPositions[index] = t.position;
    }

    /*
    void OnGUI()
    {
        const float h = 32.0f;
        var y = 10.0f;
        int MaxPlayers = GameManager.Instance.m_PersistantGameInfo.MaxPlayers;
        int PlayerCount = GameManager.GetPersistantGameInfo().ActivePlayers.Count;

        foreach (Player player in m_Players)
        {
            GUI.Label(new Rect(10, y, 300, y + h), player.m_PlayerName + " : " + player.GetComponent<PlayerGameplay>().m_CurrentScore );
            y += h;
        }
    }
    */
}
