using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Web.Http;
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 SelfServicesController : BaseApiController
    {
        [HttpGet]
        [APIErrorHandler]
        public IHttpActionResult Verify([FromUri] ServiceVerificationRequestViewModel request)
        {
            TaxPayerService tps = new TaxPayerService();
            List<ServiceVerificationResponseViewModel> VerifyVM = new List<ServiceVerificationResponseViewModel>();
            string strAdditionalData = "[CC Self serfice Verify] IDNo : {0}, IDType : {1}, DOB : {2}, PostCode : {3}, AccountNo : {4}, PhoneNo {5};";
            DateTime now = DateTime.Now;
            bool blnIsInValid = false;
            HttpResponseMessage responseMessage = null;

            if (request.IDNo != null)
            {
                string strDOB = request.Detailed?.DOB.ToString();

                if (strDOB != null)
                {
                    DateTime dDate;
                    if (DateTime.TryParse(request.Detailed.DOB.ToString(), out dDate))
                    {
                        request.Detailed.DOB = String.Format("{0:yyyy-M-d}", dDate);
                    }
                    else
                    {
                        blnIsInValid = true;
                        responseMessage = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, Constant.WebRequest.InvalidRequestMsg);
                    }
                }

                VerifyVM = tps.GetServiceVerificationRequest(request);

                if (VerifyVM.Count > 0)
                {
                    if (VerifyVM.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.IDNo.ToStringSafe(), request.IDType.ToStringSafe(),
                                                request.Detailed?.DOB.ToStringSafe(), request.Detailed?.PostCode.ToStringSafe(), request.Detailed?.AccountNo.ToStringSafe(),
                                                request.Detailed?.PhoneNo.ToStringSafe()));
                }
                throw new HttpResponseException(responseMessage);
            }
            else
            {
                return Ok(VerifyVM[0]);
            }
        }
    }
}