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


[ExecuteInEditMode]
public class CreateWand : MonoBehaviour {
	[HideInInspector]
	public GameObject broom, cane, claw, crystal, forrest, heart, moon, star, triCrystal, twig;
	public WandBase wandBase;
	public string wandName;
	[BoolToButton("makeWand")]
	public bool makeWand = false;
	// Here for spacing rather than actual functionality
	[BoolToButton("")]
	public bool destroyMe = false;

	const string randomChars = "BbCcDdFfGgHhJjKkLlMmNnPpQqRrSsTtVvWwXxYyZz0123456789";
	string[] removeChars = { "/", "\\", "\"", ":", "?", "<", ">", "|", "*", "~", ".", "@", "#", "$", "%", "^", "(", ")", "{", "}", "[", "]", "`", ";", ",", "!", "'", "+", "=" };

	GameObject selectedWand;
	GameObject myCreatedWand;

	public List<Object> customWands;


	void OnValidate () {

		LocateWandPrefabs ();

		if (wandName != "") {
			foreach (string c in removeChars) {
				wandName = wandName.Replace (c, string.Empty);
			}
		}

		if (makeWand && selectedWand) {

			string additionalChars = "";

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

			wandName = wandName + "_" + additionalChars;
				

			GameObject myWand = GameObject.Instantiate (selectedWand);
			myWand.name = wandName;
			/*
			//customWands.Add (customWand);
			*/

			myCreatedWand = myWand;

			makeWand = false;
			UnityEditor.EditorApplication.delayCall += () => {CreateCustomWandPrefab();};
		}
	}

	void CreateCustomWandPrefab () {
		Object customWand = PrefabUtility.CreateEmptyPrefab ("Assets/Prefabs/Resources/Wands/" + wandName + ".prefab");
		PrefabUtility.ReplacePrefab (myCreatedWand, customWand, ReplacePrefabOptions.ConnectToPrefab);
		wandName = "";
	}

	void LocateWandPrefabs () {
		if (!broom) {
			broom = (GameObject) AssetDatabase.LoadAssetAtPath ("Assets/Do Not Edit OR Delete/Resources/Wands/Broom Wand.prefab", typeof(GameObject));
		}
		if (!cane) {
			cane = (GameObject) AssetDatabase.LoadAssetAtPath ("Assets/Do Not Edit OR Delete/Resources/Wands/Cane Wand.prefab", typeof(GameObject));
		}
		if (!claw) {
			claw = (GameObject) AssetDatabase.LoadAssetAtPath ("Assets/Do Not Edit OR Delete/Resources/Wands/Claw Wand.prefab", typeof(GameObject));
		}
		if (!crystal) {
			crystal = (GameObject) AssetDatabase.LoadAssetAtPath ("Assets/Do Not Edit OR Delete/Resources/Wands/Crystal Wand.prefab", typeof(GameObject));
		}
		if (!forrest) {
			forrest = (GameObject) AssetDatabase.LoadAssetAtPath ("Assets/Do Not Edit OR Delete/Resources/Wands/Forrest Wand.prefab", typeof(GameObject));
		}
		if (!heart) {
			heart = (GameObject) AssetDatabase.LoadAssetAtPath ("Assets/Do Not Edit OR Delete/Resources/Wands/Heart Wand.prefab", typeof(GameObject));
		}
		if (!moon) {
			moon = (GameObject) AssetDatabase.LoadAssetAtPath ("Assets/Do Not Edit OR Delete/Resources/Wands/Moon Wand.prefab", typeof(GameObject));
		}
		if (!star) {
			star = (GameObject) AssetDatabase.LoadAssetAtPath ("Assets/Do Not Edit OR Delete/Resources/Wands/Star Wand.prefab", typeof(GameObject));
		}
		if (!triCrystal) {
			triCrystal = (GameObject) AssetDatabase.LoadAssetAtPath ("Assets/Do Not Edit OR Delete/Resources/Wands/Tri Crystal Wand.prefab", typeof(GameObject));
		}
		if (!twig) {
			twig = (GameObject) AssetDatabase.LoadAssetAtPath ("Assets/Do Not Edit OR Delete/Resources/Wands/Twig Wand.prefab", typeof(GameObject));
		}

		ChooseWandBase ();
	}

	void ChooseWandBase () {
		if (wandBase == WandBase.Broom) {
			selectedWand = broom;
		} else if (wandBase == WandBase.Cane) {
			selectedWand = cane;
		} else if (wandBase == WandBase.Claw) {
			selectedWand = claw;
		} else if (wandBase == WandBase.Crystal) {
			selectedWand = crystal;
		} else if (wandBase == WandBase.Forrest) {
			selectedWand = forrest;
		} else if (wandBase == WandBase.Heart) {
			selectedWand = heart;
		} else if (wandBase == WandBase.Moon) {
			selectedWand = moon;
		} else if (wandBase == WandBase.Star) {
			selectedWand = star;
		} else if (wandBase == WandBase.TriCrystal) {
			selectedWand = triCrystal;
		} else if (wandBase == WandBase.Twig) {
			selectedWand = twig;
		} else {
			selectedWand = null;
		}
	}

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

#endif

[System.Serializable]
public enum WandBase {Broom, Cane, Claw, Crystal, Forrest, Heart, Moon, Star, TriCrystal, Twig}
