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

namespace LHDN.CRM.BC.Repositories
{
    public class WorkflowRepository : BaseRepository<CRMContext, Task>
    {
        public WorkflowActionViewModel GetExistingWorkflowRecord(long WorkflowID)
        {
            WorkflowActionViewModel objCurrentWorkflow = new WorkflowActionViewModel();
 
            DataSet ds = ExecuteSP("proc_GetWorkflowDetail", "@WorkflowID", WorkflowID);
            objCurrentWorkflow = DataTableToList<WorkflowActionViewModel>(ds.Tables[0]).FirstOrDefault();
            if (objCurrentWorkflow != null)
            {
                if (ds != null)
                {
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        objCurrentWorkflow = DataTableToList<WorkflowActionViewModel>(ds.Tables[0]).FirstOrDefault();
                    }

                    if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
                        objCurrentWorkflow.wfUser = DataTableToList<WorkflowActionViewModel>(ds.Tables[1]).ToList();
                }
            }

            return objCurrentWorkflow;
        }

        public WorkflowAssignToUnitIDViewModel GetWorkflowAssignToValue(WorkflowAssignToUnitIDViewModel Kategory)
        {
            return ExecuteSP<WorkflowAssignToUnitIDViewModel>("proc_GetWorkflowAssignToUnitID", "@Kategori1ID", Kategory.Kategori1,
                                                                  "@Kategori2ID", Kategory.Kategori2,
                                                                  "@Kategori3ID", Kategory.Kategori3,
                                                                  "@TPID", Kategory.TPID).FirstOrDefault();
        }

        public List<WorkflowUserViewModel> GetWorkFlowAllUserOrUnit(WorkflowUserViewModel datainfo)
        {
            var data = ExecuteSP<WorkflowUserViewModel>("proc_GetWorkflowSearchUserOrUnit", "@keyword", datainfo.keyword,
                                               "@AssignToType", datainfo.AssignToType,
                                               "@ManagerLoginId", datainfo.ManagerId).ToList();

            return data;
        }


