using BOB.Singleton;
using Common.PubEvent;
using Logger;
using Model.SQLModel;
using Prism.Commands;
using Prism.Mvvm;
using Service.Implement;
using Service.Interface;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;

namespace BOB.ViewModels
{
    public class DataViewModel : BindableBase, INavigationAware
    {
        #region 属性

        private int _currentPage = 1;
        public int CurrentPage
        {
            get => _currentPage;
            set => SetProperty(ref _currentPage, value);
        }

        private RefAsync<int> _totalPages=new RefAsync<int>(0);
        public RefAsync<int> TotalPages
        {
            get => _totalPages;
            set => SetProperty(ref _totalPages, value);
        }
        private int _UITotalPages;
        public int UITotalPages
        {
            get => _UITotalPages;
            set => SetProperty(ref _UITotalPages, value);
        }

        private List<string> _dataTableList;
        public List<string> DataTableList
        {
            get => _dataTableList;
            set => SetProperty(ref _dataTableList, value);
        }
        private string _SelectTable;
        public string SelectTable
        {
            get => _SelectTable;
            set
            {
                if (SetProperty(ref _SelectTable, value))
                {

                }
            }
        }
        private List<BackFeedData> _BackFeedDataList;
        public List<BackFeedData> BackFeedDataList
        {
            get => _BackFeedDataList;
            set => SetProperty(ref _BackFeedDataList, value);
        }
        private List<EAELD9080Data> _EAELD9080DataList;
        public List<EAELD9080Data> EAELD9080DataList
        {
            get => _EAELD9080DataList;
            set => SetProperty(ref _EAELD9080DataList, value);
        }
        private List<PSB1100Data> _PSB1100DataList;
        public List<PSB1100Data> PSB1100DataList
        {
            get => _PSB1100DataList;
            set => SetProperty(ref _PSB1100DataList, value);
        }
        private List<IT6724CData> _IT6724CDataList;
        public List<IT6724CData> IT6724CDataList
        {
            get => _IT6724CDataList;
            set => SetProperty(ref _IT6724CDataList, value);
        }
        private DateTime? _startDate;
        public DateTime? StartDate
        {
            get => _startDate;
            set => SetProperty(ref _startDate, value);
        }

        private DateTime? _endDate;
        public DateTime? EndDate
        {
            get => _endDate;
            set => SetProperty(ref _endDate, value);
        }
        private ObservableCollection<object> _currentDataSource;
        public ObservableCollection<object> CurrentDataSource
        {
            get => _currentDataSource;
            set => SetProperty(ref _currentDataSource, value);
        }

        #endregion

        #region 命令

        public ICommand FirstPageCommand { get; private set; }
        public ICommand PreviousPageCommand { get; private set; }
        public ICommand NextPageCommand { get; private set; }
        public ICommand LastPageCommand { get; private set; }
        public ICommand LoadCommand { get; private set; }
        public ICommand QueryCommand { get; private set; }
        public ICommand RefreshCommand { get; private set; }
        public ICommand SelectTableCommand { get; private set; }

        #endregion

        private IEventAggregator _eventAggregator;
        private IDialogService _dialogService;
        private Devices _devices { get; set; }
        private IBaseService<BackFeedData> _backFeedService { get; set; }
        private IBaseService<EAELD9080Data> _eAELD9080Service { get; set; }
        private IBaseService<PSB1100Data> _pSB1100Service { get; set; }
        private IBaseService<IT6724CData> _iT6724CService { get; set; }

        public DataViewModel(IContainerProvider containerProvider)
        {
            _eventAggregator = containerProvider.Resolve<IEventAggregator>();
            _dialogService = containerProvider.Resolve<IDialogService>();
            _devices = containerProvider.Resolve<Devices>();
            _backFeedService= containerProvider.Resolve<BackFeedService>();
            _eAELD9080Service = containerProvider.Resolve<EAEL9080Service>();
            _pSB1100Service = containerProvider.Resolve<PSB11000Service>();
            _iT6724CService = containerProvider.Resolve<IT6724CService>();
            FirstPageCommand = new AsyncDelegateCommand(GoToFirstPage);
            PreviousPageCommand = new AsyncDelegateCommand(GoToPreviousPage);
            NextPageCommand = new AsyncDelegateCommand(GoToNextPage);
            LastPageCommand = new AsyncDelegateCommand(GoToLastPage);
            LoadCommand = new AsyncDelegateCommand(Load);
            QueryCommand = new AsyncDelegateCommand(Query);
            RefreshCommand = new AsyncDelegateCommand(Refresh);
            SelectTableCommand = new AsyncDelegateCommand(OnSelectTable);
            DataTableList = new List<string> { "BackFeedData", "EAELD9080Data", "IT6724CData", "PSB1100Data"};
        }

        private async Task OnSelectTable()
        {
            switch (SelectTable)
            {
                case "BackFeedData":
                    CurrentDataSource = new ObservableCollection<object>(BackFeedDataList);
                    break;
                case "EAELD9080Data":
                    CurrentDataSource = new ObservableCollection<object>(EAELD9080DataList);
                    break;
                case "PSB1100Data":
                    CurrentDataSource = new ObservableCollection<object>(PSB1100DataList);
                    break;
                case "IT6724CData":
                    CurrentDataSource = new ObservableCollection<object>(IT6724CDataList);
                    break;
                default:
                    CurrentDataSource = new ObservableCollection<object>();
                    break;
            }
            GoToFirstPage();
        }
        private DateTime? QueryStartTime;
        private DateTime? QueryEndTime;
        private bool DataQuery=false;
        private async Task Refresh()
        {
            DataQuery = false;
            QueryStartTime = null;
            QueryEndTime = null;
            await GoToFirstPage();
        }

