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

namespace LHDN.CRM.WebAPI.Controllers.Api
{
    [ApiErrorHandler]
    public class GeneralApiController : BaseApiController
    {
        private GeneralService svc = new BC.Services.GeneralService();
        private UserProfileService userSvc = new BC.Services.UserProfileService();
        
        public IHttpActionResult GetDashboardTiles()
        {
            var data = svc.GetDashboardTilesCount(userSvc.LoginID);

            return Json(data);
        }

        [HttpPost]
        public IHttpActionResult GetMyDashboardNotifcation(MyNotificationDashboardTaskListViewModel paging)   ///MyNotificationDashboardTaskListViewModel paging
        {
            string strCreatedBy = userSvc.LoginID;
            MyNotificationDashboardTaskListViewModel data = null;

            int intCount = 0;
            data = svc.GetMyDashboardNotifcation(strCreatedBy, paging, out intCount);
            if (paging.IsSeeFullList)
            {
                DTResultViewModel<TaskAlertViewModel> objRecord = null;

                objRecord = new DTResultViewModel<TaskAlertViewModel>()
                {
                    draw = paging.Draw,
                    recordsTotal = intCount,
                    recordsFiltered = intCount,
                    data = data.TaskListAlert
                };

                return Json(objRecord);
            }
            else
            {
                return Json(data);
            }
            
         }

        [HttpPost]
        public IHttpActionResult UpdateAlertContent(MyNotificationDashboardTaskListViewModel paging)
        {          
            var data = svc.UpdateAlertContent(paging);

            return Json(data);
        }

        [HttpGet]
        public IHttpActionResult GetAdvancedRulesDetails(int intViewID, int intTableType)
        {
            var data = svc.GetAdvancedRulesDetails(intViewID, intTableType);

            if (data == null)
                throw new NotFoundException ("Advanced Rules not found!");

            DateTime dtNow = DateTime.Now;

            return Json(data);
        }

        public IHttpActionResult GetActDashboardWidget()
        {
            var data = svc.GetActDashboardWidget();

            return Json(data);
        }

        [HttpGet]
        public IHttpActionResult GetDisplaySetting(int intTableType, int intViewID, int intJenisKempenID)
        {
            var data = svc.GetDisplaySetting(intTableType, intViewID, intJenisKempenID);

            return Json(data);
        }

        [HttpGet]
        public IHttpActionResult GetDashboardDisplaySetting(int intTableType, int intViewID)
        {
            var data = svc.GetDashboardDisplaySetting(intTableType, intViewID);

            return Json(data);
        }

        [HttpPost]
        public IHttpActionResult UpdateUserCache(List<string> lstCache)
        {

            
            foreach(string strCache in lstCache)
            {
                //LogWriter.LogMessage("Update Server cache :"+ strCache);
                var data = userSvc.UpdateUserCacheByLoginID(strCache);
            }

            return Ok();
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult GetDashboardDynamicCol(SearchViewColumnViewModel vm)
        {
            return Json(svc.GetDashboardDynamicCol(vm));
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult GetDashboardTableResults(DashboardPagedListViewModel paging)
        {
            int intCount = 0;
            var lst = svc.GetDashboardTableResults(paging, out intCount);
            var objRecord = new
            {
                draw = paging.Draw,
                recordsTotal = intCount, //data.Count,
                recordsFiltered = intCount, //data.Count,
                data = lst,
            };

            return Json(objRecord);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult GetDashboardChartResults(DashboardPagedListViewModel paging)
        {
            int intCount = 0;
            var lst = svc.GetDashboardChartResult(paging);
           

            return Json(lst);
        }

        [HttpPost]
        [ApiErrorHandler]
        public IHttpActionResult DeleteViewConfig(int intViewID,int intTableType)
        {
            string strLastUpdatedBy =  userSvc.LoginID;
            string strLastUpdatedName = userSvc.DisplayName;
            DateTime dtNow = DateTime.Now;

            var lst = svc.DeleteViewConfig(intViewID, intTableType, strLastUpdatedBy, strLastUpdatedName, dtNow);

            return Json(lst);
        }
    }
}