using LHDN.CRM.BC.Services;
using LHDN.CRM.Models.ViewModels;
using LHDN.CRM.WebAPI.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Caching;
using System.Web;
using System.Web.Http;
using LHDN.CRM.BC.Common;
using System.Transactions;

namespace LHDN.CRM.WebAPI.Controllers.Api
{
    [CRMAuthorize]
    [ApiErrorHandler]
    public class TaxPayerApiController : BaseApiController
    {
        private TaxPayerService svcTP = new TaxPayerService();
        private UserProfileService user = new UserProfileService();

        [HttpGet]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch,Constant.Authentication.TaxPayer.BiasaOwnBranch,Constant.Authentication.TaxPayer.VIPAllBranch,Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetTaxPayer(long id)
        {
            TaxPayerViewModel data = null;
            DateTime now = DateTime.Now;
            //throw new Exception("test api error");

            data = svcTP.GetById(id);

            string strAdditionalData = "TPID : {0}, No Rujukan : {1}";
            AuditLogTxn(AuditService.AuditEventID.ViewTaxPayer,
                AuditService.ResultType.Success,
                AuditService.ActionType.View,
                user.LoginID,
                now,
                string.Format(strAdditionalData, id.ToStringSafe(), data.NoCukaiCode+ data.NoCukai));

            return Json(data);
        }
         

