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 SelectLogRecordsByFilterList : AsynchronousExecute
	{

		private string baseSQL = " select LR.* " +
								 " from   LogRecords LR";
		private string joinSQL = " order by LR.LogNumber";

		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, "TimeStamp >= " + Constants.DateDelimiter + criterion.Value + Constants.DateDelimiter);
							break;
						case "ENDTIMESTAMP":
							sql = AndAdder(sql, "TimeStamp <= " + Constants.DateDelimiter + criterion.Value + Constants.DateDelimiter);
							break;
						case "DATE":
							DateTime wrkDate = Convert.ToDateTime(criterion.Value).Date;							
							sql = AndAdder(sql, "TimeStamp >= " + Constants.DateDelimiter + wrkDate.ToString(Constants.DateTimeFormat) + Constants.DateDelimiter);
							sql = AndAdder(sql, "TimeStamp <= " + 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, "MachineID=" + machID);
							sql = AndAdder(sql, "Station=" + Constants.DoubleQuote + station + Constants.DoubleQuote);
							break;
						case "OPERATOR":
							sql = AndAdder(sql, "recordType='OperatorID' and Data=" + Constants.DoubleQuote + criterion.Value.ToString() + Constants.DoubleQuote);
							break;
						case "PATIENT":
							sql = AndAdder(sql, "recordType='PatientID' and Data=" + Constants.DoubleQuote + criterion.Value.ToString() + Constants.DoubleQuote);
							break;
						case "PHYSICIAN":
							sql = AndAdder(sql, "recordType='PhysicianID' and Data=" + Constants.DoubleQuote + criterion.Value.ToString() + Constants.DoubleQuote);
							break;
						case "SCOPE":
							sql = AndAdder(sql, "recordType='ScopeID' and Data=" + Constants.DoubleQuote + criterion.Value.ToString() + Constants.DoubleQuote);
							break;
						case "EVENTTEXT":
							sql = AndAdder(sql, "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);
			}
			 //" where  Timestamp >= " + Constants.DateDelimiter + StartDate + Constants.DateDelimiter +
			 //" and    Timestamp <= " + Constants.DateDelimiter + EndDate + Constants.DateDelimiter;
			sql = baseSQL + " where " + sql + joinSQL;
			base.Select(sql, new LogRecordDataMapper());
		}

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