using LHDN.CRM.BC.Repositories;
using LHDN.CRM.Models.Models;
using LHDN.CRM.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LHDN.CRM.BC.Services
{
    public class AuditService : ServiceBase<AuditMaster>
    {
        private AuditRepository auditRepo = new AuditRepository();

        public enum ResultType
        {
            Failed = 'F',
            Success = 'S'
        }

        public enum ActionType
        {
            Add,
            Update,
            Delete,
            Approve,
            Cancel,
            Reject,
            ViewReport,
            GenerateReport,
            View
        }

        public enum AuditEventID
        {
            Login = 10001,
            Logout = 10002,
            Dashboard = 10003,
            ViewTaxPayer = 20001,
            UpdateTaxPayers = 20002,
            SearchTaxPayer = 20003,
            ViewActivityProfile = 30001,
            CreateUpdateActivityProfile = 30002,
            CreateUpdateActivityCatatan = 30003,
            SearchActivityCatatan = 30004,
            AssignActivityProfile = 30005,
            ViewMyPendingActivity = 30006,
            DeleteActivity = 30007,
            ReopenActivity = 30008,
            ViewDMSDocumentActivity = 30009,
            PrintNotaTemudugaActivity = 30010,
            RetrieveTranscript = 30011,
            RetrieveEmailContent = 30012,
            CreateActivityDraft = 30013, 
            SendEmail = 30014,
            ViewCaseProfile = 40001,
            CreateUpdateCaseProfile = 40002,
            TransfertoCase = 40003,
            SearchCaseProfle = 40004,
            CreateUpdateCaseCatatan = 40005,
            AssignBackOffice = 40006,
            ViewMyPendingCase = 40007,
            CloseCase = 40008,
            ReopenCase = 40009,
            ReassignCase = 40010,
            ViewDMSDocumentCase = 40011,
            PrintNotaTemudugaCase = 40012,
            DeleteCase = 40013,
            CreateUpdateTask = 50001,
            DeleteTask = 50002,
            ViewTask = 50003,
            SearchTask = 50004,
            UploadHCLEmail = 60001,
            SearchHCLEmailBlastList = 60002,
            ViewHCLEmailBlastProfile = 60003,
            EditHCLEmailBlastProfile = 60004,
            ManageHCLEmailBlastSegmentisation = 60005,
            ManageHCLEmailBlastCampaign = 60006,
            RejectHCLEmailBlastList = 60007,
            VerifyHCLEmailBlastList = 60008,
            LockHCLEmailBlastList = 60009,
            ReleaseHCLEmailBlastList = 60010,
            SearchOutboundList = 70001,
            ViewOutboundProfile = 70002,
            EditOutboundProfile = 70003,
            ManageSegmentisation = 70004,
            ManageCampaign = 70005,
            ReturnOutboundList = 70006,
            VerifyOutboundList = 70007,
            LockOutboundList = 70008,
            ReleaseOutboundList = 70009,
            CreateUpdateUserRole = 80001,
            ManageParameter = 80002
        }

        public long SaveAudit(int auditEventID,
            ResultType resultType,
            ActionType actionType,
            string userID,
            string creator,
            DateTime createdDate,
            string additionalInfo = null,
            List<AuditDetailViewModel> lstAuditDetails = null,
            string refTableName = null,
            long? refTableID = null)
        {
            long auditID = 0;
            AuditEvent evt = AdminService.AuditEvents.First(e => e.AuditEventID == auditEventID);

            AuditMaster objAuditMaster = new AuditMaster()
            {
                AuditModule = evt.AuditModule,
                AuditEventID = evt.AuditEventID,
                AuditEventName = evt.AuditEventName,
                ResultType = ((char)resultType).ToString(),
                ActionType = Enum.GetName(typeof(ActionType), actionType),
                AdditionalInfo = additionalInfo,
                RefTableName = refTableName,
                RefTableID = refTableID,
                UserID = userID,
                CreatedBy = creator,
                CreatedDateTime = createdDate
            };

            if (lstAuditDetails != null && lstAuditDetails.Count > 0)
            {
                var details = from d in lstAuditDetails
                              select new AuditDetail
                              {
                                  FieldName = d.FieldName,
                                  OldValue = d.OldValue,
                                  NewValue = d.NewValue,
                                  CreatedBy = creator,
                                  CreatedDateTime = createdDate
                              };

                objAuditMaster.AuditDetails = details.ToList();
            }

            auditID = auditRepo.SaveAudit(objAuditMaster);

            return auditID;
        }
    }
}
