using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using Medivators.BusinessObjects;
using Medivators.Common;
using Medivators.Data;
using Medivators.Data.Queries;


namespace TestApp
{
	public partial class DBTest : Form
	{

		private delegate void UpdateDisplay(string Text);

		public DBTest()
		{
			InitializeComponent();
		}

		private void cmdOpen_Click(object sender, EventArgs e)
		{
			Connection.GetInstance().Open();
			DisplayState();
		}

		private void DisplayState()
		{
			this.txtDisplay.AppendText("Connection state: " + Connection.GetInstance().State.ToString() + Constants.NewLine);
		}

		private void cmdClose_Click(object sender, EventArgs e)
		{
			Connection.GetInstance().Close();
			DisplayState();
		}

		private void query_SelectComplete(object sender, SelectCompleteEventArgs e)
		{
			UpdateDisplay update = this.Display;
			object[] parms = new object[1] { e.ToString() }; 
			txtDisplay.Invoke(update, parms);			
		}
		private void query_DataException(object sender, DataExceptionEventArgs e)
		{
			UpdateDisplay update = this.Display;
			object[] parms = new object[1] { e.ToString() };
			txtDisplay.Invoke(update, parms);
		}
		private void query_InsertComplete(object sender, InsertCompleteEventArgs e)
		{
			UpdateDisplay update = this.Display;
			object[] parms = new object[1] { e.ToString() };
			txtDisplay.Invoke(update, parms);
		}
		private void Display(string NewText)
		{
			this.txtDisplay.AppendText(NewText);
			if (!NewText.EndsWith(Constants.NewLine))
				this.txtDisplay.AppendText(Constants.NewLine);
		}
		private void cmdUpdateLog_Click(object sender, EventArgs e)
		{
			DSD201EventRecord rec = new DSD201EventRecord();
			rec.IsAlarm = false;
			rec.LogNumber = 007;
			rec.MachineID = 8008;
			rec.Program = 3;
			rec.RecordType = DeviceRecordTypes.LogRecord;
			rec.Station = "Z";
			rec.Text = "<Updated!>";
			rec.Timestamp = Convert.ToDateTime("4/20/2006 14:20");
			using (UpdateLogRecord query = new UpdateLogRecord(rec))
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.Update();
			}
		}
		private void cmdInsertLog_Click(object sender, EventArgs e)
		{
			DSD201EventRecord rec = new DSD201EventRecord();
			rec.IsAlarm = false;
			rec.LogNumber = 007;
			rec.MachineID = 8008;
			rec.Program = 3;
			rec.RecordType = DeviceRecordTypes.LogRecord;
			rec.Station = "Z";
			rec.Text = "Test Record!";
			rec.Timestamp = Convert.ToDateTime("4/20/2006 14:20");
			using (InsertLogRecord query = new InsertLogRecord(rec))
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.Insert();
			}
			// insert a second record
			rec.IsAlarm = true;
			rec.LogNumber = 008;
			rec.MachineID = 8008;
			rec.Program = 3;
			rec.RecordType = DeviceRecordTypes.LogRecord;
			rec.Station = "Z";
			rec.Text = "Related";
			rec.Timestamp = Convert.ToDateTime("4/20/2006 16:20");
			using (InsertLogRecord query = new InsertLogRecord(rec))
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.Insert();
			}			
		}
		private void cmdInsertReport_Click(object sender, EventArgs e)
		{
			CycleReport rpt = new CycleReport(8008, "Z");
			rpt.CycleCount = 76;
			rpt.DisinfectantCycleCount = 42;
			rpt.EndTimestamp = Convert.ToDateTime("4/20/2006 16:20");
			rpt.OperatorID = 10101010;
			rpt.PatientID = 123456;
			rpt.PhysicianID = 65;
			rpt.ScopeID = 987;
			rpt.SSGRatio = "010:101:01.0";
			using (InsertCycleReport query = new InsertCycleReport(rpt))
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.Insert();
			}
			// insert another
			rpt.CycleCount = 1000;
			rpt.DisinfectantCycleCount = 900;
			rpt.EndTimestamp = Convert.ToDateTime("4/20/2006 16:20");
			rpt.StartTimestamp = Convert.ToDateTime("4/20/2006 14:20");
			rpt.OperatorID = 800;
			rpt.PatientID = 700;
			rpt.PhysicianID = 600;
			rpt.ScopeID = 500;
			rpt.SSGRatio = "400:300:20.1";
			using (InsertCycleReport query = new InsertCycleReport(rpt))
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.Insert();
			}
		}
		private void cmdUpdateReport_Click(object sender, EventArgs e)
		{
			CycleReport rpt = new CycleReport(8008, "Z");
			rpt.CycleCount = 100;
			rpt.DisinfectantCycleCount = 100;
			rpt.EndTimestamp = Convert.ToDateTime("4/20/2006 16:20");
			rpt.OperatorID = 9;
			rpt.PatientID = 87;
			rpt.PhysicianID = 1;
			rpt.ScopeID = 111111;
			rpt.SSGRatio = "987:654:32.1";
			using (UpdateCycleReport query = new UpdateCycleReport(rpt))
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.Update();
			}
		}

		private void cmdInsertJoin_Click(object sender, EventArgs e)
		{
			InsertCycleReportLogRecord query = new InsertCycleReportLogRecord(
				"8008Z000101011200200604200420",
				"2006042004200008LogRecord8008Z"
				);
			query.Insert();
			//	there are two test records to join
			query = new InsertCycleReportLogRecord(
				"8008Z000101011200200604200420",
				"2006042002200007LogRecord8008Z"
				);
			query.Insert();
		}

		private void cmdDeleteJoins_Click(object sender, EventArgs e)
		{
			DeleteCycleReportLogRecords query = new DeleteCycleReportLogRecords();				
			{
				int rows = query.Delete("8008Z000101011200200604200420");
				Display("Delete Joins reports rows affected: " + rows.ToString());
			}
		}

		private void cmdLogByDates_Click(object sender, EventArgs e)
		{
			using (SelectLogRecords query = new SelectLogRecords())
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.SelectByDates(Convert.ToDateTime("4/19/2006 12:00"), Convert.ToDateTime("4/21/2006 12:00"));
			}
		}

		private void cmdLogByKey_Click(object sender, EventArgs e)
		{
			using (SelectLogRecords query = new SelectLogRecords())
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.SelectByKey("2006042004200008LogRecord8008Z");
			}
		}

		private void cmdLogByReport_Click(object sender, EventArgs e)
		{
			using (SelectLogRecords query = new SelectLogRecords())
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.SelectByCycleReport("8008Z000101011200200604200420");
			}
		}

		private void cmdLogByKeys_Click(object sender, EventArgs e)
		{
			string[] keys = {"2006042004200008LogRecord8008Z", 
							 "2006042002200007LogRecord8008Z", 
							 "2006062708080007LogRecord8008Z"};
			using (SelectLogRecords query = new SelectLogRecords())
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.SelectByKeys(keys);
			}
		}

		private void cmdLogByDatesNumbers_Click(object sender, EventArgs e)
		{
			using (SelectLogRecords query = new SelectLogRecords())
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.SelectByDateAndLogNumber(Convert.ToDateTime("4/19/2006 12:00"),
											   Convert.ToDateTime("4/21/2006 12:00"),
											   1, 7);
			}
		}

		private void cmdReportByDates_Click(object sender, EventArgs e)
		{
			using (SelectCycleReports query = new SelectCycleReports())
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.SelectByDates(Convert.ToDateTime("4/19/1999 12:00"), Convert.ToDateTime("4/21/2006 12:00"));
			}
		}

		private void cmdCycleByKey_Click(object sender, EventArgs e)
		{
			using (SelectCycleReports query = new SelectCycleReports())
			{
				query.DataException += new DataExceptionEventHandler(query_DataException);
				query.InsertComplete += new InsertCompleteEventHandler(query_InsertComplete);
				query.SelectComplete += new SelectCompleteEventHandler(query_SelectComplete);
				query.SelectByKey("8008Z000101011200200604200420");
			}
		}
	}
}