        [HttpPost]
        public IHttpActionResult GetPagedChangeHistory(ChangeHistoryPagedListViewModel paging)
        {
            //var data = svcTP.GetChangesHistory(paging.TPID, paging.ActivityID, paging.CaseID);
            int intCount = 0;
            var lst = svcTP.GetChangesHistory(paging, out intCount);
            DTResultViewModel<ChangeHistoryViewModel> objRecord = null;

            objRecord = new DTResultViewModel<ChangeHistoryViewModel>()
            {
                draw = paging.Draw,
                recordsTotal = intCount, //data.Count,
                recordsFiltered = intCount, //data.Count,
                data = lst
            };


            return Json(objRecord);
        }

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.UpdateTPProfile)]
        public IHttpActionResult UpdateInOrOutBoundDetail(TaxPayerUpdateViewModel model)
        {
            if (ModelState.IsValid)
            {
                string strUserName = user.DisplayName;
                string strUserID = user.LoginID;

                DateTime now = DateTime.Now;
                List<ChangeHistoryUpdateViewModel> data = svcTP.SaveInOutBoundDetails(model, strUserName, strUserID, now);

                using (var scope = new TransactionScope(TransactionScopeOption.Required))
                {
                    AuditLogTxn(AuditService.AuditEventID.UpdateTaxPayers,
                              AuditService.ResultType.Success,
                              AuditService.ActionType.Update,
                              strUserID,
                              now,
                              "Update Tax Payers",
                               data.Select(a => new AuditDetailViewModel
                               {
                                   FieldName = a.DisplayName,
                                   OldValue = a.OldValue,
                                   NewValue = a.NewValue
                               }).Cast<AuditDetailViewModel>().ToArray().ToList(),
                                "TaxPayer", model.TaxpayerCorrespondence.TPID
                            );
                    scope.Complete();
                }
            }
            else
            {
                throw new Exception(ModelStateErrMessage(ModelState));
            }

            return Ok();
        }

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.SearchTPProfile)]
        public IHttpActionResult SearchTaxPayer(ActSearchTPViewModel model)
        {
            List<TPForActivityViewModel> data = svcTP.SearchTaxPayer(model);
            return Json(data);
        }

        [HttpGet]
        public IHttpActionResult GetDropdown()
        {
            return Json(svcTP.GetDropdown());
        }

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch,Constant.Authentication.TaxPayer.BiasaOwnBranch, Constant.Authentication.TaxPayer.VIPAllBranch,Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetOutboundListByTaxPayer(OutboundTabPageListViewModel paging)
        {
            //var data = svcTP.GetChangesHistory(paging.TPID, paging.ActivityID, paging.CaseID);
            int intCount = 0;
            var lst = svcTP.GetOutboundListByTP(paging, out intCount);
            DTResultViewModel<OutboundTabPageListViewModel> objRecord = null;

            objRecord = new DTResultViewModel<OutboundTabPageListViewModel>()
            {
                draw = paging.Draw,
                recordsTotal = intCount, //data.Count,
                recordsFiltered = intCount, //data.Count,
                data = lst
            };


            return Json(objRecord);
        }

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch, Constant.Authentication.TaxPayer.BiasaOwnBranch, Constant.Authentication.TaxPayer.VIPAllBranch, Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetAdditionalDataByTP(TaxPayerParamViewModel model)
        { 
            var data = svcTP.GetAdditionalDataByTP(model); 

            return Json(data);
        }

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch, Constant.Authentication.TaxPayer.BiasaOwnBranch, Constant.Authentication.TaxPayer.VIPAllBranch, Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetRingkasanBayaranBalikTP(TaxPayerParamViewModel model)
        {
            var data = svcTP.GetRingkasanBayaranBalikTP(model);

            return Json(data);
        }

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch, Constant.Authentication.TaxPayer.BiasaOwnBranch, Constant.Authentication.TaxPayer.VIPAllBranch, Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetLejerDataByTP(TaxPayerParamViewModel model)
        { 
            var data = svcTP.GetLejerDataByTP(model); 
            return Json(data);
        }

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch, Constant.Authentication.TaxPayer.BiasaOwnBranch, Constant.Authentication.TaxPayer.VIPAllBranch, Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetRingkasanLejerByTP(TaxPayerParamViewModel model)
        {
            var data = svcTP.GetRingkasanLejerByTP(model);
            return Json(data);
        }

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch, Constant.Authentication.TaxPayer.BiasaOwnBranch, Constant.Authentication.TaxPayer.VIPAllBranch, Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetKawalBorangDataByTP(TaxPayerParamViewModel model)
        {
            var data = svcTP.GetKawalBorangDataByTP(model);
            return Json(data);
        }

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch, Constant.Authentication.TaxPayer.BiasaOwnBranch, Constant.Authentication.TaxPayer.VIPAllBranch, Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetAllAnsuranByTP(TaxPayerParamViewModel model)
        {
            var data = svcTP.GetAllAnsuranByTP(model);
            return Json(data);
        }

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch, Constant.Authentication.TaxPayer.BiasaOwnBranch, Constant.Authentication.TaxPayer.VIPAllBranch, Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetAllAnggaranByTP(TaxPayerParamViewModel model)
        {
            var data = svcTP.GetAllAnggaranByTP(model);
            return Json(data);
        } 

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch, Constant.Authentication.TaxPayer.BiasaOwnBranch, Constant.Authentication.TaxPayer.VIPAllBranch, Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetRingkasanTaksiranByTP(TaxPayerParamViewModel model)
        {
            var data = svcTP.GetRingkasanTaksiranByTP(model);
            return Json(data);
        }
         
        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch, Constant.Authentication.TaxPayer.BiasaOwnBranch, Constant.Authentication.TaxPayer.VIPAllBranch, Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetPungutanCPbyTP(TaxPayerParamViewModel model)
        {
            var data = svcTP.GetPungutanCPbyTP(model);
            return Json(data);
        }

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch, Constant.Authentication.TaxPayer.BiasaOwnBranch, Constant.Authentication.TaxPayer.VIPAllBranch, Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetPungutanCKHTbyTP(TaxPayerParamViewModel model)
        {
            var data = svcTP.GetPungutanCKHTbyTP(model);
            return Json(data);
        }

        [HttpPost]
        [CRMAuthorize(Constant.Authentication.TaxPayer.BiasaAllBranch, Constant.Authentication.TaxPayer.BiasaOwnBranch, Constant.Authentication.TaxPayer.VIPAllBranch, Constant.Authentication.TaxPayer.VIPOwnBranch)]
        public IHttpActionResult GetStatusBayaranBalikbyTP(TaxPayerParamViewModel model)
        {
            var data = svcTP.GetStatusBayaranBalikbyTP(model);
            return Json(data);
        }

        //[HttpGet]
        //[ValidateAjaxRequest]
        //public IHttpActionResult GetDummyTPByTPID(long id)
        //{
        //    TPForActivityViewModel data = null;
        //    DateTime now = DateTime.Now;
        //    //throw new Exception("test api error");

        //    data = svcTP.GetDummyTPByTPID(id);

        //    return Json(data);
        //}
    }
}
