//--------------------------------------
//               PowerUI
//
//        For documentation or 
//    if you have any issues, visit
//        powerUI.kulestar.com
//
//    Copyright  2013 Kulestar Ltd
//          www.kulestar.com
//--------------------------------------

using UnityEditor;
using UnityEngine;
using System.IO;
using Dom;


namespace PowerUI{
	
	/// <summary>
	/// Allows component based creation of WorldUI's.
	/// </summary>
	
	public class CreateWorldUI:MonoBehaviour{
	
		/// <summary>Allows component based creation of a WorldUI.</summary>
		[MenuItem("GameObject/UI/PowerUI/In World UI")]
		[MenuItem("Assets/PowerUI/In World UI")]
		public static void WorldUI(){
			
			// Use a plane to describe the size/ shape of the WorldUI:
			GameObject plane=GameObject.CreatePrimitive(PrimitiveType.Plane);
			
			// We'll name it with something distinctive:
			plane.name="My World UI";
			
			// Remove collider:
			MeshCollider mc=plane.GetComponent<MeshCollider>();
			
			if(mc!=null){
				GameObject.DestroyImmediate(mc);
			}
			
			// Set default rotation:
			plane.transform.rotation=Quaternion.Euler(270f,180f,0f);
			
			// Add a WorldUI Helper. It'll instance the WorldUI object when the game starts up
			// and destroy the mesh renderer/ filters.
			// (which is generally used from scripting anyway).
			plane.AddComponent<WorldUIHelper>();
			
			// Get the path to PowerUI:
			string powerUIPath=PowerUIEditor.GetPowerUIPath();
			
			if(string.IsNullOrEmpty(powerUIPath)){
				
				NoMaterial();
				
			}else{
				 
				// Use the WorldUI Editor material:
				Material mat=AssetDatabase.LoadAssetAtPath(powerUIPath+"/Editor/worldUIMaterial.mat",typeof(Material)) as Material;
				
				if(mat==null){
					
					NoMaterial();
					
				}else{
					
					// Apply it:
					plane.GetComponent<MeshRenderer>().material=mat;
					
				}
				
			}
			
		}
		
		private static void NoMaterial(){
			
			Debug.Log("Couldn't find the default material for WorldUI preview (Doesn't matter).");
			
		}
		
	}
	
}