using System;
using System.Data;
using Ease.Core.Model;
using Ease.Core.DataAccess;
using Ease.Core;
using System.Collections.Generic;
using Ease.Core.Utility;
using HRM.BO;

namespace HRM.DA
{
    //public class NotificationService : ServiceTemplate
    //{

    //    #region Map Objects

    //    #region Map Parent
    //    private void MapObject(Notification oNotification, DataReader oReader)
    //    {
    //        base.SetObjectID(oNotification, (oReader.GetInt32("NotificationID").Value));
    //        oNotification.Subject = oReader.GetString("Subject");
    //        oNotification.Body = oReader.GetString("Body");
    //        oNotification.TagOutputID = oReader.GetInt32("TagOutputID",0);
    //        oNotification.ObjectID = oReader.GetInt32("ObjectID",0);
    //        oNotification.OptionalObjectID = oReader.GetInt32("OptionalObjectID",0);
    //        oNotification.Attachment = oReader.GetString("Attachment");
    //        oNotification.Footer = oReader.GetString("Footer");
    //        oNotification.Type = (EnumNotificationType)oReader.GetInt32("Type");
    //        //oNotification.Medium = (EnumNotificationMedium) oReader.GetInt32("Medium");
    //        oNotification.IsMailNf = oReader.GetBoolean("IsMailNf").Value;
    //        oNotification.IsSMSNf = oReader.GetBoolean("IsSMSNf").Value;
    //        oNotification.IsWebNf = oReader.GetBoolean("IsWebNf").Value;
    //        oNotification.Status = (EnumNotificationStatus) oReader.GetInt32("Status");

    //        this.SetObjectState(oNotification, Ease.Core.ObjectState.Saved);
    //    }
    //    protected override T CreateObject<T>(DataReader oReader)
    //    {
    //        Notification oNotification = new Notification();
    //        MapObject(oNotification, oReader);
    //        return oNotification as T;
    //    }

    //    #endregion

    //    #region Map NotificationParticipants
    //    private void MapParticipantObject(NotificationParticipant oNotificationParticipant, DataReader oReader)
    //    {
    //        base.SetObjectID(oNotificationParticipant, (oReader.GetInt32("NotificationParticipantID").Value));
    //        oNotificationParticipant.NotificationID = oReader.GetInt32("NotificationID",0);
    //        oNotificationParticipant.EmployeeID = oReader.GetInt32("EmployeeID",0);
    //        oNotificationParticipant.Comments = oReader.GetString("Comments");
    //        oNotificationParticipant.SentTime = (DateTime) oReader.GetDateTime("SentTime");
    //        this.SetObjectState(oNotificationParticipant, Ease.Core.ObjectState.Saved);

    //    }

    //    private  NotificationParticipant CreateParticipant(DataReader oReader)
    //    {
    //        NotificationParticipant oNotificationParticipant = new NotificationParticipant();
    //        MapParticipantObject(oNotificationParticipant, oReader);
    //        return oNotificationParticipant;
    //    }

    //    private List<NotificationParticipant> CreateParticipants(DataReader oReader)
    //    {
    //        List<NotificationParticipant> oNotificationParticipants = new List<NotificationParticipant>();
    //        NotificationParticipant oNotificationParticipant;

    //        while (oReader.Read())
    //        {
    //            oNotificationParticipant = new NotificationParticipant();
    //            oNotificationParticipant = CreateParticipant(oReader);
    //            oNotificationParticipants.Add(oNotificationParticipant);
    //        }
    //        return oNotificationParticipants;
    //    }

    //    #endregion

    //    #region Map NotificationRule
    //    private void MapNotificationRuleObject(NotificationRule oNotificationRule, DataReader oReader)
    //    {
    //        base.SetObjectID(oNotificationRule, (oReader.GetInt32("NotificationRuleID").Value));
    //        oNotificationRule.BatchNo = oReader.GetInt32("BatchNo").Value;
    //        oNotificationRule.NotificationID = oReader.GetInt32("NotificationID",0);
    //        oNotificationRule.PerticipantID = oReader.GetInt32("PerticipantID",0);
    //        oNotificationRule.PerticipantType = (EnumPerticipantType) oReader.GetInt32("PerticipantType");
    //        oNotificationRule.SendDate = oReader.GetDateTime("SendDate").Value;
    //        oNotificationRule.ExtendedBody = oReader.GetString("ExtendedBody");
    //        oNotificationRule.Remarks = oReader.GetString("Remarks");
    //        oNotificationRule.EmailSendStatus = (EnumSendStatus)oReader.GetInt32("EmailSendStatus");
    //        oNotificationRule.WebSendStatus = (EnumSendStatus)oReader.GetInt32("WebSendStatus");
    //        oNotificationRule.SMSSendStatus = (EnumSendStatus)oReader.GetInt32("SMSSendStatus");
    //        oNotificationRule.AllowWrite = (bool)oReader.GetBoolean("AllowWrite");

