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

using System;
using InfiniText;


namespace PowerUI{
	
	/// <summary>
	/// This class provides graphical characters, such as for Emoji.
	/// Each one of these defines a range of unicode characters that it provides for.
	/// Any character found within it's defined range will be directed to the provider, but only if it wasn't found in the font.
	/// </summary>
	
	public class CharacterProvider{
		
		/// <summary>The minimum charcode of the range of characters this provides for.</summary>
		public int MinimumID;
		/// <summary>The maximum charcode of the range of characters this provides for.</summary>
		public int MaximumID;
		/// <summary>The path to attempt finding a character in. The characters must be .png files.</summary>
		public string Path;
		
		
		/// <summary>Creates a character provider for characters in the specified range.</summary>
		/// <param name="path">The URL where the characters are coming from.</param>
		/// <param name="minimum">The minimum charcode of the range of characters this provides for.</param>
		/// <param name="maximum">The maximum charcode of the range of characters this provides for.</param>
		public CharacterProvider(string path,int minimum,int maximum){
			MinimumID=minimum;
			MaximumID=maximum;
			Path=path;
		}
		
		/// <summary>Called when the given character was not found in the font.</summary>
		/// <param name="glyph">The character to try and find an alternate image for.</param.
		public ImagePackage Load(Glyph glyph,int charcode){
			
			string path=GetPath(charcode);
			
			if(string.IsNullOrEmpty(path)){
				return null;
			}
			
			// Create an image package and request for the graphic. Convert it to lowercase hex to match standard filenames:
			ImagePackage package=new ImagePackage(path,null);
			
			// Go get it, calling ImageReady when it's been retrieved (we only care about success here).
			package.onload=delegate(UIEvent e){
				
				// Tell it about the success:
				glyph.SetupImage(package);
				
			};
			
			package.send();
			
			return package;
		}
		
		/// <summary>Gets the path to the image for the given character code. 
		/// Override this if you want to define a custom location for your characters.</summary>
		/// <param name="charcode">The charcode of the character to look for.</param>
		/// <returns>The path to the charcode.</returns>
		public virtual string GetPath(int charcode){
			return Path+"/"+charcode.ToString("x2")+".png";
		}
		
	}

}