using LHDN.CRM.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Newtonsoft.Json;

namespace LHDN.CRM.BC.Services
{
    public class GeneralService
    {
        private Repositories.GeneralRepository repo = new Repositories.GeneralRepository();
        private BC.Services.UserProfileService userSvc = new UserProfileService();

        public DashboardTilesViewModel GetDashboardTilesCount(string strCurrentUserID)
        {
            return repo.GetDashboardTilesCount(strCurrentUserID);
        }

        public MyNotificationDashboardTaskListViewModel GetMyDashboardNotifcation(string CreatedBy, MyNotificationDashboardTaskListViewModel paging, out int intCount)
        {
            MyNotificationDashboardTaskListViewModel lstNotifyDashboard = new MyNotificationDashboardTaskListViewModel();
            intCount = 0;

            if (paging.IsSeeFullList)
            {

                DataSet ds = repo.GetAlertFullDetail(CreatedBy, paging);

                List<TaskAlertViewModel> TaskAlertList = new List<TaskAlertViewModel>();

                if (ds != null && ds.Tables.Count > 0)
                {
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                        TaskAlertList = repo.DataTableToList<TaskAlertViewModel>(ds.Tables[0]).ToList();
                    if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
                        intCount = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());
                }

                lstNotifyDashboard.TaskListAlert = TaskAlertList;
                lstNotifyDashboard.TotalNotification = intCount;


            }
            else
            {
                lstNotifyDashboard = repo.GetMyDashboardNotification(CreatedBy, paging);
            }

            return lstNotifyDashboard;

        }

        public long InsertTaskAlertNotify(string strCurrentLoginId, Int32 intTableTypeId, long lngRefID, int intMessageTypeID, string strDescription, string strURL, bool blnIsRead, string strCreatedBy, string strCreatedByName,
                                   DateTime dtCreatedDateTime)
        {
            return repo.InsertTaskAlertNotify(strCurrentLoginId, intTableTypeId, lngRefID, intMessageTypeID, strDescription, strURL, blnIsRead, strCreatedBy, strCreatedByName,
                                                      dtCreatedDateTime);
        }

        public TaskAlertViewModel UpdateAlertContent(MyNotificationDashboardTaskListViewModel paging)
        {
            TaskAlertViewModel vmTaskAlertDetail = new TaskAlertViewModel();
            vmTaskAlertDetail = repo.UpdateAlertContent(paging.IsContentRead, paging.TaskAlertID);

            return vmTaskAlertDetail;
        }

        public AdvancedRulesMainViewModel GetAdvancedRulesDetails(int intViewID, int intTableType)
        {
            var data = repo.GetAdvancedRulesDetails(intViewID, intTableType);
            if (data == null)
                throw new NotFoundException("Advanced Rules not found!");
            else
                data.DisplaySetting = GetDisplaySetting(intTableType, intViewID, 0);

            return data;
        }

        public DashboardTilesStatusViewModel GetActDashboardWidget()
        {
            return repo.GetActDashboardWidget();
        }

        public TaskAlertViewModel GetTaskAlertDetail(string strCurrentUserID, long reftableID)
        {
            TaskAlertViewModel taskAlertDetail = new TaskAlertViewModel();
            taskAlertDetail = repo.GetTaskAlertDetail(strCurrentUserID, reftableID);

            return taskAlertDetail;
        }

        public DisplaySettingViewModel GetDisplaySetting(int intTableType, int intViewID, int intJenisKempenID)
        {
            DisplaySettingViewModel obj = new DisplaySettingViewModel();

            if (intViewID != 0)
            {
                obj = repo.GetDisplayColSavedView(intTableType, intViewID);
            }
            else
            {
                obj = repo.GetDisplayColSystemView(intTableType, intJenisKempenID);
            }

            return obj;
        }

        public List<DashboardDisplayColumn> GetDashboardDisplaySetting(int intTableType, int intViewID)
        {
            List<DashboardDisplayColumn> obj = new List<DashboardDisplayColumn>();

            if (intViewID != 0)
            {
                obj = repo.GetDashboardDisplayCol(intTableType, intViewID);
            }

            return obj;
        }

        public DashboardViewModel GetDashboardbyLoginUnit(int DashboardID)
        {
            DashboardViewModel obj = new DashboardViewModel();
            if (CheckDashboardPermission(DashboardID))
            {
                obj = repo.GetDashboardbyLoginUnit(DashboardID, userSvc.UserInfo.UserProfileID);
                if (obj == null || obj.DashboardID == 0)
                {
                    throw new NotFoundException("Dashboard not found!");
                }
                obj.IsAllowAccess = true;
            }
            else {
                obj.IsAllowAccess = false;
                throw new UnauthorizedAccessException();
            }
            return obj;
        }

        public List<SearchSavedViewViewModel> GetDashboardDynamicCol(SearchViewColumnViewModel vm)
        {
            return repo.GetDashboardDynamicCol(vm.ViewID, vm.TableType);
        }

        public dynamic GetDashboardTableResults(DashboardPagedListViewModel paging, out int intCount)
        {
            intCount = 0;
            
            DataSet ds = repo.GetDashboardTableResults(paging.SortExpression, paging.PageIndex, paging.Length,
                                            paging.ViewID, paging.ViewType, userSvc.UserInfo.LoginID);


            object lst = new List<DashboardPagedListViewModel>();

            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    lst = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(ds.Tables[0]));
                if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
                    intCount = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());
            }

            return lst;
        }

        public ChartList GetDashboardChartResult(DashboardPagedListViewModel paging)
        {
            ChartList obj = new ChartList();
            obj = repo.GetDashboardChartResult(paging.DashboardZonID, paging.ViewID, paging.ViewType,
                                                        paging.xFieldMappingID, paging.yFieldMappingID, userSvc.UserInfo.LoginID);
            if (obj == null)
            {
                throw new NotFoundException("Dashboard not found!");
            }
            return obj;
        }

        public bool CheckDashboardPermission(int DashboardID)
        {
            bool isAllowed = false;
            isAllowed = repo.CheckDashboardPermission(DashboardID, userSvc.UserInfo.UserProfileID);

            return isAllowed;
        }

        public List<DashboardViewModel> GetDashboardList()
        {
            return repo.GetDashboardList(userSvc.UserInfo.UserProfileID);
            
        }

        public AdvancedRulesMainViewModel DeleteViewConfig(int intViewID, int intTableType,string lastupdateBy,string lastupdateByName,DateTime now)
        {
            AdvancedRulesMainViewModel ruleModel = new AdvancedRulesMainViewModel();
            ruleModel = repo.DeleteViewConfig(intViewID, intTableType, lastupdateBy, lastupdateByName,now);

            return ruleModel;
        }

    }
}