    //        this.SetObjectState(oNotificationRule, Ease.Core.ObjectState.Saved);

    //    }

    //    private NotificationRule CreateNotificationRule(DataReader oReader)
    //    {
    //        NotificationRule oNotificationRule = new NotificationRule();
    //        MapNotificationRuleObject(oNotificationRule, oReader);
    //        return oNotificationRule;
    //    }

    //    private List<NotificationRule> CreateNotificationRules(DataReader oReader)
    //    {
    //        List<NotificationRule> oCreateNotificationRules = new List<NotificationRule>();
    //        NotificationRule oCreateNotificationRule;

    //        while (oReader.Read())
    //        {
    //            oCreateNotificationRule = CreateNotificationRule(oReader);
    //            oCreateNotificationRules.Add(oCreateNotificationRule);
    //        }
    //        return oCreateNotificationRules;
    //    }

    //    #endregion

    //    #endregion

    //    #region Implementation of INotificationService

    //    #region Get Functions

    //    public int GetNewID()
    //    {
    //        TransactionContext tc = null;
    //        tc = TransactionContext.Begin(true);
    //        try
    //        {
    //            return tc.GenerateID("Notification", "NotificationID");
    //        }
    //        catch (Exception e)
    //        {

    //            throw e;
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }
    //    }

    //    public Notification Get(int id)
    //    {
    //        Notification oNotification = null;


    //        TransactionContext tc = null;
    //        try
    //        {
    //            tc = TransactionContext.Begin();
    //            DataReader oreader = new DataReader(NotificationDA.Get(tc,id));
    //            if(oreader.Read())
    //            {
    //                oNotification = CreateObject<Notification>(oreader);
    //            }
    //            oreader.Close();

    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }


    //        return oNotification;
    //    }

    //    public List<Notification> Get()
    //    {
    //        List<Notification> oNotifications = new List<Notification>();


    //        TransactionContext tc = null;
    //        try
    //        {
    //            tc = TransactionContext.Begin();
    //            DataReader oreader = new DataReader(NotificationDA.Get(tc));
    //            oNotifications = CreateObjects<Notification>(oreader);

    //            oreader.Close();

    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }


    //        return oNotifications;
    //    }

    //    public List<Notification> Get(EnumNotificationStatus status)
    //    {

    //        List<Notification> oNotifications = new List<Notification>();


    //        TransactionContext tc = null;
    //        try
    //        {
    //            tc = TransactionContext.Begin();
    //            DataReader oreader = new DataReader(NotificationDA.Get(tc,status));
    //            oNotifications = CreateObjects<Notification>(oreader);

    //            oreader.Close();

    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }


    //        return oNotifications;
    //    }

    //    public List<Notification> GetByDateRange(DateTime startDate, DateTime endDate)
    //    {
    //        List<Notification> oNotifications = new List<Notification>();


    //        TransactionContext tc = null;
    //        try
    //        {
    //            tc = TransactionContext.Begin();
    //            DataReader oreader = new DataReader(NotificationDA.GetByDateRange(tc,startDate,endDate));
    //            oNotifications = CreateObjects<Notification>(oreader);

    //            oreader.Close();

    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }


    //        return oNotifications;
    //    }

    //    public List<NotificationParticipant> GetNotificationParticipants(int id)
    //    {
    //        List<NotificationParticipant> oNotificationParticipants = new List<NotificationParticipant>();


    //        TransactionContext tc = null;
    //        try
    //        {
    //            tc = TransactionContext.Begin();
    //            DataReader oreader = new DataReader(NotificationDA.GetParticipants(tc, id));

    //            oNotificationParticipants = CreateParticipants(oreader);

    //            oreader.Close();

    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }


    //        return oNotificationParticipants;
    //    }

    //    public List<NotificationRule> GetNotificationRules(int id)
    //    {
    //        List<NotificationRule> oCreateNotificationRules = new List<NotificationRule>();


    //        TransactionContext tc = null;
    //        try
    //        {
    //            tc = TransactionContext.Begin();
    //            DataReader oreader = new DataReader(NotificationDA.GetNotificationRules(tc, id));

    //            oCreateNotificationRules = CreateNotificationRules(oreader);

    //            oreader.Close();

    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }


    //        return oCreateNotificationRules;
    //    }

    //    #endregion

    //    #region Get Scalars

    //    public int GetNewBatchNumber()
    //    {
    //        TransactionContext tc = null;
    //        try
    //        {
    //            tc = TransactionContext.Begin(true);
    //            int id = tc.GenerateID("NotificationRule", "BatchNo");
    //            return id;

    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }
    //    }

    //    private int GetNewBatchNumber(TransactionContext tc)
    //    {

    //        try
    //        {
    //            int id = tc.GenerateID("NotificationRule", "BatchNo");
    //            return id;

    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }

    //    }

    //    public DateTime GetMinSendDateByID(int iD)
    //    {
    //        TransactionContext tc = null;
    //        try
    //        {
    //            tc = TransactionContext.Begin(true);
    //            DateTime minDate = NotificationDA.GetMinDateByID(tc, iD);
    //            return minDate;

    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }
    //    }

    //    #endregion

    //    public int Insert(Notification oNotification)
    //    {
    //        TransactionContext tc = null;
    //        try
    //        {
    //            int startingBatchNo;
    //            int previousBatchNo = 0;
    //            tc = TransactionContext.Begin(true);
    //            if (oNotification.IsNew)
    //            {

    //                int id = tc.GenerateID("Notification", "NotificationID");
    //                base.SetObjectID(oNotification, (id));

    //                NotificationDA.Insert(tc, oNotification);
    //                startingBatchNo = GetNewBatchNumber(tc);
    //                foreach (NotificationRule notificationRule in oNotification.NotificationRules)
    //                {
    //                    if(previousBatchNo!=notificationRule.BatchNo && (previousBatchNo!=0))
    //                    {
    //                        startingBatchNo++;
    //                    }
    //                    previousBatchNo = notificationRule.BatchNo;
    //                    notificationRule.BatchNo = startingBatchNo;
    //                    base.SetObjectID(notificationRule, (tc.GenerateID("NotificationRule", "NotificationRuleID")));
    //                    notificationRule.NotificationID = (id);
    //                    NotificationDA.InsertNotificationRule(tc, notificationRule);
    //                }
    //            }
    //            else
    //            {
    //                NotificationDA.Update(tc, oNotification);

    //                NotificationDA.DeleteNotificationRules(tc,oNotification.ID);
    //                startingBatchNo = GetNewBatchNumber(tc);

    //                foreach (NotificationRule notificationRule in oNotification.NotificationRules)
    //                {
    //                    if (previousBatchNo != notificationRule.BatchNo && (previousBatchNo != 0))
    //                    {
    //                        startingBatchNo++;
    //                    }
    //                    previousBatchNo = notificationRule.BatchNo;
    //                    notificationRule.BatchNo = startingBatchNo;
    //                    base.SetObjectID(notificationRule, (tc.GenerateID("NotificationRule", "NotificationRuleID")));
    //                    notificationRule.NotificationID = (oNotification.ID);
    //                    NotificationDA.InsertNotificationRule(tc, notificationRule);
    //                }
    //            }

    //            return oNotification.ID;
    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }
    //    }

    //    public void SaveReply(NotificationParticipant oParticipant)
    //    {
    //        TransactionContext tc = null;
    //        tc = TransactionContext.Begin(true);
    //        try
    //        {
    //            if (oParticipant.IsNew)
    //            {
    //                int id = tc.GenerateID("NotificationParticipant", "NotificationParticipantID");
    //                base.SetObjectID(oParticipant, (id));
    //                NotificationDA.InsertReply(tc, oParticipant);
    //            }
    //            else
    //            {

    //            }
    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }
    //    }

    //    public void UpdateNotificationRule(NotificationRule notfRule)
    //    {
    //        TransactionContext tc = null;
    //        try
    //        {
    //            tc = TransactionContext.Begin(true);
    //            NotificationDA.UpdateNotficationRule(tc, notfRule);
    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }
    //    }

    //    public void Delete(int id)
    //    {
    //        TransactionContext tc = null;
    //        try
    //        {
    //            tc = TransactionContext.Begin(true);

    //            NotificationDA.DeleteParticipants(tc,id);
    //            NotificationDA.DeleteNotificationRules(tc, id);

    //            NotificationDA.Delete(tc, id);

    //        }
    //        catch (Exception e)
    //        {
    //            #region Handle Exception
    //            if (tc != null)
    //                tc.HandleError();
    //            ExceptionLog.Write(e);
    //            throw new ServiceException(e.Message, e);
    //            #endregion
    //        }
    //        finally
    //        {
    //            tc.End();
    //        }
    //    }


    //    #endregion


    //}
}