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

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using PowerUI;


namespace Css{
	
	public class ShaderSetFunction : CssFunction{
		
		/// <summary>The cached shader family in use.</summary>
		public ShaderSet Shaders;
		
		
		public ShaderSetFunction(){
			
			Name="shaderset";
			Type=ValueType.Text;
			
		}
		
		public ShaderSetFunction(string setName){
			
			Name="shaderset";
			Type=ValueType.Text;
			Values=new Value[]{new Units.TextUnit(setName)};
			Set(setName);
			
		}
		
		public override string[] GetNames(){
			return new string[]{"shaderset"};
		}
		
		protected override Css.Value Clone(){
			ShaderSetFunction result=new ShaderSetFunction();
			result.Values=CopyInnerValues();
			return result;
		}
		
		public override void OnValueReady(CssLexer lexer){
			
			string name=this[0].Text.Trim();
			
			Set(name);
			
		}
		
		public void Set(string name){
			
			string lower=name.ToLower();
			
			if(lower=="" || lower=="none" || lower=="standard"){
				
				// Standard shader set:
				Shaders=PowerUI.ShaderSet.Standard;
				
			}else if(lower=="standard lit"){
				
				// Standard lit shader set:
				Shaders=PowerUI.ShaderSet.StandardLit;
				
			}else{
				
				// Get the shader set:
				Shaders=PowerUI.ShaderSet.Get(name);
				
			}
			
		}
		
	}
	
}

namespace PowerUI{
	
	/// <summary>
	/// Represents a family of shaders. Globally cached to minimise memory usage.
	/// </summary>
	
	public class ShaderSet{
		
		/// <summary>The standard shader set. Use Standard instead.</summary>
		private static ShaderSet CachedStandard;
		/// <summary>The standard lit shader set. Use StandardLit instead.</summary>
		private static ShaderSet CachedStandardLit;
		/// <summary>The cache of shader sets.</summary>
		public static Dictionary<string,ShaderSet> GlobalCache;
		
		
		/// <summary>The standard shader set.</summary>
		public static ShaderSet Standard{
			get{
				if(CachedStandard==null){
					CachedStandard=Get("StandardUI");
				}
				
				return CachedStandard;
			}
		}
		
		/// <summary>The standard shader set (lit).</summary>
		public static ShaderSet StandardLit{
			get{
				if(CachedStandardLit==null){
					CachedStandardLit=Get("StandardUI Lit");
				}
				
				return CachedStandardLit;
			}
		}
		
		/// <summary>Gets a shader set by name.</summary>
		public static ShaderSet Get(string name){
			
			ShaderSet result=null;
			
			if(GlobalCache==null){
				// Create the global cache:
				GlobalCache=new Dictionary<string,ShaderSet>();
			}else{
				
				// Load from cache:
				GlobalCache.TryGetValue(name,out result);
				
			}
			
			if(result==null){
				
				// Create and add:
				result=new ShaderSet(name);
				
				// Push it in:
				GlobalCache[name]=result;
				
			}
			
			return result;
			
		}
		
		/// <summary>True if this set uses lighting.</summary>
		public bool UsesLighting;
		/// <summary>The name of this shader set.</summary>
		public string Name;
		/// <summary>The normal use shader.</summary>
		private Shader CachedNormal;
		/// <summary>The extruded shader.</summary>
		private Shader CachedExtruded;
		/// <summary>The default isolation shader.</summary>
		private Shader CachedIsolated;
		
		
		/// <summary>Creates the shader set for the given family name.</summary>
		private ShaderSet(string name){
			Name=name;
			UsesLighting=name.ToLower().Contains(" lit");
		}
		
		/// <summary>Gets the normal purpose shader.</summary>
		public Shader Normal{
			get{
				
				if(CachedNormal==null){
					// Load it now:
					CachedNormal=GetShader("Normal");
				}
				
				return CachedNormal;
			}
		}
		
		/// <summary>Gets the extrusion shader.</summary>
		public Shader Extruded{
			get{
				
				if(CachedExtruded==null){
					// Load it now:
					CachedExtruded=GetShader("Extruded");
				}
				
				return CachedExtruded;
			}
		}
		
		/// <summary>Gets the isolation shader.</summary>
		public Shader Isolated{
			get{
				
				if(CachedIsolated==null){
					// Load it now:
					CachedIsolated=GetShader("Isolated");
				}
				
				return CachedIsolated;
			}
		}
		
		/// <summary>Gets a particular variant of this shader.</summary>
		public Shader GetShader(string extraName){
			
			return Shader.Find("PowerUI/"+Name+"/"+extraName);
			
		}
		
	}
	
}