﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class ViewManager : MonoBehaviour {

	[System.Serializable]
	public class ViewEvent : UnityEvent<int> {}

	public List<View> uiList;

	public ViewEvent onViewOpen;
	public ViewEvent onViewClose;

	public View GetView(int index) {
		if (uiList.Count - 1 < index) {
			Debug.LogWarning("[UIManager] The index is out of bounds. index : " + index);
			return null;
		}

		return uiList[index];
	}

	public void Invalidate() {
		foreach (View ui in uiList) {
			ui.Invalidate();
		}
	}

	public void Invalidate(int index) {
		if (uiList.Count - 1 < index) {
			Debug.LogWarning("[UIManager] The index is out of bounds. index : " + index);
			return;
		}

		uiList[index].Invalidate();
	}

	public void Open(int index) {
		if (uiList.Count - 1 < index) {
			Debug.LogWarning("[UIManager] The index is out of bounds. index : " + index);
			return;
		}

		uiList[index].SetActive(true);

		if (onViewOpen != null) {
			onViewOpen.Invoke(index);
		}
	}

	public void Close(int index) {
		if (uiList.Count - 1 < index) {
			Debug.LogWarning("[UIManager] The index is out of bounds. index : " + index);
			return;
		}

		uiList[index].SetActive(false);

		if (onViewClose != null) {
			onViewClose.Invoke(index);
		}
	}
}
