using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.RFIDLab.EPCISDemo.Common;

namespace Microsoft.RFIDLab.EPCISDemo
{
	/// <summary>
	/// Displays EPC events.
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.Label statusLbl;
		protected System.Web.UI.WebControls.DropDownList databaseSelection;
		protected System.Web.UI.WebControls.Button btnQuery;
		protected System.Web.UI.WebControls.DataGrid dgEventDetails;
	
		/// <summary>
		/// Invoked when the page loads.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		private void Page_Load(object sender, System.EventArgs e)
		{
			statusLbl.Visible = false;
			if(!Page.IsPostBack)
			{
				BindGrid(); //display Manufacturers Database view by default.
			}
		}

		/// <summary>
		/// Invoked when the page-number at the data-grid changes.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		void dgEventDetails_Page(object sender, DataGridPageChangedEventArgs e) 
		{
    		dgEventDetails.CurrentPageIndex = e.NewPageIndex;
			BindGrid();
		}

		/// <summary>
		/// Binds the data-grid with the data.
		/// </summary>
		void BindGrid()
		{
			TrackTraceDBManager dbManager;
			if(databaseSelection.SelectedValue.Equals("MANUFACTURER"))
				dbManager = new TrackTraceDBManager(TrackTraceDBManager.Constants.Manufacturer);
			else if(databaseSelection.SelectedValue.Equals("SHIPPING AGENT"))
				dbManager = new TrackTraceDBManager(TrackTraceDBManager.Constants.ShippingAgent);
			else if(databaseSelection.SelectedValue.Equals("RECEIVING AGENT"))
				dbManager = new TrackTraceDBManager(TrackTraceDBManager.Constants.ReceivingAgent);
			else if(databaseSelection.SelectedValue.Equals("RECEIVER"))
				dbManager = new TrackTraceDBManager(TrackTraceDBManager.Constants.Receiver);
			else
				return;

			DataSet dsOrderDetails;

			if (SortField == String.Empty)
			{
				// MS - commented
				// dsOrderDetails = dbManager.GetAllEPCEvents(null);
				dsOrderDetails = dbManager.GetFullEPCEventsOrdered ( null ) ;

			} // end if

			else
			{
				// MS - commented
				// dsOrderDetails = dbManager.GetAllEPCEvents(SortField);
				dsOrderDetails = dbManager.GetFullEPCEventsOrdered ( SortField ) ;

			} // end else
				
			if (dsOrderDetails.Tables[0].Rows.Count > 0) 
			{
				dgEventDetails.Visible=true;
				dgEventDetails.DataSource = dsOrderDetails;
				dgEventDetails.DataBind();
			}
			else 
			{
				dgEventDetails.Visible = false;
				statusLbl.Text = "Database Empty. No EPC Events available for display.";
				statusLbl.Visible = true;
			}
		}
	

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
			this.dgEventDetails.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgEventDetails_Page);
			this.dgEventDetails.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dgEventDetails_Sort);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		/// <summary>
		/// Invoked when the 'Query database' button is clicked.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		private void btnQuery_Click(object sender, System.EventArgs e)
		{
			dgEventDetails.CurrentPageIndex = 0;
			BindGrid();
		}

		/// <summary>
		/// Invoked when the data-grid is sorted.
		/// </summary>
		/// <param name="sender">The source of the event. </param>
		/// <param name="e">The event-arguments. </param>
		private void dgEventDetails_Sort(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			dgEventDetails.CurrentPageIndex = 0;
			SortField = e.SortExpression;
			BindGrid();
		}

		/// <summary>
		/// Gets or sets the sort-field.
		/// </summary>
		protected String SortField 
		{
			get 
			{
				object o = ViewState["SortField"];
				return (o == null) ? String.Empty : (String)o;
			}
			set 
			{
				ViewState["SortField"] = value;
			}
		}
	}
}
