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

public class EditorHelper : Editor
{
	private const string ROOT = "Helper/";
	
	[MenuItem("GameObject/Create Other/Custom Plane")]
	private static void CreatePlane()
	{
		Selection.activeGameObject = MeshUtility.CreatePlane(MeshUtility.HorizontalAnchor.Center, MeshUtility.VerticalAnchor.Center, true);
	}
	
	[MenuItem("GameObject/Combine Children Mesh")]
	private static void CombineChildren()
	{
		if (Selection.activeGameObject == null)
		{
			Debug.Log ("Select a GameObject first");
			return;
		}
		MeshUtility.CombineChildren(Selection.activeGameObject, false, false);
	}
	
	[MenuItem("Assets/Create Flipped Mesh")]
	private static void CreateFlippedMesh()
	{
		if (Selection.activeObject == null)
		{
			Debug.Log ("Select an Object first");
			return;
		}
		Mesh mesh = null;
		if (Selection.activeObject is Mesh)
		{
			mesh = (Mesh)Selection.activeObject;
		}
		else
		{
			if (Selection.activeGameObject == null)
			{
				Debug.Log ("Select an Object first");
				return;
			}
			MeshFilter mf = Selection.activeGameObject.GetComponent<MeshFilter>();
			if (mf == null)
			{
				Debug.Log ("Object must have a Mesh attached to it");
				return;
			}
			mesh = mf.sharedMesh;
		}
		Util.AssertTrue(mesh != null, "'mesh' must be non-null");
		
		string filePath = EditorUtility.SaveFilePanelInProject("Save Procedural Mesh", mesh.name + "-flipped", "asset", "");
		AssetDatabase.CreateAsset(mesh.Flip(), filePath);
	}
	
	[MenuItem(ROOT + "Get Object Size")]
	private static void GetObjectSize()
	{
		if (Selection.activeGameObject == null)
		{
			Debug.Log ("Select a GameObject first");
			return;
		}
		string formatX = "X Size of '{0}': {1}";
        string formatY = "Y Size of '{0}': {1}";
        string formatZ = "Z Size of '{0}': {1}";
		foreach (var obj in Selection.gameObjects) 
        { 
            Debug.Log(string.Format(formatX, obj.name, MeshUtility.GetSize(obj, true, false).x));
            Debug.Log(string.Format(formatY, obj.name, MeshUtility.GetSize(obj, true, false).y));
            Debug.Log(string.Format(formatZ, obj.name, MeshUtility.GetSize(obj, true, false).z)); 
        }
	}
	
	[MenuItem(ROOT + "Check Random Stuff")]
	private static void Check()
	{
		string filePath = EditorUtility.SaveFilePanelInProject("Save Dynamic Object", "", "asset", "");
		
		//AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<VariableSpawnableSpawnChancesConfigs>(), filePath);
	}
}
