using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;


[ExecuteInEditMode]
public class CreateLevelPrefab : MonoBehaviour {
	[HideInInspector]
	public GameObject defaultLevel;
	public string levelName;
	[BoolToButton("makeLevel")]
	public bool makeLevel = false;
	// Here for spacing rather than actual functionality
	[BoolToButton("")]
	public bool destroyMe = false;

	const string randomChars = "BbCcDdFfGgHhJjKkLlMmNnPpQqRrSsTtVvWwXxYyZz0123456789";

	void Awake () {
		HideHierarchyElements ();
	}


	void OnValidate () {
		//makeLevel = false;

		if (!defaultLevel) {
			defaultLevel = (GameObject) AssetDatabase.LoadAssetAtPath ("Assets/Do Not Edit OR Delete/Terrains/Prefabs/In Use/Scene Setup/Default Level Prefab.prefab", typeof(GameObject));
		}

		if (gameObject.activeInHierarchy && makeLevel && defaultLevel) {

			string additionalChars = "";

			for (int i = 0; i < 6; i++) {
				additionalChars += randomChars [Random.Range (0, randomChars.Length)];
			}

			GameObject myLevel = GameObject.Instantiate (defaultLevel);
			myLevel.name = levelName + "_" + additionalChars;
			TerrainData myData = new TerrainData ();
			myData.size = new Vector3 (50, 400, 50);
			myData.heightmapResolution = 257;
			myData.baseMapResolution = 1024;
			myData.SetDetailResolution (1024, 8);

			AssetDatabase.CreateAsset (myData, "Assets/Do Not Edit OR Delete/Terrains/" + levelName + "_" + additionalChars + " Terrain.asset");
			GameObject myTerrain = Terrain.CreateTerrainGameObject (myData);
			myTerrain.name = levelName + " Terrain";
			myTerrain.transform.SetParent (myLevel.transform);

			string[] tGuids = AssetDatabase.FindAssets("t:Texture2D", new string[] { "Assets/Textures/Terrain Textures" });
			TerrainData checkData = myTerrain.GetComponent<Terrain> ().terrainData;
			SplatPrototype[] terrainTextures = new SplatPrototype[tGuids.Length];

			for (int t = 0; t < tGuids.Length; t++) {
				string myAssetPath = AssetDatabase.GUIDToAssetPath(tGuids[t]);
				terrainTextures [t] = new SplatPrototype ();
				terrainTextures [t].texture = AssetDatabase.LoadAssetAtPath<Texture2D> (myAssetPath);
			}

			int terrainX = checkData.heightmapWidth;
			int terrainY = checkData.heightmapHeight;
			Debug.Log (terrainX + ", " + terrainY);
			float[,] heights = checkData.GetHeights (0, 0, terrainX, terrainY);

			for (int x = 0; x < terrainX; x++) {
				for (int y = 0; y < terrainY; y++) {
					heights [x, y] = 0.5f;
				}
			}

			myTerrain.GetComponent<Terrain> ().terrainData.SetHeights (0, 0, heights);
			myTerrain.GetComponent<Terrain> ().terrainData.splatPrototypes = terrainTextures;
			destroyMe = true;

			UnityEditor.EditorApplication.delayCall += () => {DestroyImmediate(gameObject);};

		}
	}
	void Update () {
		if (transform.childCount > 0) {
			foreach (Transform child in transform) {
				child.parent = null;
			}
		}
	}

	void HideHierarchyElements () {
		GameObject canvases = GameObject.FindGameObjectWithTag ("Safe");
		canvases.hideFlags = HideFlags.HideInHierarchy;
	}
}
#endif
