using UnityEngine;
using System.Collections;

public class FadeToTrans : MonoBehaviour {

	public enum FadeState{
		undiscovered,
		discovering,
		discovered,
		permanent
	}

	public enum RespondsTo{
		player0,
		player1,
		anyPlayer,
		bothPlayers
	}

	public FadeState fadeState = FadeState.undiscovered;

	public float fadeStart = 0f;
	public float fadeTime = 10f;
	public float fadeFrac = 1f;

	public bool discovering = false;
	public float discoveryTime = 0f;
	public float timeToDiscover = 2f;
	public bool isDiscovered = false;

	public int beingObserved = 0;
	public int observedByPlayer = 0;

	public int fadeLimit = 1;

	public Color player0Color;
	public Color player1Color;
	public Color bothColor;
	public Color anyColor;

	public Texture2D p0Texture;
	public Texture2D p1Texture;
	public Texture2D bothTexture;
	public Texture2D anyTexture;

	public Color myColor = new Color(255,255,255,255);

	public RespondsTo respondsTo = RespondsTo.anyPlayer;

	// Use this for initialization
	void Start () {
		fadeStart = Time.time;
		gameObject.collider.isTrigger = true;
		if(respondsTo == RespondsTo.bothPlayers)fadeLimit = 2;
		switch(respondsTo){
			case RespondsTo.anyPlayer:
				myColor = anyColor;
				renderer.material.mainTexture = anyTexture;
				break;
			case RespondsTo.bothPlayers:
				myColor = bothColor;
				renderer.material.mainTexture = bothTexture;
				break;
			case RespondsTo.player0:
				myColor = player0Color;
				renderer.material.mainTexture = p0Texture;
				break;
			case RespondsTo.player1:
				myColor = player1Color;
				renderer.material.mainTexture = p1Texture;
				break;
			}
		//gameObject.renderer.material.color = myColor;
		Debug.Log("Start done");
	}
	
	// Update is called once per frame
	void Update () {
		Color col = gameObject.renderer.material.color;

		if(beingObserved >= fadeLimit && fadeState == FadeState.discovered)
		{
			Reset();
		}	

		if(beingObserved >= fadeLimit && fadeState == FadeState.undiscovered)
		{
			gameObject.collider.isTrigger = false;
			discoveryTime = Time.time;
			fadeState = FadeState.discovering;
		}

		if(fadeState == FadeState.discovering){
			if(Time.time-discoveryTime > timeToDiscover)
			{
				fadeState = FadeState.discovered;
				fadeStart = Time.time;
			}
		}

		if(fadeState == FadeState.discovered){
			fadeFrac = 1-(Time.time-fadeStart)/fadeTime;
			gameObject.renderer.material.color = new Color(col.r,col.b,col.g,fadeFrac);
			if(fadeFrac <= 0)
			{
				gameObject.SetActive(false);
			}
		} else if(fadeState == FadeState.discovering){
			fadeFrac = (Time.time-discoveryTime)/timeToDiscover;
			gameObject.renderer.material.color = new Color(col.r,col.b,col.g,fadeFrac);
		} else {
			gameObject.renderer.material.color = new Color(col.r,col.b,col.g,0f);
		}
	}

	void OnTriggerEnter(Collider col){
		if(col.gameObject.GetComponent<Observer>() != null){
		switch(respondsTo){
			case RespondsTo.anyPlayer:
				if(col.gameObject.GetComponent<Observer>().playerNum == 0 || col.gameObject.GetComponent<Observer>().playerNum == 1)beingObserved++;
				break;
			case RespondsTo.player0:
				if(col.gameObject.GetComponent<Observer>().playerNum == 0)beingObserved++;
				break;
			case RespondsTo.player1:
				if(col.gameObject.GetComponent<Observer>().playerNum == 1)beingObserved++;
				break;
			case RespondsTo.bothPlayers:
				if(col.gameObject.GetComponent<Observer>().playerNum == 0 || col.gameObject.GetComponent<Observer>().playerNum == 1)beingObserved++;
				break;
			}
		}
	}
	
	void OnTriggerExit(Collider col){
		if(col.gameObject.GetComponent<Observer>() != null){
			switch(respondsTo){
			case RespondsTo.anyPlayer:
				if(col.gameObject.GetComponent<Observer>().playerNum == 0 || col.gameObject.GetComponent<Observer>().playerNum == 1)beingObserved--;
				break;
			case RespondsTo.player0:
				if(col.gameObject.GetComponent<Observer>().playerNum == 0)beingObserved--;
				break;
			case RespondsTo.player1:
				if(col.gameObject.GetComponent<Observer>().playerNum == 1)beingObserved--;
				break;
			case RespondsTo.bothPlayers:
				if(col.gameObject.GetComponent<Observer>().playerNum == 0 || col.gameObject.GetComponent<Observer>().playerNum == 1)beingObserved--;
				break;
			}
		}
	}

	public void Reset(){
		fadeStart = Time.time;
	}
}
