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

namespace LHDN.CRM.WebAPI.External.Controllers.Api.CallingCentre
{
    public class TaxPayerController : BaseApiController
    {
        [HttpGet]
        [APIErrorHandler]
        public IHttpActionResult Language([FromUri] LanguageRequestViewModel request)
        {
            TaxPayerService tps = new TaxPayerService();
            List<LanguageResponseViewModel> LanguageVM = new List<LanguageResponseViewModel>();
            string strAdditionalData = "[CC Request Language]TelNo : {0}";
            DateTime now = DateTime.Now;
            bool blnIsInValid = false;
            HttpResponseMessage responseMessage = null;

            if (request?.PhoneNo != null)
            {
                LanguageVM = tps.GetLanguageRequest(request);

                if (LanguageVM.Count > 0)
                {
                    blnIsInValid = false;
                    //if (LanguageVM.Count > 1)
                    //{
                    //    blnIsInValid = true;
                    //    responseMessage = Request.CreateErrorResponse(HttpStatusCode.Ambiguous, Constant.WebRequest.DuplicateRecordMsg);
                    //}
                }
                else
                {
                    blnIsInValid = true;
                    responseMessage = Request.CreateErrorResponse(HttpStatusCode.BadRequest, Constant.WebRequest.NoRecordMsg);
                }
            }
            else
            {
                blnIsInValid = true;
                responseMessage = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, Constant.WebRequest.InvalidRequestMsg);
            }

            // Result
            if (blnIsInValid)
            {
                if (responseMessage.StatusCode == HttpStatusCode.InternalServerError)
                {
                    AuditLogTxn(AuditService.AuditEventID.ViewTaxPayer,
                        AuditService.ResultType.Failed,
                        AuditService.ActionType.View,
                        Constant.System.ContactCentre,
                        now,
                        string.Format(strAdditionalData, request?.PhoneNo.ToStringSafe()));
                }
                throw new HttpResponseException(responseMessage);
            }
            else
            {
                return Ok(LanguageVM[0]);
            }
        }

        [HttpGet]
        [APIErrorHandler]
        public IHttpActionResult Verify([FromUri] VerifyRequestViewModel request)
        {
            TaxPayerService tps = new TaxPayerService();
            List<VerifyResponseViewModel> VerifyVM = new List<VerifyResponseViewModel>();
            string strAdditionalData = "[CC Verify TaxPayer] IDNo : {0}, IDType : {1}, SocialName : {2}";
            DateTime now = DateTime.Now;
            bool blnIsInValid = false;
            HttpResponseMessage responseMessage = null;

            if (request.IDNo != null)
            {
                if (request.SocialName == null)
                {
                    request.SocialName = "";
                }
                VerifyVM = tps.GetVerifyRequest(request);

                if (VerifyVM.Count > 0)
                {
                    if (VerifyVM.Count > 1)
                    {
                        blnIsInValid = true;
                        responseMessage = Request.CreateErrorResponse(HttpStatusCode.Ambiguous, Constant.WebRequest.DuplicateRecordMsg);
                    }
                    else
                    {
                        return Ok(VerifyVM[0]);
                    }
                }
                else
                {
                    blnIsInValid = true;
                    responseMessage = Request.CreateErrorResponse(HttpStatusCode.BadRequest, Constant.WebRequest.NoRecordMsg);
                }
            }
            else
            {
                blnIsInValid = true;
                responseMessage = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, Constant.WebRequest.InvalidRequestMsg);
            }

