using System;
using System.Collections.Generic;
using System.Text;
using Medivators.BusinessObjects;
using Medivators.Common;
using Medivators.Common.Logging;


namespace Medivators.Data.Queries
{
	public class SelectCycleReportsByFilterList : AsynchronousExecute
	{
		private static string selectList = " CR.*, LR.* "; 
		private static string baseSQL =		" select   " + selectList +
											" from     CycleReports CR, CycleReportLogRecords CRLR, LogRecords LR ";
		private static string joinSQL = " and      CR.[Key] = CRLR.CycleReportKey " +
											" and      CRLR.LogRecordKey = LR.[Key] " +
											" order by LR.Key ";
								 
		
		public void Select(Dictionary<string, string> FilterCriteria)
		{
			string sql = string.Empty;
			try
			{
				foreach (KeyValuePair<string, string> criterion in FilterCriteria)
				{
					switch (criterion.Key.ToUpper().Trim())
					{
						case "STARTTIMESTAMP":
							sql = AndAdder(sql, "CR.StartTimeStamp >= " + Constants.DateDelimiter + criterion.Value + Constants.DateDelimiter);
							break;
						case "ENDTIMESTAMP":
							sql = AndAdder(sql, "CR.StartTimeStamp <= " + Constants.DateDelimiter + criterion.Value + Constants.DateDelimiter);
							break;
						case "DATE":
							DateTime wrkDate = Convert.ToDateTime(criterion.Value).Date;
							sql = AndAdder(sql, "CR.StartTimeStamp >= " + Constants.DateDelimiter + wrkDate.ToString(Constants.DateTimeFormat) + Constants.DateDelimiter);
							sql = AndAdder(sql, "CR.StartTimeStamp <= " + Constants.DateDelimiter + wrkDate.AddDays(1.0).ToString(Constants.DateTimeFormat) + Constants.DateDelimiter);
							break;
						case "MACHINESTATION":
							string machID = criterion.Value.ToString();
							string station = machID.Substring(machID.Length - 1);
							machID = machID.Remove(machID.Length - 1);
							sql = AndAdder( sql, "CR.MachineID=" + machID);
							sql = AndAdder(sql, "CR.Station=" + Constants.DoubleQuote + station + Constants.DoubleQuote);
							break;
						case "OPERATOR":
							sql = AndAdder(sql, "CR.OperatorID=" + criterion.Value.ToString());
							break;
						case "PATIENT":
							sql = AndAdder(sql, "CR.PatientID=" + criterion.Value.ToString());
							break;
						case "PHYSICIAN":
							sql = AndAdder(sql, "CR.PhysicianID=" + criterion.Value.ToString());
							break;
						case "SCOPE":
							sql = AndAdder(sql, "CR.ScopeID=" + criterion.Value.ToString());
							break;
						case "EVENTTEXT":
							sql = AndAdder(sql, "exists ( select CRLR2.CycleReportKey from CycleReportLogRecords CRLR2, LogRecords LR2 where CRLR2.CycleReportKey = CR.[Key] and LR2.[Key]=CRLR2.[LogRecordKey] and LR2.Text=" + Constants.DoubleQuote + criterion.Value.ToString() + Constants.DoubleQuote + ")");
							break;
						default:
							ErrorHandler.GetInstance().LogException(String.Format("Unsupported criterion type {0} value {1}",criterion.Key, criterion.Value));
							break;
					}
				}
			}
			catch ( Exception ex)
			{
				ErrorHandler.GetInstance().LogException("SelectLogRecordsByFilterList", ex);
			}

			sql = baseSQL + " where " + sql + joinSQL;
			base.Select(sql, new CycleReportDataMapper());
		}

		private string AndAdder(string OriginalSql, string NewSql)
		{
			if (OriginalSql == null || OriginalSql.Equals(string.Empty) || OriginalSql.Length == 0)
				return " " + NewSql + " ";
			else
				return " " + OriginalSql + " and " + NewSql + " ";
		}
	}
}
