using UnityEngine;
using System.Collections;

public class InterestPointController : MonoBehaviour
{
	static InterestPointController prototype = null;

	SpriteRenderer sprite = null;// Set from editor
	public SphereCollider selfCollider = null;// Set from editor
	StarMap.InterestPoint currentPoint = null;

	public static InterestPointController Create(Transform parent, StarMap.InterestPoint point)
	{
		if (prototype == null)
		{
			Debug.LogError("Interest point marker prototype not created");
			return null;
		}
		GameObject marker = Instantiate (prototype.gameObject);
		marker.SetActive(true);
		marker.transform.parent = parent;
		marker.transform.localPosition = point.position * 10f + point.position.normalized * 0.05f;

		InterestPointController controller = marker.GetComponent<InterestPointController> ();
		controller.currentPoint = point;
		controller.Init();
		return controller;
	}

	void Init()
	{
		selfCollider = gameObject.GetComponent<SphereCollider>();
		sprite = gameObject.GetComponentInChildren<SpriteRenderer>();
		if (sprite != null)
		{
			sprite.transform.localScale = Vector3.zero;
		}
		
		if (!Player.IsPointVisited(currentPoint.id))
		{
			if (currentPoint.visible == Player.info.scannerPower + 1)
			{
				AnimateInterpolarion(0.5f, Color.gray);
			}
			if (currentPoint.visible <= Player.info.scannerPower || Player.IsPointDiscovered(currentPoint.id))
			{
				AnimateInterpolarion(1f, GetTypeColor(currentPoint.type));
			}
		}
	}

	public void UpdateState()
	{
		if (!Player.IsPointVisited (currentPoint.id))
		{
			if (currentPoint.visible == Player.info.scannerPower + 1)
			{
				AnimateInterpolarion(0.5f, Color.gray);
			}
			if (currentPoint.visible <= Player.info.scannerPower || Player.IsPointDiscovered(currentPoint.id))
			{
				AnimateInterpolarion(1f, GetTypeColor(currentPoint.type));
			}
		}
	}

	Color GetTypeColor(PointType type)
	{		
		switch(type)
		{
		case PointType.resoursePoint:
			return Color.white;
		case PointType.biologycalPoint:
			return Color.green;
		case PointType.artificalPoint:
			return Color.blue;
		case PointType.energyPoint:
			return Color.yellow;
		case PointType.activePoint:
			return Color.red;
		}
		return Color.white;
	}

	void AnimateInterpolarion(float size, Color color, float time = 0.25f)
	{
		if (sprite != null)
		{
			float startSize = sprite.transform.localScale.x;
			Color startColor = sprite.color;
			Timer.Add(time, (anim) => {
				if (sprite != null)
				{
					sprite.transform.localScale = Vector3.one * Mathf.Lerp(startSize, size, anim);
					sprite.color = Color.Lerp(startColor, color, anim);
				}
			}, () => {
				if (sprite != null)
				{
					sprite.transform.localScale = Vector3.one * size;
					sprite.color = color;
				}
			});
		}
	}

	void AddResource(string resourceName, int count)
	{
		Logic.triggers [resourceName] += count;
	}

	public void Collect()
	{
		switch (currentPoint.type)
		{
		case PointType.resoursePoint:
			if (Random.Range(0, 100) < 50)
			{
				AddResource(Res.rareMaterials, Random.Range(5, 30));
			}
			else
			{
				AddResource(Res.transuranicMaterials, Random.Range(1, 15));
			}
			break;
		case PointType.biologycalPoint:
			AddResource(Res.biologicalMaterials, Random.Range(1, 2));
			break;
		case PointType.energyPoint:
			if (Random.Range(0, 100) < 50)
			{
				AddResource(Res.rareMaterials, Random.Range(1, 2));
			}
			else
			{
				AddResource(Res.transuranicMaterials, Random.Range(5, 20));
			}
			break;
		case PointType.artificalPoint:
			AddResource(Res.artificalMaterials, Random.Range(1, 2));
			break;
		case PointType.activePoint:
			break;
		}
		AnimateInterpolarion (0, GetTypeColor(currentPoint.type));
		Destroy(gameObject, 0.5f);
	}

	void Awake ()
	{
		if (prototype == null)
		{
			prototype = this;
			gameObject.SetActive (false);
		}
	}

	void Start ()
	{
		if (sprite == null)
		{
			sprite = GetComponentInChildren<SpriteRenderer>();
		}
	}

}
