using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AssembleItemsManager : MonoBehaviour
{
	public GameObject[] itemsPrefabs;
	public GameObject[] missingItemsPrefabs;

	public Transform containerMissing;
	public Transform containerGenerated;

	private GameObject[] objectivesToCompleteGO;
	public bool[] objectivesCompleted;

	public float timeWaitingAfterFinish = 1f;

	public Vector2 LocationToCreateItem;

	private List<GameObject> listGos;
	private int completedItems;

	private static AssembleItemsManager instance;
	public static AssembleItemsManager Instance {
		get {
			return instance;
		}
	}

	private void Awake() {
		instance = this;
	}

	private void Start() {
		listGos = new List<GameObject>();
	}

	public void ClearAll() {
		for (int x = 0; x < listGos.Count; ++x) {
			Destroy(listGos[x]);
		}
		listGos.Clear();
		for (int x = 0; x < objectivesToCompleteGO.Length; ++x) {
			Destroy(objectivesToCompleteGO[x]);
		}
	}

	public void CreateObjective(Vector3[] locations, ResourceType[] instructions) {
		completedItems = 0;
		objectivesCompleted = new bool[locations.Length];
		objectivesToCompleteGO = new GameObject[locations.Length];
		for (int x = 0; x < locations.Length; ++x) {
			objectivesToCompleteGO[x] = Instantiate(missingItemsPrefabs[(int)instructions[x]], containerMissing);
			objectivesToCompleteGO[x].transform.localPosition = locations[x];
			objectivesCompleted[x] = false;
			objectivesToCompleteGO[x].GetComponent<RequirementItem>().resourceRequirement = instructions[x];
		}
	}

	public void SetCompleted(int value) {
		bool checkComplete = true;
		objectivesCompleted[completedItems] = true;
		completedItems++;
		for (int x = 0; x < objectivesCompleted.Length; ++x) {
			if (!objectivesCompleted[x]) {
				checkComplete = false;
			}
		}
		if (checkComplete) {
			StartCoroutine(WaitForSomeSeconds());
		}
	}

	IEnumerator WaitForSomeSeconds() {
		yield return new WaitForSeconds(timeWaitingAfterFinish);
		/*
		 MAKE CURRENT ITEM SHOWN
		 */
		KangarooGameManager.Instance.CompletedLevel();
	}

	public void GenerateNewItem(ResourceType type) {
		GameObject newItem = Instantiate(itemsPrefabs[(int)type], containerGenerated);
		newItem.GetComponent<AssembleItem>().resourceType = type;
		newItem.GetComponent<RectTransform>().localPosition = LocationToCreateItem;
		listGos.Add(newItem);
	}

	public void StartGame() {

	}
}
