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

using System;
using System.Collections;
using System.Collections.Generic;


namespace Dom{
	
	/// <summary>
	/// Represents a set of &variables; used for localization.
	/// </summary>
	
	public partial class VariableSet{
	
		/// <summary>The lookup of variable name to its content.</summary>
		public Dictionary<string,string> Map;
		
		
		/// <summary>Creates a new empty variable set.</summary>
		public VariableSet(){
			Map=new Dictionary<string,string>();
		}
		
		/// <summary>The number of entries in the set.</summary>
		public int Count{
			get{
				return Map.Count;
			}
		}
		
		/// <summary>Empties this variable set.</summary>
		public void Clear(){
			Map.Clear();
		}
		
		/// <summary>Adds a variable to this set.</summary>
		/// <param name="code">The name of the variable.</param>
		/// <param name="value">The value of this variable.</param>
		public void Add(string code,string value){
			SetValue(code,value);
		}
		
		/// <summary>Removes a variable by name.</summary>
		/// <param name="code">The name of the variable to remove.</param>
		public void Remove(string code){
			SetValue(code,null);
		}
		
		/// <summary>Checks if this variable set contains the named variable.</summary>
		/// <param name="code">The name of the variable to look for.</param>
		/// <returns>True if this variable was found in this set; false otherwise.</returns>
		public bool Contains(string code){
			return Map.ContainsKey(code);
		}
		
		/// <summary>Gets the value of the named variable from this set.</summary>
		/// <param name="code">The name of the variable to get the value of.</param>
		/// <returns>The value of the variable; null if it wasn't found.</returns>
		public string GetValue(string code){
			string result;
			if(!Map.TryGetValue(code+Dom.Text.VariableModifiers,out result)){
				Map.TryGetValue(code,out result);
			}
			return result;
		}
		
		/// <summary>Sets a value of the named variable into this set. Creates it if it doesn't exist.</summary>
		/// <param name="code">The name of the variable to set or create.</param>
		/// <param name="value">The value of the variable to set.</param>
		public void SetValue(string code,string value){
			if(value==null){
				Map.Remove(code);
			}else{
				Map[code]=value;
			}
		}
		
		/// <summary>Gets or sets (or creates if not found) the value of a named variable in this set.</summary>
		/// <param name="code">The name of the variable to get/set/create.</param>
		public string this[string code]{
			get{
				return GetValue(code);
			}
			set{
				SetValue(code,value);
			}
		}
		
	}
	
}