//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Text;
using System.Xml;

namespace CommunityServer.Components
{
	public class Template : IComparable
	{
		TemplateType type;
		int id;
		string title;
		string body;
		int sortOrder = 0;

		public Template(XmlNode node)
		{
			id = Int32.Parse(node.Attributes["id"].Value);
			type = (TemplateType)Enum.Parse(typeof(TemplateType), node.Attributes["type"].Value);
			title = node.SelectSingleNode("title").InnerText;
			body = node.SelectSingleNode("body").InnerText;

			if (node.Attributes["sortOrder"] != null)
				sortOrder = Int32.Parse(node.Attributes["sortOrder"].Value);
		}

		public TemplateType Type
		{
			get { return type; }
		}

		public int TemplateID
		{
			get { return id; }
		}

		public string Title
		{
			get { return title; }
		}

		public string Body
		{
			get { return body; }
		}

		public int SortOrder
		{
			get { return sortOrder; }
		}

		#region IComparable Members
		public int CompareTo(object obj)
		{
			if(obj is Template)
			{
				Template temp = (Template)obj;

				// if sortOrders are equal, sort by id
				if (SortOrder.CompareTo(temp.SortOrder) == 0)
					return TemplateID.CompareTo(temp.TemplateID);
				else
					return SortOrder.CompareTo(temp.SortOrder);
			}

			throw new ArgumentException("object is not a Template");
		}
		#endregion
	}
}
