//------------------------------------------------------------------------------
//
// Copyright (c) 2002-2006 CodeSmith Tools, LLC.  All rights reserved.
// 
// The terms of use for this software are contained in the file
// named sourcelicense.txt, which can be found in the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
// 
// You must not remove this notice, or any other, from this software.
//
//------------------------------------------------------------------------------

using System;
using System.Reflection;
using CodeSmith.Engine;

namespace CodeSmith.Samples
{
	/// <summary>
	/// This class provides CodeSmith serialization support for DropDownEditorProperty.
	/// </summary>
	public class DropDownEditorPropertySerializer : IPropertySerializer
	{
		public DropDownEditorPropertySerializer()
		{
		}
		
		/// <summary>
		/// This method will be used to save the property value when a template is being compiled.
		/// </summary>
		/// <param name="propertyInfo">Information about the target property.</param>
		/// <param name="propertyValue">The property to be saved.</param>
		/// <returns>An object that will be stored in a Hashtable during template compilation.</returns>
		public object SaveProperty(PropertyInfo propertyInfo, object propertyValue)
		{
			// Nothing special needs to be done to save this property so we just return the unmodified property value.
			return propertyValue;
		}
		
		/// <summary>
		/// This method will be used to restore the property value after a template has been compiled.
		/// </summary>
		/// <param name="propertyInfo">Information about the target property.</param>
		/// <param name="propertyValue">The property to be loaded.</param>
		/// <returns>The value to be assigned to the template property after it has been compiled.</returns>
		public object LoadProperty(PropertyInfo propertyInfo, object propertyValue)
		{
			// Nothing special needs to be done to load this property so we just return the unmodified property value.
			return propertyValue;
		}
		
		/// <summary>
		/// This method will be used when serializing the property value to an XML property set.
		/// </summary>
		/// <param name="propertyInfo">Information about the target property.</param>
		/// <param name="writer">The XML writer that the property value will be written to.</param>
		/// <param name="propertyValue">The property to be serialized.</param>
		public void WritePropertyXml(PropertyInfo propertyInfo, System.Xml.XmlWriter writer, object propertyValue)
		{
			if (propertyValue == null) return;
			
			DropDownEditorProperty dropDownEditorPropertyValue = propertyValue as DropDownEditorProperty;
			if (dropDownEditorPropertyValue != null)
			{
				writer.WriteElementString("SampleBoolean", dropDownEditorPropertyValue.SampleBoolean.ToString());
				writer.WriteElementString("SampleString", dropDownEditorPropertyValue.SampleString);
			}
		}
		
		/// <summary>
		/// This method will be used when deserializing the property from an XML property set.
		/// </summary>
		/// <param name="propertyInfo">Information about the target property.</param>
		/// <param name="propertyValue">The XML node to read the property value from.</param>
		/// <param name="basePath">The path to use for resolving file references.</param>
		/// <returns>The value to be assigned to the template property.</returns>
		public object ReadPropertyXml(PropertyInfo propertyInfo, System.Xml.XmlNode propertyValue, string basePath)
		{
			if (propertyInfo.PropertyType == typeof(DropDownEditorProperty))
			{
				DropDownEditorProperty dropDownEditorPropertyValue = new DropDownEditorProperty();
				dropDownEditorPropertyValue.SampleBoolean = Boolean.Parse(propertyValue.SelectSingleNode("SampleBoolean").InnerText);
				dropDownEditorPropertyValue.SampleString = propertyValue.SelectSingleNode("SampleString").InnerText;
				return dropDownEditorPropertyValue;
			}
			else
			{
				return null;
			}
		}
		
		/// <summary>
		/// This method will be used to parse a default value for a property when a template is being instantiated.
		/// </summary>
		/// <param name="propertyInfo">Information about the target property.</param>
		/// <param name="defaultValue">The default value.</param>
		/// <param name="basePath">The path to use for resolving file references.</param>
		/// <returns>An object that will be assigned to the template property.</returns>
		public object ParseDefaultValue(PropertyInfo propertyInfo, string defaultValue, string basePath)
		{
			if (propertyInfo.PropertyType == typeof(DropDownEditorProperty))
			{
				DropDownEditorProperty dropDownEditorPropertyValue = new DropDownEditorProperty();
				string[] values = defaultValue.Split(',');
				if (values.Length == 2)
				{
					dropDownEditorPropertyValue.SampleString = values[0];
					dropDownEditorPropertyValue.SampleBoolean = Boolean.Parse(values[1]);
					return dropDownEditorPropertyValue;
				}
				else
				{
					return null;
				}
			}
			else
			{
				return null;
			}
		}
	}
}
