using System;
using System.Data;
using System.Data.OleDb;

namespace VoServices.Footprint.Web
{
	/// <summary>
	/// Summary description for PageList.
	/// </summary>
	public class PageList : System.Web.UI.WebControls.WebControl
	{

		public int Count;
		private int _currentPage;
		private int _pageSize;
		private string _path;
		private string _parameters;

		public PageList()
		{
			_currentPage = 0;
			_pageSize = 10;
		}

		private void PageList_Load(object sender, System.EventArgs e)
		{
			Initialize();
		}

		public void Initialize()
		{
			if (Page.Request["page"] != null)
				_currentPage = Int32.Parse(Page.Request["page"]);

			//			if (Page.Request["pagesize"] != null)
			//				_pageSize = Int32.Parse(Page.Request["pagesize"]);

			_path = Page.Request.Path;

			_parameters = "";
			foreach (string key in Page.Request.QueryString)
			{
				if (key != null)
					if (key.ToLower() != "page")
						_parameters += "&" + key + "=" + Page.Server.UrlEncode(Page.Request.QueryString[key]);
			}
			if (_parameters != "")
				_parameters = _parameters.Remove(0, 1);
		}

		public int CurrentPage
		{
			get
			{
				return _currentPage;
			}
		}

		public int PageSize
		{
			get
			{
				return _pageSize;
			}
			set
			{
				_pageSize = value;
			}
		}

		public int Pages
		{
			get
			{
				return (int) System.Math.Ceiling(Count / (double) _pageSize);
			}
		}

		public int FirstRecord
		{
			get
			{
				return _currentPage * _pageSize;
			}
		}

		public int LastRecord
		{
			get
			{
				int lastrecord = (_currentPage + 1) * _pageSize;
				if (lastrecord < Count)
					return lastrecord;
				else
					return Count;
			}
		}

		public int GetPageWhenSize(int pageSize)
		{
			return FirstRecord / pageSize;
		}

		override protected void Render(System.Web.UI.HtmlTextWriter writer)
		{
			writer.Write("<p class=\"PageList\">");

			if (_currentPage > 0)
			{
				writer.Write(String.Format("&nbsp;[&nbsp;<a class=\"ArticleBoxTitle\" href=\"{0}?{1}&page=0\">First</a>&nbsp;]", _path, _parameters));
				writer.Write(String.Format("&nbsp;[&nbsp;<a class=\"ArticleBoxTitle\" href=\"{0}?{1}&page={2}\">Prev</a>&nbsp;]\r\n", _path, _parameters, _currentPage - 1));
			}

			if (Pages > 1 )
			{
				writer.Write(String.Format("<select onChange=\"document.location='{0}?{1}&page='+this.selectedIndex\">", _path, _parameters));

				for (int i = 0; i < Pages; i ++)
				{
					if (i == _currentPage)
					{
						writer.Write(String.Format("<option value=\"{0}\" selected>{0}</option>\r\n", i + 1));
					}
					else
					{
						writer.Write(String.Format("<option value=\"{0}\">{0}</option>", i + 1));
						//writer.Write(String.Format(" <a class=\"ArticleBoxTitle\" href=\"{0}?{1}&page={2}\">{3}</a>", _path, _parameters, i, i + 1));
					}
				}

				writer.Write("</select>\r\n");
			}

			if (_currentPage < Pages - 1)
			{
				writer.Write(String.Format("&nbsp;[&nbsp;<a class=\"ArticleBoxTitle\" href=\"{0}?{1}&page={2}\">Next</a>&nbsp;]", _path, _parameters, _currentPage + 1));
				writer.Write(String.Format("&nbsp;[&nbsp;<a class=\"ArticleBoxTitle\" href=\"{0}?{1}&page={2}\">Last</a>&nbsp;]", _path, _parameters, Pages - 1));
			}

			writer.Write("</p>");
		}

		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}
		
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.PageList_Load);
		}

	}
}
