using LHDN.CRM.BC.Common;
using LHDN.CRM.BC.Repositories;
using LHDN.CRM.Models.ViewModels;
using LHDN.CRM.Models.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace LHDN.CRM.BC.Services
{
    public class SearchService
    {
        private SearchRepository repo = new SearchRepository();
        private UserProfileService userSVC = new UserProfileService();
        public DropdownViewModel GetSearchDropdown(int tabletype)
        {
            DropdownViewModel ddlVM = new DropdownViewModel();
            ddlVM.JenisPengenalanOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "JenisPengenalan" && x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, Code = x.LookupValue, SeqNo = x.SeqNo }).ToList());
            ddlVM.ActStatusOptions = Utilities.SortDDL(AdminService.LookupParams.Where(x => x.LookupCode == "ActivityStatus" && x.IsActive == true).Select(x => new DropdownDetailViewModel { Value = x.LookupParamID, Text = x.ParamDesc, IsDefault = x.IsDefault, Code = x.LookupValue, SeqNo = x.SeqNo }).ToList());
            ddlVM.ViewNameOptions = repo.GetSearchViewsDDL(tabletype, userSVC.UserInfo.UserProfileID).Select(x => new DropdownDetailViewModel { Value = x.ViewID, Text = x.ViewName }).ToList();
            ddlVM.ActChannelOptions = AdminService.ActivityChannels; //TODO Order at js
            ddlVM.Status1Options = AdminService.StatusLevels.Where(x => x.IsActive == true).ToList<StatusLevel1>(); //TODO Order at js
            ddlVM.MainKategoriOptions = AdminService.Kategoris.Where(x => x.IsActive == true).ToList<MainKategori>();
            ddlVM.ParameterSettings = AdminService.ParameterSettings.Where(x => (x.ParameterID == 20 || x.ParameterID == 21 || x.ParameterID == 22)).ToList<ParameterSettings>(); //Smart-dummy profile
            return ddlVM;
        }

        public dynamic SearchTP(SearchTPPagedListViewModel paging, out int intCount)
        {
            intCount = 0;

            DataSet ds = repo.GetSearchTPResult(paging.SortExpression, paging.PageIndex, paging.Length,
                                            paging.ViewID, paging.JenisPengenalanID, paging.NoPengenalan, paging.Nama, paging.RujukanNo, 
                                            paging.IsExact, paging.IsSavedView, paging.IsAdvancedView,
                                            (int)Constant.TableType.TaxPayer, paging.Rules, paging.IsDynamicDisplayColumn, paging.DisplayColumns, paging.FieldsSorting);


            object lst = new List<SearchTPResultViewModel>();

            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]));

                    intCount = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());

                }

                //if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 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 byte[] ExportExcelSearchTP(SearchTPPagedListViewModel paging)
        {
            DataSet ds = repo.GetSearchTPResult(paging.SortExpression, paging.PageIndex, paging.Length,
                                            paging.ViewID, paging.JenisPengenalanID, paging.NoPengenalan, paging.Nama, paging.RujukanNo,
                                            paging.IsExact, paging.IsSavedView, paging.IsAdvancedView,
                                            (int)Constant.TableType.TaxPayer, paging.Rules, paging.IsDynamicDisplayColumn, paging.DisplayColumns, paging.FieldsSorting);


            DataTable dtbl = new DataTable();

            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    dtbl = ds.Tables[0];
            }

            return Utilities.ExportExcel(dtbl);
        }


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

        public dynamic SearchActBasic(SearchActPagedListViewModel paging, out int intCount)
        {
            intCount = 0;

            DataSet ds = repo.GetSearchResultAct(paging.SortExpression, paging.PageIndex, paging.Length,
                                            paging.ViewID, paging.JenisPengenalanID, paging.NoPengenalan, paging.Nama, paging.RujukanNo, paging.IsExact, paging.IsSavedView,
                                            paging.ActRefNo, paging.ActChannelID, paging.IsOprhan, paging.ActStatusID, paging.IsLinkfromDashboard, paging.CurrentLoginID,paging.IsAdvancedView,
                                            (int)Constant.TableType.TaxPayer, paging.Rules,paging.IsDynamicDisplayColumn, paging.DisplayColumns,paging.FieldsSorting);


            object lst = new List<SearchActResultViewModel>();

            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]));

                    intCount = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
                }
                    


                //if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
                //    intCount = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());
            }

            return lst;
        }

        public byte[] ExportExcelSearchAct(SearchActPagedListViewModel paging)
        {
            DataSet ds = repo.GetSearchResultAct(paging.SortExpression, paging.PageIndex, paging.Length,
                                            paging.ViewID, paging.JenisPengenalanID, paging.NoPengenalan, paging.Nama, paging.RujukanNo, paging.IsExact, paging.IsSavedView,
                                            paging.ActRefNo, paging.ActChannelID, paging.IsOprhan, paging.ActStatusID, paging.IsLinkfromDashboard, paging.CurrentLoginID, paging.IsAdvancedView,
                                            (int)Constant.TableType.TaxPayer, paging.Rules, paging.IsDynamicDisplayColumn, paging.DisplayColumns, paging.FieldsSorting);


            DataTable dtbl = new DataTable();

            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    dtbl = ds.Tables[0];
            }

            return Utilities.ExportExcel(dtbl);
        }

        //public dynamic SearchActSavedView(SearchActPagedListViewModel paging, out int intCount)
        //{
        //    intCount = 0;

        //    DataSet ds = repo.GetSearchResultAct(paging.SortExpression, paging.PageIndex, paging.Length,
        //                                      paging.ViewID, paging.JenisPengenalanID, paging.NoPengenalan, paging.Nama, paging.RujukanNo, paging.IsExact, paging.IsSavedView,
        //                                      paging.ActRefNo, paging.ActChannelID, paging.IsOprhan, paging.ActStatusID, paging.CurrentLoginID);

        //    object lst = new List<SearchActResultViewModel>();

        //    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.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 dynamic SearchCaseBasic(SearchCasePagedListViewModel paging, out int intCount)
        {
            intCount = 0;

            DataSet ds = repo.GetSearchResultCase(paging.SortExpression, paging.PageIndex, paging.Length,
                                            paging.ViewID, paging.JenisPengenalanID, paging.NoPengenalan, paging.Nama, paging.RujukanNo, paging.IsExact, paging.IsSavedView,
                                            paging.CaseRefNo, paging.Status1ID, paging.IsOprhan, paging.Kategori1ID,paging.IsLinkfromDashboard,paging.CurrentLoginID, paging.IsCaseToday,
                                            paging.IsAdvancedView,(int)Constant.TableType.TaxPayer, paging.Rules, paging.IsDynamicDisplayColumn, paging.DisplayColumns, paging.FieldsSorting);


            object lst = new List<SearchCaseResultViewModel>();

            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]));

                    intCount = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
                }
                  


        
                //if (ds.Tables[1] != null && ds.Tables[1].Rows.Count > 0)
                //    intCount = Convert.ToInt32(ds.Tables[1].Rows[0][0].ToString());
            }

            return lst;
        }

        public byte[] ExportExcelSearchCase(SearchCasePagedListViewModel paging)
        {
            DataSet ds = repo.GetSearchResultCase(paging.SortExpression, paging.PageIndex, paging.Length,
                                            paging.ViewID, paging.JenisPengenalanID, paging.NoPengenalan, paging.Nama, paging.RujukanNo, paging.IsExact, paging.IsSavedView,
                                            paging.CaseRefNo, paging.Status1ID, paging.IsOprhan, paging.Kategori1ID, paging.IsLinkfromDashboard, paging.CurrentLoginID, paging.IsCaseToday,
                                            paging.IsAdvancedView, (int)Constant.TableType.TaxPayer, paging.Rules, paging.IsDynamicDisplayColumn, paging.DisplayColumns, paging.FieldsSorting);


            DataTable dtbl = new DataTable();

            if (ds != null && ds.Tables.Count > 0)
            {
                if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    dtbl = ds.Tables[0];
            }

            return Utilities.ExportExcel(dtbl);
        }

        //public dynamic SearchCaseSavedView(SearchCasePagedListViewModel paging, out int intCount)
        //{
        //    intCount = 0;

        //    DataSet ds = repo.GetSearchResultCase(paging.SortExpression, paging.PageIndex, paging.Length,
        //                                    paging.ViewID, paging.JenisPengenalanID, paging.NoPengenalan, paging.Nama, paging.RujukanNo, paging.IsExact, paging.IsSavedView,
        //                                    paging.CaseRefNo, paging.Status1ID, paging.IsOprhan, paging.Kategori1ID,paging.CurrentLoginID,paging.IsCaseToday);

        //    List<SearchCaseResultViewModel> lst = new List<SearchCaseResultViewModel>();

        //    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.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;
        //}
    }
}
