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.VerisignEPCIS;

namespace Microsoft.RFIDLab.EPCISDemo
{
	/// <summary>
	/// Summary description for LogCustomEPCISEventInformation.
	/// </summary>
	public class LogCustomEPCISEventInformation : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.DropDownList LocationTypeDDL;
		protected System.Web.UI.WebControls.Label Label3;
		protected System.Web.UI.WebControls.TextBox txtLocation;
		protected System.Web.UI.WebControls.Label Label4;
		protected System.Web.UI.WebControls.TextBox txtOperation;
		protected System.Web.UI.WebControls.Label Label5;
		protected System.Web.UI.WebControls.TextBox txtDateTime;
		protected System.Web.UI.WebControls.Label Label6;
		protected System.Web.UI.WebControls.TextBox txtTagEPC;
		protected System.Web.UI.WebControls.Button btnLogEvent;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
		protected System.Web.UI.WebControls.Label lblStatus;
		protected System.Web.UI.WebControls.Label Label1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			txtDateTime.Text=System.DateTime.Now.ToString();

			if (Request.QueryString["uri"] != null)
			{
				string caseEPCURI = Request.QueryString["uri"];
				Label1.Text = "Log event for EPC " + caseEPCURI + @" to <a href=""http://microsoft.verisignepctest.com/"">Microsoft EPC IS</a>";

				txtTagEPC.Text = caseEPCURI;				
			}
		}

		#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.btnLogEvent.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Button1_Click(object sender, System.EventArgs e)
		{
			ObservationDataProfile wsObservationDataProfile = new ObservationDataProfile();
			UserTokenType objUserTokenType = new UserTokenType();
			objUserTokenType.UserName = System.Configuration.ConfigurationSettings.AppSettings["VerisignEPCISUserName"];
			objUserTokenType.Password = System.Configuration.ConfigurationSettings.AppSettings["VerisignEPCISPassword"];

			AuthInfoType objAuthInfoType = new AuthInfoType();
			objAuthInfoType.UserToken = objUserTokenType;				
			wsObservationDataProfile.AuthInfo = objAuthInfoType;
				
			logEvent[] epcEvents = new logEvent[1];

			epcEvents[0] = new logEvent();
			epcEvents[0].location = new IdentifierType();

			if (LocationTypeDDL.SelectedValue.ToString() == "GLN")
			{
				epcEvents[0].location.schemeAgencyID = "EAN.UCC";
				epcEvents[0].location.schemeID = "GLN";
				epcEvents[0].location.schemeURI = "urn:epc:id:gln";
				epcEvents[0].location.schemeVersionID = "1.0";
				epcEvents[0].location.Value = txtLocation.Text;
			}
			else //Custom
			{
				epcEvents[0].location.schemeAgencyID = "Custom";
				epcEvents[0].location.schemeID = "Custom";
				epcEvents[0].location.schemeURI = "Custom";
				epcEvents[0].location.schemeVersionID = "1.0";
				epcEvents[0].location.Value = txtLocation.Text;
			}
			
			epcEvents[0].observation = new ObservationType();
			epcEvents[0].observation.Command = txtOperation.Text;
			epcEvents[0].observation.DateTime = System.DateTime.Now;
			
			TagType[] objTags = new TagType[1];
			objTags[0] = new TagType();
			
			//Tags in the EPC Network are identified by an identifier code. 
			//The universal structure ID element is reused to capture tag identification information.
			objTags[0].ID = new IdentifierType();
			objTags[0].ID.schemeID = "EPC";
			objTags[0].ID.schemeAgencyID = "EAN";			
			objTags[0].ID.schemeVersionID = "1.0";
            objTags[0].ID.schemeURI = txtTagEPC.Text.Substring(1,txtTagEPC.Text.LastIndexOf(":"));
			objTags[0].ID.Value = txtTagEPC.Text;					
			epcEvents[0].observation.Tag = objTags;

			bool eventsLogged = wsObservationDataProfile.logEvents(epcEvents);
			if (eventsLogged)
			{
				lblStatus.Text = "Event logged successfully" + @" to <a href=""http://microsoft.verisignepctest.com/"">Microsoft EPC IS</a>";
				lblStatus.ForeColor= System.Drawing.Color.Green;
			}
			else
			{
				lblStatus.Text = "Failed to log event" + @" to <a href=""http://microsoft.verisignepctest.com/"">Microsoft EPC IS</a>";
				lblStatus.ForeColor=System.Drawing.Color.Red;
			}
		}
	}
}
