using UnityEngine;
using System.Collections;

public class GameStatusGUIManager : MonoBehaviour {

	public GameObject panel;
	public GameObject lifePrefab;
	public GameObject meterUnderBar;
	public GameObject meter;
	public GameObject leftcurtain;
	public GameObject rightcurtain;
	public GameObject allParent;
	public GameObject faceSmile;
	public GameObject faceCry;
	public GameObject faceHappy;

	private RectTransform meterHomePosition;
	private ArrayList lives;
	private float meterLeftPosition;
	private int oldLifeNum;
	private GameObject flashLife;
	private float deltaTime;
	private bool isActive;
	// Use this for initialization
	void Start () {
		lives = new ArrayList ();
		meterHomePosition = meter.GetComponent<RectTransform> ();
		meterLeftPosition = meterHomePosition.localPosition.x;
		faceSmile.SetActive (true);
		GameController gameController = FindObjectOfType<GameController> ();
		oldLifeNum = gameController.lives;
		deltaTime = 0;
	}
	
	// Update is called once per frame
	void Update () {
		GameController gameController = FindObjectOfType<GameController> ();
		for (int count = 0; count < lives.Count; count++) {
			if (!(oldLifeNum - 1 == count && oldLifeNum != gameController.lives)) { 
				((GameObject)lives [count]).transform.SetParent (null);
				Destroy (((GameObject)lives [count]));
			} else {
				flashLife = (GameObject)lives [count];
				Invoke("heartDestory", 1.0f);
			}

		}
		lives.Clear ();
		deltaTime += Time.deltaTime;
		if (flashLife != null && deltaTime > 0.1) {
			isActive = !isActive;
			flashLife.SetActive (isActive);
			deltaTime = 0;
		}

		for (int count = 0; count < gameController.lives; count++) {
			GameObject life = (GameObject)Instantiate (lifePrefab);
			life.transform.SetParent(panel.transform,false);
			lives.Add (life);
		}
		oldLifeNum = gameController.lives;
		float number = gameController.NumberOfStagesInCurrentPeriod;
		float stage = gameController.CurrentStageInCurrentPeriod;
		float progress = stage / number;
		Vector3 pos = meterHomePosition.localPosition;
		pos.x = meterLeftPosition + 1024 * progress;
		meterHomePosition.localPosition = pos;

	}

	public void OpenStage(){
		FaceHidden ();
		meter.SetActive (false);
		leftcurtain.GetComponent<Animator> ().SetTrigger ("openAnim");
		rightcurtain.GetComponent<Animator> ().SetTrigger ("openAnim");
		Invoke("DelayHiddenMethod", 0.5f);
	}

	public void FaceHidden(){
		faceSmile.SetActive (false);
		faceCry.SetActive (false);
		faceHappy.SetActive (false);
	}

	private void DelayHiddenMethod(){
		allParent.SetActive (false);
	}

	private void DelayDisplayMethod(){
		allParent.SetActive (true);
		meter.SetActive (true);
		GameController gameController = FindObjectOfType<GameController> ();
		if (gameController.success) {
			faceHappy.SetActive (true);
		} else {
			faceCry.SetActive (true);
		}

	}

	private void heartDestory(){
		flashLife.transform.SetParent (null);
		Destroy (flashLife);
		flashLife = null;
		Debug.Log ("destroy");
	}

	public void CloseStage(){
		Invoke("DelayDisplayMethod", 0.5f);
		leftcurtain.GetComponent<Animator> ().SetTrigger ("closeAnim");
		rightcurtain.GetComponent<Animator> ().SetTrigger ("closeAnim");
	}
}
