using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;

/// <summary>
/// This script is no longer in use, should be deleted.
/// </summary>

[ExecuteInEditMode]
public class LevelEdgeGuard : MonoBehaviour {
	#if UNITY_EDITOR
	public Collider terrain;
	bool levelError;
	BoxCollider cube;
	List<BoxCollider> edges;
	List<Collider> objs;

	void Update () {

		if (EditorApplication.isPlaying) {
			if (levelError) {
				Debug.LogError ("LEVEL ERROR: The level cannot be tested because there is something wrong with the level format. Check to see if there is an object outside of the build area.");
				Debug.DebugBreak ();
			}
		} else {
			CheckLevelBounds ();
		}
	}

	void CheckLevelBounds () {
		cube = GetComponent<BoxCollider> ();
		if (cube) {
			levelError = false;
			edges.Clear ();
			for (int i = 0; i < cube.transform.childCount; i++) {
				edges.Add (cube.transform.GetChild (i).GetComponent<BoxCollider> ());
			}

			objs = FindObjectsOfType<Collider> ().ToList<Collider> ();

			foreach (Collider edge in edges) {
				objs.Remove (edge);
			}

			foreach (Collider obj in objs) {
				if (!cube.bounds.Contains (obj.transform.position)) {
					Debug.LogError ("NOT CONTAINED: The " + obj.name + " is outside the build area for the level. Please move the object inside the build area.");
					levelError = true;
					return;
				} else {
					levelError = false;
				}
			}

			foreach (Collider edge in edges) {
				foreach (Collider obj in objs) {
					if ((edge.bounds.Intersects (obj.bounds) && cube != obj)) {
						Debug.LogError ("INTERSECTING EDGE: The " + obj.name + " is extending outside the build area for the level and intersecting with the " + edge.name + ". Please move the object inside the build area.");
						levelError = true;
						return;
					} else {
						levelError = false;
					}
				}
			}
		} else {
			Debug.LogError ("NO BOUNDS: The out of bounds area is not defined, please revert the prefab to its original state by pressing the Revert button at the top of the inspector for this object.");
			levelError = true;
			return;
		}
	}

	void AdjustBoundSizes (BoxCollider levelBounds, List<BoxCollider> edgeBounds) {
		levelBounds.transform.position = Vector3.zero;
		levelBounds.transform.eulerAngles = Vector3.zero;
		levelBounds.transform.localScale = Vector3.one;
		levelBounds.size = Vector3.one * 400;
		for (int i = 0; i < edgeBounds.Count; i++) {
			edgeBounds [i].transform.position = Vector3.zero;
			edgeBounds [i].transform.eulerAngles = Vector3.zero;
			edgeBounds [i].transform.localScale = Vector3.one;
		}
	}
	#endif
}