        private async Task Query()
        {
            if (!StartDate.HasValue || !EndDate.HasValue)
            {
                return;
            }
            DateTime endDateWithTime = EndDate.Value.AddDays(1);
            if (StartDate.Value < endDateWithTime)
            {
                QueryStartTime = StartDate;
                QueryEndTime = endDateWithTime;
                DataQuery = true;
                await GoToFirstPage();
            }
            else
            {
                return;
            }
        }


        private async Task Load()
        {
            var backFeedResult = await _backFeedService.GetAllAsync();
            if (backFeedResult.IsSuccess)
            {
                BackFeedDataList = backFeedResult.Data;
            }
            else
            {
                LoggerHelper.ErrorWithNotify("加载 BackFeed 数据失败");
            }

            var eaELD9080Result = await _eAELD9080Service.GetAllAsync();
            if (eaELD9080Result.IsSuccess)
            {
                EAELD9080DataList = eaELD9080Result.Data;
            }
            else
            {
                LoggerHelper.ErrorWithNotify("加载 EAELD9080 数据失败");
            }

            var pSB1100Result = await _pSB1100Service.GetAllAsync();
            if (pSB1100Result.IsSuccess)
            {
                PSB1100DataList = pSB1100Result.Data;
            }
            else
            {
                LoggerHelper.ErrorWithNotify("加载 PSB1100 数据失败");
            }

            var it6724CResult = await _iT6724CService.GetAllAsync();
            if (it6724CResult.IsSuccess)
            {
                IT6724CDataList = it6724CResult.Data;
            }
            else
            {
                LoggerHelper.ErrorWithNotify("加载 IT6724C 数据失败");
            }
        }
        private async Task GoToFirstPage()
        {
            CurrentPage = 1;
            await LoadPageData(CurrentPage);
        }

        private async Task GoToPreviousPage()
        {
            if (CurrentPage > 1)
            {
                CurrentPage--;
                await LoadPageData(CurrentPage);
            }
        }

        private async Task GoToNextPage()
        {
            if (CurrentPage < TotalPages)
            {
                CurrentPage++;
                await LoadPageData(CurrentPage);
            }
        }

        private async Task GoToLastPage()
        {
            CurrentPage = TotalPages;
            await LoadPageData(CurrentPage);
        }

        private int  PageSize=20;
        private async Task LoadPageData(int currentPage)
        {
            switch (SelectTable)
            {
                case "BackFeedData":
                    if (DataQuery) // 如果启用日期查询
                    {
                        var result = await _backFeedService.GetPagedAsync(currentPage, PageSize, TotalPages, QueryStartTime, QueryEndTime);
                        if (result.IsSuccess)
                            CurrentDataSource = new ObservableCollection<object>(result.Data);
                    }
                    else
                    {
                        var result = await _backFeedService.GetPagedAsync(currentPage, PageSize, TotalPages);
                        if (result.IsSuccess)
                            CurrentDataSource = new ObservableCollection<object>(result.Data);
                    }
                    break;

                case "EAELD9080Data":
                    if (DataQuery)
                    {
                        var result = await _eAELD9080Service.GetPagedAsync(currentPage, PageSize, TotalPages, QueryStartTime, QueryEndTime);
                        if (result.IsSuccess)
                            CurrentDataSource = new ObservableCollection<object>(result.Data);
                    }
                    else
                    {
                        var result = await _eAELD9080Service.GetPagedAsync(currentPage, PageSize, TotalPages);
                        if (result.IsSuccess)
                            CurrentDataSource = new ObservableCollection<object>(result.Data);
                    }
                    break;

                case "PSB1100Data":
                    if (DataQuery)
                    {
                        var result = await _pSB1100Service.GetPagedAsync(currentPage, PageSize, TotalPages, QueryStartTime, QueryEndTime);
                        if (result.IsSuccess)
                            CurrentDataSource = new ObservableCollection<object>(result.Data);
                    }
                    else
                    {
                        var result = await _pSB1100Service.GetPagedAsync(currentPage, PageSize, TotalPages);
                        if (result.IsSuccess)
                            CurrentDataSource = new ObservableCollection<object>(result.Data);
                    }
                    break;

                case "IT6724CData":
                    if (DataQuery)
                    {
                        var result = await _iT6724CService.GetPagedAsync(currentPage, PageSize, TotalPages, QueryStartTime, QueryEndTime);
                        if (result.IsSuccess)
                            CurrentDataSource = new ObservableCollection<object>(result.Data);
                    }
                    else
                    {
                        var result = await _iT6724CService.GetPagedAsync(currentPage, PageSize, TotalPages);
                        if (result.IsSuccess)
                            CurrentDataSource = new ObservableCollection<object>(result.Data);
                    }
                    break;

                default:
                    CurrentDataSource = new ObservableCollection<object>();
                    break;
            }

            UITotalPages = TotalPages.Value;
        }

        #region 导航

        public void OnNavigatedTo(NavigationContext navigationContext) { }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext) { }

        #endregion
    }
}
