using LHDN.CRM.BC.Services;
using LHDN.CRM.Models.ViewModels;
using LHDN.CRM.WebAPI.Filters;
using System.Collections.Generic;
using System.Web.Http;
using LHDN.CRM.BC.Common;
using System;
using Newtonsoft.Json;
using System.Configuration;
using System.Web.Http.Results;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net;

namespace LHDN.CRM.WebAPI.Controllers.Api
{
    public class SearchApiController : BaseApiController
    {
        private SearchService svc = new BC.Services.SearchService();
        private UserProfileService user = new UserProfileService();

        [HttpGet]
        [ApiErrorHandler]
        public IHttpActionResult GetSearchDropdown(int id)
        {
            return Json(svc.GetSearchDropdown(id));
        }

        [HttpPost]
        [ApiErrorHandler]
        [ValidateAjaxRequest]
        [CRMAuthorize(Constant.Authentication.TaxPayer.SearchTPProfile)]
        public IHttpActionResult GetPagedSearchTP(SearchTPPagedListViewModel paging)
        {
            int intCount = 0;

            //if (paging.IsSavedView)
            //{
                var lst = svc.SearchTP(paging, out intCount);
                var objRecord = new
                {
                    draw = paging.Draw,
                    recordsTotal = intCount, //data.Count,
                    recordsFiltered = intCount, //data.Count,
                    data = lst,
                };

            string strAdditionalData = "Search Filters - TP Name : {0}, IDNo : {1}, No Rujukan : {2}";
            AuditLogTxn(AuditService.AuditEventID.SearchTaxPayer,
                AuditService.ResultType.Success,
                AuditService.ActionType.View,
                user.LoginID,
                DateTime.Now,
                string.Format(strAdditionalData, paging.Nama.ToStringDash(), paging.NoPengenalan.ToStringDash(), paging.RujukanNo.ToStringDash()));

            return Json(objRecord);
        }

        
        [HttpPost]
        public IHttpActionResult ExportExcelSearchTP(ExportToExcelViewModel vm)
        {
            SearchTPPagedListViewModel paging = new SearchTPPagedListViewModel();
            paging = JsonConvert.DeserializeObject<SearchTPPagedListViewModel>(vm.strJSON);
            paging.Length = Convert.ToInt32(AdminService.ParameterSettings.Where(x => x.ParameterID == 22).FirstOrDefault().ParameterValue);

            byte[] bytFileContent = null;
            bytFileContent = svc.ExportExcelSearchTP(paging);

            ResponseMessageResult responseMessageResult = null;
            HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(bytFileContent)
            };

            httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "CarianPC_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"
            };

            httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

            responseMessageResult = ResponseMessage(httpResponseMessage);

            return responseMessageResult;
        }


        [HttpPost]
        [ApiErrorHandler]
        [ValidateAjaxRequest]
        public IHttpActionResult GetSavedViewColumns(SearchViewColumnViewModel vm)
        {
            return Json(svc.GetSavedViewColumns(vm));
        }
        
        [HttpPost]
        [ApiErrorHandler]
        [ValidateAjaxRequest]
        public IHttpActionResult GetPagedSearchAct(SearchActPagedListViewModel paging)
        {
            int intCount = 0;
            //if (paging.IsLinkfromDashboard)
            //{
                paging.CurrentLoginID = user.LoginID;
            //}
            var lst = svc.SearchActBasic(paging, out intCount);
            var objRecord = new
            {
                draw = paging.Draw,
                recordsTotal = intCount, //data.Count,
                recordsFiltered = intCount, //data.Count,
                data = lst,
            };

            return Json(objRecord);
            
        }

        [HttpPost]
        public IHttpActionResult ExportExcelSearchAct(ExportToExcelViewModel vm)
        {
            SearchActPagedListViewModel paging = new SearchActPagedListViewModel();
            paging = JsonConvert.DeserializeObject<SearchActPagedListViewModel>(vm.strJSON);
            paging.Length = Convert.ToInt32(AdminService.ParameterSettings.Where(x => x.ParameterID == 22).FirstOrDefault().ParameterValue);
            paging.CurrentLoginID = user.LoginID;

            byte[] bytFileContent = null;
            bytFileContent = svc.ExportExcelSearchAct(paging);

            ResponseMessageResult responseMessageResult = null;
            HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(bytFileContent)
            };

            httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "CarianAktiviti_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"
            };

            httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

            responseMessageResult = ResponseMessage(httpResponseMessage);

            return responseMessageResult;
        }

        [HttpPost]
        [ApiErrorHandler]
        [ValidateAjaxRequest]
        public IHttpActionResult GetPagedSearchCase(SearchCasePagedListViewModel paging)
        {
            int intCount = 0;
            //if (paging.IsLinkfromDashboard)
            //{
                paging.CurrentLoginID = user.LoginID;
            //}
            var lst = svc.SearchCaseBasic(paging, out intCount);
            var objRecord = new
            {
                draw = paging.Draw,
                recordsTotal = intCount, //data.Count,
                recordsFiltered = intCount, //data.Count,
                data = lst,
            };

            return Json(objRecord);
            
            
        }

        [HttpPost]
        public IHttpActionResult ExportExcelSearchCase(ExportToExcelViewModel vm)
        {
            SearchCasePagedListViewModel paging = new SearchCasePagedListViewModel();
            paging = JsonConvert.DeserializeObject<SearchCasePagedListViewModel>(vm.strJSON);
            paging.Length = Convert.ToInt32(AdminService.ParameterSettings.Where(x => x.ParameterID == 22).FirstOrDefault().ParameterValue);

            paging.CurrentLoginID = user.LoginID;
            byte[] bytFileContent = null;
            bytFileContent = svc.ExportExcelSearchCase(paging);

            ResponseMessageResult responseMessageResult = null;
            HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new ByteArrayContent(bytFileContent)
            };

            httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = "CarianKes_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"
            };

            httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

            responseMessageResult = ResponseMessage(httpResponseMessage);

            return responseMessageResult;
        }

    }
}