using System;
using System.Collections.Generic;
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 VoiceController : BaseApiController
    {
        [HttpGet]
        [APIErrorHandler]
        public IHttpActionResult Hotline([FromUri] HotlineRequestViewModel request)
        {
            TaxPayerService tps = new TaxPayerService();
            List<HotlineResponseViewModel> HotlineVM = new List<HotlineResponseViewModel>();
            string strAdditionalData = "[CC Hotline Verification]IDNo : {0}, IDType : {1};";
            DateTime now = DateTime.Now;
            bool blnIsInValid = false;
            HttpResponseMessage responseMessage = null;

            if (request.IDNo != null)
            {
                HotlineVM = tps.GetHotlineRequest(request);

                if (HotlineVM.Count > 0)
                {
                    if (HotlineVM.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, request.IDType));
                }
                throw new HttpResponseException(responseMessage);
            }
            else
            {
                return Ok(HotlineVM[0]);
            }
        }
    }
}