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

public class IntermissionController : MonoBehaviour {
	bool checkEnd;
	bool checkRestart;
	public GameObject restart;
	public GameObject ready;

	// Use this for initialization
	void Start () {
		checkEnd = false;
		checkRestart = false;
		ready.SetActive (true);
		restart.SetActive (false);
		Invoke ("Go", 1.5f);
		Invoke ("EarlyChecks", 60f);
	}

	void Go() {
		ready.SetActive (false);
	}
	
	// Update is called once per frame
	void Update () {
		if (checkEnd && GameObject.FindGameObjectsWithTag("GoodItem").Length <= 1) {
			checkEnd = false;
			EarlyChecks ();
		}
	}

	public void RunEndChecks() {
		checkEnd = true;
	}

	void ReadyRestart() {
		checkRestart = true;
		foreach (GameObject thing in GameObject.FindGameObjectsWithTag("GoodItem")) {
			Destroy (thing);
		}
		foreach (GameObject thing in GameObject.FindGameObjectsWithTag("BadItem")) {
			Destroy (thing);
		}
		ready.SetActive (false);
		restart.SetActive (true);
	}

	void EarlyChecks() {
		foreach (GameObject thing in GameObject.FindGameObjectsWithTag("Chair")) {
			Destroy (thing, Random.value);
		}
		Invoke ("ReadyRestart", 2.1f);
	}
}
