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

namespace LHDN.CRM.BC.Repositories
{
    public class TaskRepository : BaseRepository<CRMContext, Task>
    {
        public DataSet GetCaseOrActivity(TaskPagedListViewModel paging)
        {
            List<object> lstParam = new List<object>(new object[] {
                                        "@PageIndex", paging.PageIndex,
                                        "@PageSize", paging.Length,
                                        "@Regarding", paging.Regarding,
                                        "@RefNo", paging.RefNo,
                                        "@Kategori1ID", paging.Kategori1ID,
                                        "@Kategori2ID", paging.Kategori2ID,
                                        "@Kategori3ID", paging.Kategori3ID,
                                        "@Status1ID", paging.CaseStatusLevel1ID,
                                        "@Status2ID", paging.CaseStatusLevel2ID,
                                        "@Status3ID", paging.CaseStatusLevel3ID,
                                        "@ActStatusID", paging.ActStatusID,
                                        "@ActChannelID", paging.ActChannelID,
                                        "@AliranID", paging.AliranID
                                    });

            if (!string.IsNullOrEmpty(paging.SortExpression))
            {
                lstParam.AddRange(new object[] {
                "@SortExpression", paging.SortExpression
                });
            }

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

        public TaskViewModel GetTask(long taskID)
        {
            return ExecuteSP<TaskViewModel>("proc_GetTask", "@TaskID", taskID).FirstOrDefault();
        }

        public TaskViewModel GetPTPTaskByActivityID(long lngActivityID) {
            return ExecuteSP<TaskViewModel>("proc_GetPTPTaskByActivityID", "@ActivityID", lngActivityID).FirstOrDefault();
        }

        public bool DeleteTask(long lngTaskID)
        {
            DataSet ds = ExecuteSP("proc_DeleteTask", "@TaskID", lngTaskID);
            bool blnIsSuccess = false;

            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    blnIsSuccess = Convert.ToBoolean(ds.Tables[0].Rows[0][0]);
                }
            }
            return blnIsSuccess;
        }

