using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class TooltipPanel : MonoBehaviour {

	static TooltipPanel current;
	Tooltip displaying=null;

	Text text;
	Text cost;

	// Use this for initialization
	void Start () {
		current = this;
		text = transform.Find ("Text").GetComponent<Text>();
		cost = transform.Find ("Cost").GetComponent<Text>();

		current.gameObject.SetActive(false);
	}

	void Update() {
		if(displaying == null)
			gameObject.SetActive(false);

/*		CanvasScaler scaler = GetComponentInParent<CanvasScaler>();
		this.GetComponent<RectTransform>().anchoredPosition = new Vector2(Input.mousePosition.x * scaler.referenceResolution.x / Screen.width, Input.mousePosition.y * scaler.referenceResolution.y / Screen.height);

*/		

		Canvas myCanvas = GetComponentInParent<Canvas>();
		Vector2 pos;
		RectTransformUtility.ScreenPointToLocalPointInRectangle(myCanvas.transform as RectTransform, Input.mousePosition, myCanvas.worldCamera, out pos);
		transform.position = myCanvas.transform.TransformPoint(pos);



		UpdateText();


	}

	void UpdateText() {
		current.text.text = displaying.text;
		
		if(displaying.GetComponent<Soulzooka>()) {
			current.text.text += "\nCurrent Soul Level: " + displaying.GetComponent<Soulzooka>().CurrentSoulLevel();
		}
		else if(displaying.GetComponent<Turret>()) {
			current.text.text += "\nCurrent DPS: " + displaying.GetComponent<Turret>().CurrentDPS();
			
		}
		
		if(displaying.cost > 0) {
			current.cost.text = "$" + string.Format("{0:n0}", displaying.cost);
			current.cost.gameObject.SetActive(true);
		}
		else {
			current.cost.gameObject.SetActive(false);
		}

	}

	public static void Show(Tooltip t) {
		current.gameObject.SetActive(true);
		current.displaying = t;

		current.UpdateText();

	}

	public static void Hide(Tooltip t) {
		if(current.displaying == t) {
			current.gameObject.SetActive(false);
		}
	}

}
