using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using LHDN.CRM.Models.Models;
using LHDN.CRM.BC.Common;
using LHDN.CRM.Models.ViewModels;

namespace LHDN.CRM.BC.Repositories
{
    public class EmailRepository: BaseRepository<CRMContext, EmailQueue>
    {
        public DataSet GetBookmarkValue(int intTemplateID, long lngRefTableID,bool isActAssign = false)
        {
            List<object> lstParam = new List<object>(new object[] {
                                        "@TemplateID", intTemplateID,
                                        "@RefTableID", lngRefTableID,
                                        "@IsActAssignTemplate",isActAssign
                                    });

            return ExecuteSP("proc_GetBookmarkValue", lstParam.ToArray());
        }

        public DataSet GetCorrespondenceTemplateByID(int templateId)
        {
            List<object> lstParam = new List<object>(new object[] {
                                        "@TemplateID", templateId
                                    });

            return ExecuteSP("proc_GetCorrespondenceTemplateByID", lstParam.ToArray());
        }

        public long SendEmailNotification(EmailQueueViewModel emailqueue, DateTime now)
        {
            long i = ExecuteUpdateSP("proc_InsertEmailQueue", "@From", emailqueue.From,
                                                              "@SendTo", emailqueue.To,
                                                              "@CC", emailqueue.CC,
                                                              "@BCC", emailqueue.BCC,
                                                              "@Subject", emailqueue.Subject,
                                                              "@Body", emailqueue.Body,
                                                              "@FailAttempt",0,
                                                              "@CreatedBy", emailqueue.CreatedBy,
                                                              "@CreatedDateTime",now,
                                                              "@LastUpdatedBy", emailqueue.LastUpdatedBy,
                                                              "@LastUpdatedDateTime",now,
                                                              "@AttachmentTableType", emailqueue.AttachmentTableType,
                                                              "@RefAttachmentTableID", emailqueue.RefAttachmentTableID);

            return i;
        }

        public List<EmailQueueViewModel> GetEmailQueueList()
        {
            return ExecuteSP<EmailQueueViewModel>("proc_GetEmailQueueList").ToList();
        }
         
        public List<EmailQueueAttachmentViewModel> GetEmailQueueAttachmentList()
        {
            return ExecuteSP<EmailQueueAttachmentViewModel>("proc_GetEmailQueueAttachmentList").ToList();
        }

        public void InsertEmailHist(long lngEmailQueueID)
        {
            ExecuteSP("proc_InsertEmailHist", "@EmailQueueID", lngEmailQueueID,
                                              "@EmailSentOn", DateTime.Now);

        }

        public void DeleteEmailQueue(long lngEmailQueueID)
        {
            ExecuteSP("proc_DeleteEmailQueue", "@EmailQueueID", lngEmailQueueID);
        }

        public void UpdateEmailFailAttempt(EmailQueueViewModel objEmail)
        {
            ExecuteSP("proc_UpdateEmailFailAttempt", "@EmailQueueID", objEmail.EmailQueueID,
                                                 "@LastUpdatedBy", objEmail.CreatedBy,
                                                 "@LastUpdatedDateTime", objEmail.CreatedDateTime);
        }

        public List<CaseKategoriDocReceivedViewModel> GetAllKategoryDocumentByRefID(long ReftableID,int TableType,string ActDocID="")
        {
            List<CaseKategoriDocReceivedViewModel> lst = new List<CaseKategoriDocReceivedViewModel>();
            DataSet ds = new DataSet();

            if(TableType == (int)Constant.TableType.Activity)
            {
                ds = ExecuteSP("proc_GetActDocReceivedForCase", "@CaseID", 0, "@ActID", ReftableID);
            }
            else
            {
                if(ActDocID != "" && ActDocID != null)
                {
                    ds = ExecuteSP("proc_GetActDocReceivedForCase", "@CaseID", 0, "@ActID", ReftableID);
                }
                //else
                //{
                //    ds = ExecuteSP("proc_GetActDocReceivedForCase", "@CaseID", ReftableID, "@ActID", 0);
                //}
              
            }
          
            if (ds.Tables.Count>0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    var actDocLst = DataTableToList<ActDocReceivedViewModel>(ds.Tables[0]).ToList();

                    //distint activity list
                    var activityLst = actDocLst.GroupBy(x => x.ActivityID).Select(grp => grp.First()).ToList();

                    foreach (var a in activityLst)
                    {
                        CaseKategoriDocReceivedViewModel obj = new CaseKategoriDocReceivedViewModel
                        {
                            ActivityID = a.ActivityID,
                            ActRefNo = a.ActRefNo,
                            KategoriDocReceived = new List<KategoriDocReceived>()
                        };
                        lst.Add(obj);
                    }

                    //get each activity kat doc list
                    foreach (var a in lst)
                    {
                        var katDocLst = actDocLst.Where(x => x.ActivityID == a.ActivityID).GroupBy(x => x.DocListID).Select(grp => grp.First()).ToList();
                        List<KategoriDocReceived> _lstKatDoc = new List<KategoriDocReceived>();
                        foreach (var b in katDocLst)
                        {
                            KategoriDocReceived obj = new KategoriDocReceived
                            {
                                DocListID = b.DocListID,
                                KategoriDocCode = b.KategoriDocCode,
                                KategoriDocName = b.KategoriDocName,
                                ActDocReceived = new List<ActDocReceivedViewModel>()
                            };
                            a.KategoriDocReceived.Add(obj);
                        }
                    }

                    //get each doc list act doc received
                    foreach (var a in lst)
                    {
                        foreach (var b in a.KategoriDocReceived)
                        {
                            foreach (var c in actDocLst)
                            {

                                if (c.ActivityID == a.ActivityID && c.DocListID == b.DocListID)
                                {
                                    b.ActDocReceived.Add(c);
                                }
                            }
                        }
                    }
                }
            }

            return lst;
        }
    }
}
