﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace OKHOSTING.ORM.Operations
{
	/// <summary>
	/// Use to define paging in select operations, for example, if you only want to retrieve Objects from index 0 to 100, or 101 to 200
	/// <para xml:lang="es">
	/// Se utiliza para definir paginacion en operaciones de seleccion, por ejemplo si solo desea recuperar objetos de indice de 0 a 100 ó de 101 a 200.
	/// </para>
	/// </summary>
	public class SelectLimit: IXmlSerializable
	{
		/// <summary>
		/// Starting 0-based index for the list you want to select
		/// <para xml:lang="es">
		/// Indice basado en 0 para iniciar la lista que desea seleccionar.
		/// </para>
		/// </summary>
		public int From { get; set; }

		/// <summary>
		/// Finishing 0-based index for the list you want to select
		/// <para xml:lang="es">
		/// Indice basado en 0 para terminar la lista que desea seleccionar.
		/// </para>
		/// </summary>
		public int To
		{
			get
			{
				return From + Count;
			}
		}

		/// <summary>
		/// Returns the count
		/// <para xml:lang="es">
		/// Devuelve el conteo
		/// </para>
		/// </summary>
		public int Count { get; set; }
		

		/// <summary>
		/// Creates a new instance
		/// <para xml:lang="es">
		/// Crea una nueva instancia
		/// </para>
		/// </summary>
		public SelectLimit()
		{
			From = Count = 0;
		}

		/// <summary>
		/// Create a new instance with the specified parameters, which starts and where ends
		/// <para xml:lang="es">
		/// Crea una nueva instancia especificandole con parametros, donde inicie y donde termine
		/// </para>
		/// </summary>
		/// <param name="from">
		/// </param>
		/// <param name="to">
		/// </param>
		public SelectLimit(int from, int count)
		{
			From = from;
			Count = count;
		}

		#region Xml Serialization

		/// <summary>
		/// Deserialize the filter
		/// </summary>
		public void ReadXml(XmlReader reader)
		{
			reader.MoveToAttribute("From");
			From = Convert.ToInt32(reader.Value);

			reader.MoveToAttribute("Count");
			Count = Convert.ToInt32(reader.Value);
		}

		/// <summary>
		/// Serialize the filter
		/// </summary>
		public void WriteXml(XmlWriter writer)
		{
			writer.WriteAttributeString("From", Serializer.ToString(From));
			writer.WriteAttributeString("Count", Serializer.ToString(Count));
		}

		XmlSchema IXmlSerializable.GetSchema()
		{
			return null;
		}

		#endregion
	}
}
