using UnityEngine;
using System.Collections;

public class ScorePoint : MonoBehaviour
{
	[SerializeField]
	float pointsPerSecond;

	Player activePlayer;
	int numPlayers;
	SpriteRenderer renderer;

	// Use this for initialization
	void Start ()
	{
		numPlayers = 0;
		activePlayer = null;
		renderer = GetComponent<SpriteRenderer>();
	}
	
	// Update is called once per frame
	void Update ()
	{
		if (numPlayers == 1)
		{
			//*
			Debug.Log("Add score " + (pointsPerSecond * Time.deltaTime));
            if (activePlayer)
            {
                activePlayer.AddScore(pointsPerSecond * Time.deltaTime);
            }
            
			//*/
		}
	}

	private void OnTriggerEnter2D(Collider2D collision)
	{
		//Debug.Log("Trigger enter");

		Player enterPlayer = collision.gameObject.GetComponent<Player>();

		if (enterPlayer)
		{
			//Debug.Log("Player found");
            /*
			if (numPlayers == 0)
			{
				//Debug.Log("New active player");
				SetActivePlayer(enterPlayer);
			}
			else
			{
				SetContested();
			}
            //*/
			++numPlayers;
		}
	}

	private void OnTriggerExit2D(Collider2D collision)
	{
		Player exitPlayer = collision.gameObject.GetComponent<Player>();
		
		if (exitPlayer)
		{
            Debug.Log("Player left");
			--numPlayers;
            SetContested();
            /*
			if (numPlayers == 0 || numPlayers >= 2)
			{
                Debug.Log("Num players left" + numPlayers);
				SetContested();
			}
			else if (numPlayers == 1)
			{
				Debug.Log("Double check the OnTriggerExit2D of ScorePoint script, case player exits and 1 player remains");
                //Find remaining player
                BoxCollider2D myBox = GetComponent<BoxCollider2D>();
                //Collider2D hit = Physics2D.OverlapBox(myBox.transform.position, new Vector2(myBox.size.x+0.5f, myBox.size.y+0.5f), 0.0f);
                Collider2D hit = Physics2D.OverlapArea(new Vector2(myBox.transform.position.x - (myBox.size.x / 2.0f), myBox.transform.position.y - (myBox.size.y / 2.0f)),
                    new Vector2(myBox.transform.position.x + (myBox.size.x / 2.0f), myBox.transform.position.y + (myBox.size.y / 2.0f)));
                Player remainingPlayer = hit.gameObject.GetComponent<Player>();
                Debug.Log("OverlapBox is " + remainingPlayer);
                if (remainingPlayer)
                {
                    SetActivePlayer(remainingPlayer);
                }
                
			}
            //*/
		}

	}

    void OnTriggerStay2D(Collider2D collision)
    {
        Player inPlayer = collision.gameObject.GetComponent<Player>();

        if (numPlayers == 1 && inPlayer)
        {
            SetActivePlayer(inPlayer);
        }
        else
        {
            SetContested();
        }
    }

	void SetActivePlayer(Player enterPlayer)
	{
		activePlayer = enterPlayer;
		renderer.color = activePlayer.GetPlayerColor();
		renderer.color = new Color(renderer.color.r, renderer.color.g, renderer.color.b, 0.3f);
	}

	void SetContested()
	{
		SetDefaultColor();
		activePlayer = null;
	}

	void SetDefaultColor()
	{
		renderer.color = new Color(1.0f, 1.0f, 1.0f, 0.3f);
	}
}