        public byte[] GetChangeStampbyID(long ID)
        {
            byte[] changeStamp = null;
            DataSet ds = ExecuteSP("proc_GetChangeStamp", "@Table", (int)Constant.TableType.Task, "@ID", ID);

            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    changeStamp = (byte[])ds.Tables[0].Rows[0][0];
                }
            }
            return changeStamp;
        }

        public DataTable UpdateTask(TaskViewModel taskVM, string strLastUpdatedBy, DateTime dtLastUpdatedDateTime, string strLastUpdatedName, string strLastUpdatedEmail)
        {
            DataTable dTblTask = taskVM.ToDataTable<TaskViewModel>();
            dTblTask.Columns.Remove("TaskRefNo");
            dTblTask.Columns.Remove("RefNo");
            dTblTask.Columns.Remove("AssignByName");
            dTblTask.Columns.Remove("AssignByLogin");
            dTblTask.Columns.Remove("TaskStatus");
            dTblTask.Columns.Remove("ChangeStamp");
            dTblTask.Columns.Remove("LstActivity");
            dTblTask.Columns.Remove("LstCase");
            dTblTask.Columns.Remove("ChangeHistory");
            dTblTask.Columns.Remove("CanSearchCase");
            dTblTask.Columns.Remove("CanSearchAct");

            DataSet ds = ExecuteSP("proc_UpdateTask", "@tblTask", dTblTask,
                                                    "@LastUpdatedBy", strLastUpdatedBy,
                                                    "@LastUpdatedDateTime", dtLastUpdatedDateTime,
                                                    "@LastUpdatedName", strLastUpdatedName,
                                                    "@LastUpdatedEmail", strLastUpdatedEmail);

            DataTable dTblResult = new DataTable();
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    dTblResult = ds.Tables[0];
                }
            }

            return dTblResult;
        }

        public DataSet SearchTask(TaskPagedListViewModel paging)
        {
            List<object> lstParam = new List<object>(new object[] {
                                        "@PageIndex", paging.PageIndex,
                                        "@PageSize", paging.Length,
                                        "@TaskRefNo", paging.TaskRefNo,
                                        "@Subject", paging.Subject,
                                        "@Regarding", paging.Regarding,
                                        "@RefNo", paging.RefNo,
                                        "@TaskStatusID", paging.TaskStatusID,
                                        "@AssignToName", paging.AssignToName,
                                        "@DateFrom", paging.DueDateFrom,
                                        "@DateTo", paging.DueDateTo,
                                        "@Username",paging.CurrentLoginID
                                    });

            if (!string.IsNullOrEmpty(paging.SortExpression))
            {
                lstParam.AddRange(new object[] {
                "@SortExpression", paging.SortExpression
                });
            }

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

        public DataSet GetDashboardMyTask(TaskPagedListViewModel paging, string strCurrentUser)
        {
            List<object> lstParam = new List<object>(new object[] {
                                        "@PageIndex", paging.PageIndex,
                                        "@PageSize", paging.Length,
                                        "@AssignToLogin", strCurrentUser
                                    });

            if (!string.IsNullOrEmpty(paging.SortExpression))
            {
                lstParam.AddRange(new object[] {
                "@SortExpression", paging.SortExpression
                });
            }

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

        public  DataSet GetDashboardMyTaskSubmission(TaskPagedListViewModel paging, string strCurrentUser)
        {
            List<object> lstParam = new List<object>(new object[] {
                                        "@PageIndex", paging.PageIndex,
                                        "@PageSize", paging.Length,
                                        "@AssignByLogin", strCurrentUser
                                    });

            if (!string.IsNullOrEmpty(paging.SortExpression))
            {
                lstParam.AddRange(new object[] {
                "@SortExpression", paging.SortExpression
                });
            }

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

        public List<RecentActivityViewModel> GetRecentActivity(string strCurrentUser, int intRecords)
        {
            return ExecuteSP<RecentActivityViewModel>("proc_GetUserRecentActivity", "@UserLoginID", strCurrentUser,
                                                                                    "@Records", intRecords).ToList();
        }

        public long InsertRecentActivity(string strModule, long lngRefID, string strDescription, string strCreatedBy, string strCreatedByName, 
                                         DateTime dtCreatedDateTime, string strURL)
        {
            List<object> lstParam = new List<object>(new object[] {
                                        "@Module", strModule,
                                        "@ModuleRefID", lngRefID,
                                        "@Description", strDescription,
                                        "@UserLoginID", strCreatedBy,
                                        "@UserDisplayName", strCreatedByName,
                                        "@CreatedDateTime", dtCreatedDateTime,
                                        "@ModuleURL", strURL
                                    });
            DataSet ds = ExecuteSP("proc_InsertUserRecentActivity", lstParam.ToArray());

            long lngID = 0;
            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    lngID = Convert.ToInt64(ds.Tables[0].Rows[0][0]);
                }
            }

            return lngID;
        }

        public void DeleteRecentActivity()
        {
            ExecuteSP("proc_DeleteRecentActivity");
        }

        public List<WorkflowUserViewModel> GetAllUsers()
        {
            return ExecuteSP<WorkflowUserViewModel>("proc_GetAllUsers").ToList();
        }

        public DataSet GetTaskListByID(string strSortExpr, int intPageIndex, int intPageSize, long CaseID, long ActivityID, int StatusID)
        {
            List<object> lstParam = new List<object>(new object[] {
                                        "@PageIndex", intPageIndex,
                                        "@PageSize", intPageSize,
                                        "@CaseID", CaseID,
                                        "@ActivityID", ActivityID,
                                        "@TaskStatusID", StatusID,
                                    });

            if (!string.IsNullOrEmpty(strSortExpr))
            {
                lstParam.AddRange(new object[] {
                "@SortExpression", strSortExpr
                });
            }

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

        public List<TaskForCalendarViewModel> GetTaskForCalendar (string strLoginID)
        {
            return ExecuteSP<TaskForCalendarViewModel>("proc_GetAllTaskForCalendar", "@LoginID", strLoginID).ToList();

        }

        public List<TaskViewModel> GetAllTaskDetail()
        {
            return ExecuteSP<TaskViewModel>("proc_GetAllTaskDetailForNotifcation").ToList();

        }

    }
}
