using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace B4Restore.Common.Control.BaseDS
{
	public sealed class DSAttributeParser
	{
		private static string[] customAttributes;

		static DSAttributeParser()
		{
			customAttributes = Enum.GetNames(typeof(AttributeType));
		}

		/// <summary>
		/// Gets the names of the currently defined custom attributes.
		/// </summary>
		public static string[] Attributes
		{
			get { return customAttributes; }
		}

		/// <summary>
		/// Processes the control's custom attributes.
		/// </summary>
		/// <param name="control">Current web control being rendered.</param>
		public static void ProcessControl(WebControl control)
		{
			foreach (string attribute in Attributes)
			{
				// Get the value of the custom attributes
				string attributeValue = control.Attributes[attribute];

				// If the custom attribute is defined, process it
				if (attributeValue != null)
				{
					// Get the attribute's type
					AttributeType at = (AttributeType)Enum.Parse(typeof(AttributeType), attribute, true);

					// Get the class to process the custom attribute
					IAttribute customAttribute = AttributeFactory.GetAttribute(at);

					if (customAttribute != null)
					{
						// Apply the attribute for the control
						customAttribute.Apply(attributeValue, control);
					}
				}
			}
		}
	}
}