        public DataTable InsertWorkflow(WorkflowUpdateViewModel caseWorkflow, WorkflowUserViewModel assignedUser, DateTime now,int TableType)
        {
            DataTable dTblResult = new DataTable();
            if (TableType == (int)Constant.TableType.Case)
            {
                DataSet ds = ExecuteSP("proc_InsertWorkflowAction", "@CaseID", caseWorkflow.Workflow.CaseID,
                                           "@ActID", 0,
                                           "@AssignToTypeID", assignedUser.AssignToType,
                                           "@AssignToLogin", assignedUser.ID,
                                           "@AssignToName", assignedUser.DisplayName,
                                           "@AssignToEmail", assignedUser.Email,
                                           "@ManagerFunctionRoleID", assignedUser.ManagerFunctionRoleID,
                                           "@WFStatusID", caseWorkflow.Workflow.WFStatusID,
                                           "@StartDate", now,
                                           "@Remarks", caseWorkflow.Workflow.Remarks,
                                           "@AssignBy", caseWorkflow.Workflow.AssignBy,
                                           "@AssignByName", caseWorkflow.Workflow.AssignByName,
                                           "@CreatedBy", caseWorkflow.Workflow.CreatedBy,
                                           "@CreatedDateTime", now,
                                           "@LastUpdatedBy", caseWorkflow.Workflow.LastUpdatedBy,
                                           "@LastUpdatedDateTime", now);

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

            }
            else
            {
                DataSet ds = ExecuteSP("proc_InsertWorkflowAction", "@CaseID", 0,
                                          "@ActID", caseWorkflow.Workflow.ActivityID,
                                         "@AssignToTypeID", assignedUser.AssignToType,
                                         "@AssignToLogin", assignedUser.ID,
                                         "@AssignToName", assignedUser.DisplayName,
                                         "@AssignToEmail", assignedUser.Email,
                                         "@ManagerFunctionRoleID", assignedUser.ManagerFunctionRoleID,
                                         "@WFStatusID", caseWorkflow.Workflow.WFStatusID,
                                         "@StartDate", now,
                                         "@Remarks", caseWorkflow.Workflow.Remarks,
                                         "@AssignBy", caseWorkflow.Workflow.AssignBy,
                                         "@AssignByName", caseWorkflow.Workflow.AssignByName,
                                         "@CreatedBy", caseWorkflow.Workflow.CreatedBy,
                                         "@CreatedDateTime", now,
                                         "@LastUpdatedBy", caseWorkflow.Workflow.LastUpdatedBy,
                                         "@LastUpdatedDateTime", now);


                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 long InsertWorkflowHistory(WorkflowActionViewModel OriCaseWorkflowData, DateTime dtUpdated,int TableType)
        {
            long i = 0;
            if(TableType == 2)  //2-->Case, 3-->Activity
            {
               i = ExecuteUpdateSP("proc_InsertWorkFlowHistory", "@CaseID", OriCaseWorkflowData.CaseID,
                                                          "@ActId", 0,
                                                          "@AssignBy", OriCaseWorkflowData.AssignBy,
                                                          "@AssignByName", OriCaseWorkflowData.AssignByName,
                                                          "@WFStatusID", OriCaseWorkflowData.WFStatusID,
                                                          "@StartDate", OriCaseWorkflowData.StartDate,
                                                          "@CompletedDate", DateTime.Now,
                                                          "@DecisionID", OriCaseWorkflowData.DecisionID,
                                                          "@Remarks", OriCaseWorkflowData.Remarks,
                                                          "@CloseRemarks", OriCaseWorkflowData.CloseRemarks,
                                                          "CreatedDateTime", dtUpdated
                                                          );
            }
            else
            {
                i = ExecuteUpdateSP("proc_InsertWorkFlowHistory", "@CaseID",0,
                                                       "@ActId", OriCaseWorkflowData.ActivityID,
                                                       "@AssignBy", OriCaseWorkflowData.AssignBy,
                                                       "@AssignByName", OriCaseWorkflowData.AssignByName,
                                                       "@WFStatusID", OriCaseWorkflowData.WFStatusID,
                                                       "@StartDate", OriCaseWorkflowData.StartDate,
                                                       "@CompletedDate", DateTime.Now,
                                                       "@DecisionID", OriCaseWorkflowData.DecisionID,
                                                       "@Remarks", OriCaseWorkflowData.Remarks,
                                                       "@CloseRemarks", OriCaseWorkflowData.CloseRemarks,
                                                       "CreatedDateTime", dtUpdated
                                                       );
            }

            return i;
        }

        public int InsertChangeHistoryDetail(List<ChangeHistoryUpdateViewModel> lst, string strUserName, string strUserID, DateTime dtUpdated,int intTableType)
        {
            DataTable dTbl = lst.ToDataTable<ChangeHistoryUpdateViewModel>();
            dTbl.Columns.Remove("IsChanged");

            int i = ExecuteUpdateSP("proc_InsertChangeHistory",
                                    "@tbl", dTbl,
                                    "@TableType", intTableType,
                                    "@CreatedByName", strUserName,
                                    "@CreatedBy", strUserID,
                                    "@CreatedDateTime", dtUpdated);
            return i;

        }

        public long UpdateActivityOrCaseForWorkflowAssign(WorkflowActionViewModel WorkflowData,long lgWFStatusID, WorkflowUserViewModel assignedUser, int TableType, DateTime now)
        {
            long i = 0;
            if(TableType == 2) //2 is for case table
            {
                i = ExecuteUpdateSP("proc_UpdateCaseOrActivityForWorkflowAssign",
                                    "@CaseID", WorkflowData.CaseID,
                                    "@ActId", 0,
                                    "@AssignToLogin", assignedUser.ID,
                                    "@AssignToName", assignedUser.DisplayName,
                                    "@AssignToEmail", assignedUser.Email,
                                    "@WorkflowStatusID", lgWFStatusID,
                                    "@LastUpdatedBy", WorkflowData.AssignBy,
                                    "@AssignByName", WorkflowData.AssignByName,                                        
                                    "@LastUpdatedDateTime", now
                                 );
            }
            else
            {
                i = ExecuteUpdateSP("proc_UpdateCaseOrActivityForWorkflowAssign",
                                   "@CaseID",0, "@ActId", WorkflowData.ActivityID,
                                   "@AssignToLogin", assignedUser.ID,
                                   "@AssignToName", assignedUser.DisplayName,
                                    "@AssignToEmail", assignedUser.Email,
                                    "@WorkflowStatusID", lgWFStatusID,
                                    "@LastUpdatedBy", WorkflowData.AssignBy,
                                    "@AssignByName", WorkflowData.AssignByName,
                                    "@LastUpdatedDateTime", now
                                );
            }


            return i;
        }

        public long UpdateCaseClosedForWorkflow(CasesWorkflowActionViewModel workflow, DateTime dtUpdated)
        {
            long i = ExecuteUpdateSP("proc_UpdateClosedCaseForWorkflow",
                                  "@CaseID", workflow.CaseID,
                                  "@LastUpdatedBy", workflow.AssignBy,
                                  "@AssignByName", workflow.AssignByName,
                                  "@LastUpdatedDateTime", dtUpdated
                               );

            return i;
        }

        public bool CheckExistsFunctionalRoleMember(int intUnitID, string strManagerFunctionalRoleID) {

            int i = ExecuteSPRetInt("proc_CheckExistsFunctionalRoleMember",
                                  "@UnitID", intUnitID,
                                  "@ManagerFunctionalRoleID", strManagerFunctionalRoleID
                               );

            return i>0? true : false;
        }

        public int InsertWFForReassignment(WorkflowUpdateViewModel wfItem, long workFlowID, int IsNewCase, DateTime now)
        {
            DataTable dTblResult = new DataTable();
           
               int iResult = ExecuteUpdateSP("proc_InsertWFForReassignment",
                                         "@WorkflowID", IsNewCase ==1 ? 0 : workFlowID,
                                         "@CaseID", IsNewCase == 1 ? workFlowID : 0,
                                         "@AssignToTypeID", wfItem.Workflow.AssignToTypeID,
                                         "@AssignToLogin", wfItem.Workflow.AssignTo.ID,
                                         "@AssignToName", wfItem.Workflow.AssignTo.DisplayName,
                                         "@AssignToEmail", string.IsNullOrEmpty(wfItem.Workflow.AssignTo.Email)?"" : wfItem.Workflow.AssignTo.Email,
                                         "@ManagerFunctionRoleID", string.IsNullOrEmpty(wfItem.Workflow.AssignTo.ManagerFunctionRoleID)? "" : wfItem.Workflow.AssignTo.ManagerFunctionRoleID,
                                         "@WFStatusID", wfItem.Workflow.WFStatusID,
                                         "@StartDate", now,
                                         "@Remarks", wfItem.Workflow.Remarks,
                                         "@AssignBy", wfItem.Workflow.AssignBy,
                                         "@AssignByName", wfItem.Workflow.AssignByName,
                                         "@CreatedBy", wfItem.Workflow.CreatedBy,
                                         "@CreatedDateTime", now,
                                         "@LastUpdatedBy", wfItem.Workflow.LastUpdatedBy,
                                         "@LastUpdatedDateTime", now);
         

            return iResult;
        }

        public int MoveWFHistoryForReassignment(WorkflowUpdateViewModel wfItem, long workFlowID, DateTime now)
        {
            int iResult = ExecuteUpdateSP("proc_MoveWFHistForReassignment",
                                     "@WorkflowID", workFlowID,
                                     "@AssignBy", wfItem.Workflow.AssignBy,
                                     "@AssignByName", wfItem.Workflow.AssignByName,
                                     "@CreatedDateTime", now);


            return iResult;
        }

        public DataTable UpdateTableWorkflowAssign(WorkflowUpdateViewModel wfItem, long lgWFStatusID, long workFlowID, int IsNewCase, DateTime now)
        {
            DataSet ds = new DataSet();
            ds = ExecuteSP("proc_UpdateTableForWorkflowAssign", 
                                    "@WorkflowID", IsNewCase == 1 ? 0 : workFlowID,
                                    "@CaseID", IsNewCase == 1 ? workFlowID : 0,
                                    "@AssignToLogin", wfItem.Workflow.AssignTo.ID,
                                    "@AssignToName", wfItem.Workflow.AssignTo.DisplayName,
                                    "@AssignToEmail", string.IsNullOrEmpty(wfItem.Workflow.AssignTo.Email) ? "" : wfItem.Workflow.AssignTo.Email,
                                    "@WorkflowStatusID", lgWFStatusID,
                                    "@LastUpdatedBy", wfItem.Workflow.AssignByName,
                                    "@AssignByLogin", wfItem.Workflow.AssignBy,
                                    "@AssignByName", wfItem.Workflow.AssignByName,
                                    "@LastUpdatedDateTime", now
                                 );

            return ds.Tables[0];
        }

        public WorkflowAssignToUnitIDViewModel GetCaseAssignDefaultUnitByUnitID(int UnitID)
        {
            return ExecuteSP<WorkflowAssignToUnitIDViewModel>("proc_GetCaseAssignForUnitID", "@UnitID", UnitID).FirstOrDefault();
        }

    }
}
