using LHDN.CRM.BC.Common;
using LHDN.CRM.BC.Services;
using LHDN.CRM.Models.ViewModels;
using LHDN.CRM.WebAPI.CallCentre.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace LHDN.CRM.WebAPI.CallCentre.Controllers.Api.Form
{
    [APIErrorHandler]
    public class CFFController : BaseApiController
    {
        [HttpPost]
        public IHttpActionResult SaveActivityCFF(ActivityUpdateViewModel model)
        {
            ActReturnViewModel resultVM = new ActReturnViewModel();
            if (model != null) { 
                ActivityService svc = new ActivityService();
                long lngActivityID = 0;
                string strActRefNo = "";
                string strDescription = "";
                string strText = "";
                byte[] arrChangeStamp = null;
                bool blnIsStatusChanged = false;
                //bool blnIsSaveDraft = false;
                DateTime dtNow = DateTime.Now;
                string strUserName = Constant.System.Name;
                string strUserID = Constant.System.Name;
                string strUserEmail = AdminService.ParameterSettings.Where(x => x.ParameterID == 5).FirstOrDefault().ParameterValue;
                AuditService.ActionType actionType = new AuditService.ActionType();
                AuditService.AuditEventID eventID = model.Activity.IsDraft == true ? AuditService.AuditEventID.CreateActivityDraft : AuditService.AuditEventID.CreateUpdateActivityProfile;
                TaskViewModel modelTask = null;
                AuditService.ActionType taskAuditActionType = new AuditService.ActionType();

                var data = svc.SaveActivity(model, dtNow, out lngActivityID, out strActRefNo, out blnIsStatusChanged, out arrChangeStamp, out modelTask, out taskAuditActionType, null);

                List<AuditDetailViewModel> lstChgHistory = null;
                //TODO : 
                //1. system create draft eventid, userid =system, 
                //2. user save draft, action type= ADD, addinfo ='Create Activity From Draft'
                strText = string.Format(Constant.AuditAdditionalInfo.Activity, strUserName, strUserID, lngActivityID);

                if (model.Activity.ActivityID == 0) // Insert
                {
                    actionType = AuditService.ActionType.Add;
                    strDescription = string.Format(Constant.RecentActivity.ActivityCreatedMsg, strActRefNo);
                }
                //else //Update
                //{
                //    strText = model.IsSaveDraft == true ? "Create Activity from Draft. " + strText : strText;
                //    actionType = model.IsSaveDraft == true ? AuditService.ActionType.Add : AuditService.ActionType.Update;
                //    lstChgHistory = data.Select(a => new AuditDetailViewModel
                //    {
                //        FieldName = a.DisplayName,
                //        OldValue = a.OldValue,
                //        NewValue = a.NewValue
                //    }).ToList();

                //    if (blnIsStatusChanged)
                //    {
                //        var ActivityStatus = AdminService.LookupParams.Where(x => x.LookupCode == "ActivityStatus" && x.IsActive == true && x.LookupValue == ((int)Constant.StatusLevel1.Selesai).ToString()).FirstOrDefault();
                //        if (model.Activity.ActStatusID == ActivityStatus.LookupParamID)
                //            strDescription = string.Format(Constant.RecentActivity.ActivityClosedMsg, strActRefNo);
                //        else
                //            strDescription = string.Format(Constant.RecentActivity.ActivityReopenMsg, strActRefNo);
                //    }
                //    else
                //        strDescription = string.Format(Constant.RecentActivity.ActivityUpdatedMsg, strActRefNo);
                //}

                AuditLogTxn(eventID,
                            AuditService.ResultType.Success,
                            actionType,
                            strUserID,
                            dtNow,
                            strText,
                            lstChgHistory, "Activity", lngActivityID);

                //if (model.Activity.IsDraft != true)
                //    // Insert Recent Activity
                //    RecentActivityTxn("Activity", lngActivityID, strDescription, strUserID, strUserName, dtNow, string.Format(Constant.RecentActivity.ActivityURL, lngActivityID));

                //if (Convert.ToBoolean(Utilities.GetAppSetting("IntegrateWithCFF")))
                //{
                //    //Reply to TP = CFF & Outbound & CaseID > 0
                //    var aliran = AdminService.LookupParams.Where(x => x.LookupCode == "Aliran" && x.IsActive == true && x.LookupValue == Constant.Aliran.Outbound).FirstOrDefault();
                //    // Integrate with Kube
                //    if (model.Activity.ActChannelID == (int)Constant.ActChannel.CFF && model.Activity.AliranID == aliran.LookupParamID && model.Activity.CaseID > 0)
                //    {
                //        if (model.Activity.ActCFF != null)
                //        {
                //            List<long> lstCFFFormID = caseSvc.GetCFFListByCaseID((long)model.Activity.CaseID);
                //            CRMUpdateFieldViewModel cffUpdate = new CRMUpdateFieldViewModel();
                //            cffUpdate.CFFFormIDList = lstCFFFormID;
                //            cffUpdate.CRMLastUpdatedDate = dtNow;
                //            cffUpdate.CRMRemarks = model.Activity.ActCFF.CFFRemarks;
                //            cffUpdate.CRMStatus = Constant.Status.BelumSelesai;

                //            //HttpClient to call CFF.WebApi
                //            UpdateField(cffUpdate);
                //        }
                //    }
                //}

                //Task 
                //if (modelTask != null && modelTask.TaskRefNo != null && modelTask.TaskStatusID == (int)Constant.TaskStatus.Open)
                //{
                //    string strTaskRefNo = modelTask.TaskRefNo;
                //    long lngTaskID = modelTask.TaskID;
                //    string strTaskDescription = "";
                //    string strTaskAlertDescription = "";
                //    bool blnAlertIsRead = false;
                //    AuditService.AuditEventID taskEventID = AuditService.AuditEventID.CreateUpdateTask;
                //    if (taskAuditActionType == AuditService.ActionType.Update)
                //    {
                //        strText = "Update Task";
                //        strTaskDescription = string.Format(Constant.RecentActivity.TaskUpdatedMsg, strTaskRefNo);
                //    }
                //    else if (taskAuditActionType == AuditService.ActionType.Add)
                //    {
                //        strText = "Create Task";
                //        strTaskDescription = string.Format(Constant.RecentActivity.TaskCreatedMsg, strTaskRefNo);
                //    }
                //    else if (taskAuditActionType == AuditService.ActionType.Delete)
                //    {
                //        strText = "Delete Task";
                //        strTaskDescription = string.Format(Constant.RecentActivity.TaskDeletedMsg, strTaskRefNo);
                //    }

                //    AuditLogTxn(taskEventID,
                //            AuditService.ResultType.Success,
                //            taskAuditActionType,
                //            strUserID,
                //            dtNow,
                //            strText,
                //            null,
                //            "Task", lngTaskID);

                //    strTaskAlertDescription = string.Format(Constant.RecentActivity.TaskAlertForTask, strTaskRefNo);

                //    // Insert Recent Activity
                //    RecentActivityTxn("Task", lngTaskID, strTaskDescription, strUserID, strUserName, dtNow, string.Format(Constant.RecentActivity.TaskURL, lngTaskID));

                //    //Insert Task Alert
                //    RecentTaskAlertNotify(modelTask.AssignToLogin, (Int16)(Constant.TableType.Task), lngTaskID, (int)(Constant.MessageType.New), strTaskAlertDescription, string.Format(Constant.RecentActivity.TaskURL, lngTaskID), blnAlertIsRead, strUserID, strUserName, dtNow);

                //    // Send email
                //    if (modelTask.AssignToEmail != null)
                //    {
                //        List<string> lstEmailTo = new List<string>();
                //        List<string> lstEmailCC = new List<string>();
                //        lstEmailTo.Add(modelTask.AssignToEmail);
                //        lstEmailCC.Add(strUserEmail);
                //        emailSvc.SendEmailNotification(lstEmailTo, lstEmailCC, (int)Constant.EmailTemplate.TaskNotification, strUserID, lngTaskID, dtNow);
                //    }
                //}

                resultVM.ActivityID = lngActivityID;
                resultVM.ChangeStamp = arrChangeStamp;
                resultVM.Activity = model.Activity;
            }

            return Json(resultVM);
        }

        //[HttpPost]
        //public IHttpActionResult CreateCaseCFF(CaseUpdateViewModel model)
        //{
        //    CaseApiController caseApi = new CaseApiController();
        //    var data = caseApi.CreateCase(model);
        //    return Json(data);
        //}

    }
}