using System;
using UnityEditor.VersionControl;
using UnityEngine;
using UnityEngine.UI;

namespace Cryville.Crtr {
	public class Popup : MonoBehaviour {
		public string Message = "";
		CanvasGroup group;
		float timer = 0;

		const float DURATION = 5.0f;
		const float DURIN = 0.4f;
		const float DUROUT = 0.4f;

		void Start() {
			group = GetComponent<CanvasGroup>();
			group.alpha = 0;
			GetComponentInChildren<Text>().text = Message;
			transform.SetParent(GameObject.Find("PopupList").transform);
		}

		void Update() {
			if (timer <= DURIN) group.alpha = timer / DURIN;
			else if (timer >= DURATION) GameObject.Destroy(gameObject);
			else if (timer >= DURATION - DUROUT) group.alpha = (DURATION - timer) / DUROUT;
			timer += Time.deltaTime;
		}


		public static void CreateException(Exception ex) {
			Create(ex.Message);
		}

		public static void Create(string msg) {
			Instantiate(Resources.Load<GameObject>("Common/Popup")).GetComponent<Popup>().Message = msg;
		}
	}
}