            // Result
            if (blnIsInValid)
            {
                if (responseMessage.StatusCode == HttpStatusCode.InternalServerError)
                {
                    AuditLogTxn(AuditService.AuditEventID.ViewTaxPayer,
                                AuditService.ResultType.Failed,
                                AuditService.ActionType.View,
                                Constant.System.ContactCentre,
                                now,
                                string.Format(strAdditionalData, request?.IDNo.ToStringSafe(), request?.IDType.ToStringSafe(), request?.SocialName.ToStringSafe()));
                }
                throw new HttpResponseException(responseMessage);
            }
            else
            {
                return Ok(VerifyVM[0]);
            }
        }

        [HttpGet]
        [APIErrorHandler]
        public IHttpActionResult BlackListSekatan([FromUri] BlacklistRequestViewModel request)
        {
            TaxPayerService tps = new TaxPayerService();
            BlacklistResponseViewModel BlackListVM = new BlacklistResponseViewModel();
            string strAdditionalData = "[CC TP BlackList]IDNo : {0}, IDType : {1};";
            DateTime now = DateTime.Now;
            bool blnIsInValid = false;
            HttpResponseMessage responseMessage = null;

            if (request?.IDNo != null)
            {
                BlackListVM = tps.GetBlacklistRequest(request);

                if (BlackListVM.Count != null)
                {
                    if (BlackListVM.Count > 1)
                    {
                        blnIsInValid = true;
                        responseMessage = Request.CreateErrorResponse(HttpStatusCode.Ambiguous, Constant.WebRequest.DuplicateRecordMsg);
                    }
                    else
                    {
                        blnIsInValid = true;
                        responseMessage = Request.CreateErrorResponse(HttpStatusCode.BadRequest, Constant.WebRequest.NoRecordMsg);
                    }
                }
                else
                {
                    return Ok(BlackListVM);
                }
            }
            else
            {
                blnIsInValid = true;
                responseMessage = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, Constant.WebRequest.InvalidRequestMsg);
            }

            // Result
            if (blnIsInValid)
            {
                if (responseMessage.StatusCode == HttpStatusCode.InternalServerError)
                {
                    AuditLogTxn(AuditService.AuditEventID.ViewTaxPayer,
                                AuditService.ResultType.Failed,
                                AuditService.ActionType.View,
                                Constant.System.ContactCentre,
                                now,
                                string.Format(strAdditionalData,
                                            request?.IDNo.ToStringSafe(), request?.IDType.ToStringSafe()));
                }
                throw new HttpResponseException(responseMessage);
            }
            else
            {
                return Ok(BlackListVM);
            }
        }

        [HttpPost]
        [APIErrorHandler]
        public IHttpActionResult UpdateLanguage([FromUri] UpdateLanguageRequestViewModel model)
        {
            TaxPayerService tps = new TaxPayerService();
            string strUserName = Constant.System.ContactCentre;
            string strUserID = Constant.System.ContactCentre;
            string strAdditionalData = "[CC Update Language]IDNo : {0}, IDType : {1}, PhoneNo : {2}, LanguageID : {3}";
            DateTime now = DateTime.Now;

            if (tps.ValidateUpdateLanguage(model))
            {
                int intCount = 0;
                List<ChangeHistoryUpdateViewModel> data = tps.UpdateLanguageID(model, strUserName, strUserID, now, out intCount);
                if (data != null && data.Count > 0 && intCount == 1)
                {
                    AuditLogTxn(AuditService.AuditEventID.UpdateTaxPayers,
                                  AuditService.ResultType.Success,
                                  AuditService.ActionType.Update,
                                  strUserID,
                                  now,
                                  "Update Language ID",
                                   data.Select(a => new AuditDetailViewModel
                                   {
                                       FieldName = a.DisplayName,
                                       OldValue = a.OldValue,
                                       NewValue = a.NewValue
                                   }).Cast<AuditDetailViewModel>().ToArray().ToList(),
                                    "TaxPayer", data[0].ID
                                );
                }
                else
                {
                    if (intCount == 0)
                    {
                        throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, Constant.WebRequest.NoRecordMsg));
                    }
                    //else if (intCount > 1)
                    //{
                    //    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Ambiguous, Constant.WebRequest.DuplicateRecordMsg));
                    //}
                }
            }
            else
            {
                AuditLogTxn(AuditService.AuditEventID.ViewTaxPayer,
                                   AuditService.ResultType.Failed,
                                   AuditService.ActionType.View,
                                   "SYSTEM",
                                   now,
                                   string.Format(strAdditionalData, model?.IDNo.ToStringSafe(), model?.IDType.ToStringSafe(), model?.LanguageID.ToStringSafe()));
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, Constant.WebRequest.InvalidRequestMsg));
            }

            return Ok();
        }


    }
}