using HandyControl.Data;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using StartServerWPF.Models;
using StartServerWPF.Modules.Main.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;


namespace StartServerWPF.Modules.Main.ViewModels
{

    public class LogManagementViewModel : BindableBase
    {
        IEventAggregator _ea;
        private readonly SystemConfigModel _systemConfig;
        private readonly IDialogService _dialogService;
        private readonly MainViewModel _mainViewModel;

        public LogManagementViewModel(SystemConfigModel systemConfig, IEventAggregator ea, IDialogService dialogService, MainViewModel mainViewModel)

        {
            this._systemConfig = systemConfig;
            this._dialogService = dialogService;
            this._mainViewModel = mainViewModel;
        }

        private string _title = "日志查看";
        public string Title => _title;
        public ICommand LoadedCommand => new DelegateCommand(()=>
        {
            InitInfo();
        });

        public ICommand UploadCommand { get; set; }

        public DelegateCommand QueryDataCommand => new(QueryData);
        public DelegateCommand ResetQueryDataCommand => new(InitInfo);
        private void InitInfo()
        {
            StartTime = DateTime.Today;
            EndTime = DateTime.Now;
            QueryData();
        }

        public  void Refresh()
        {
            QueryData();
        }


        public void QueryData()
        {
            SelectIndexLogType = "全部";
            DataList = new List<LogMessageModel>();
            string fileName = _systemConfig.vpnInfo.SystemLogPath+ "SerLog_" + EndTime.ToString("yyyyMMdd") + ".txt";
            if (!File.Exists(fileName))
                return;
            Task.Run(() =>
            {
                _queryDataList = new List<LogMessageModel>();
                var lines=  File.ReadAllLines(fileName);
                foreach (var item in lines)
                {
                   var str=  item.Trim().Split(" ");

                    _queryDataList.Add(new LogMessageModel()
                    {
                        OriginTime = Convert.ToDateTime(str[0]),
                        LogType = str[1],
                        AppName = str[2],
                        State = str[3]
                    });
                }
                _totalDataList = _queryDataList;
                int index = _queryDataList.Count;
                MaxCountPage = (index % countPerPage == 0 ? index / countPerPage : index / countPerPage + 1);
                DataList = _totalDataList.Take(countPerPage).ToList();
            });
        }

        

        private string _SelectIndexLogType;

        public string SelectIndexLogType
        {
            get => _SelectIndexLogType;
            set
            {
                if (_queryDataList != null)
                {
                    _totalDataList = _queryDataList.Where(l => l.LogType == value).ToList();
                    int index = _totalDataList.Count;
                    MaxCountPage = (index % countPerPage == 0 ? index / countPerPage : index / countPerPage + 1);
                    DataList = _totalDataList.Take(countPerPage).ToList();
                }
                SetProperty(ref _SelectIndexLogType, value);
            }
            
        }

        private DateTime _StartTime;

        public DateTime StartTime
        {
            get => _StartTime;
            set => SetProperty(ref _StartTime, value);
        }
        private DateTime _EndTime;

        public DateTime EndTime
        {
            get => _EndTime;
            set => SetProperty(ref _EndTime, value);
        }
        private List<LogMessageModel> _DataList;

        public List<LogMessageModel> DataList
        {
            get { return _DataList; }
            set { SetProperty(ref _DataList, value); }
        }
        ///// <summary>
        /////     所有数据
        ///// </summary>
        private List<LogMessageModel> _totalDataList;

        private List<LogMessageModel> _queryDataList;
        /// <summary>
        ///     页码
        /// </summary>
        private int _pageIndex = 1;

        /// <summary>
        ///     页码
        /// </summary>
        public int PageIndex
        {
            get => _pageIndex;
            set => SetProperty(ref _pageIndex, value);

        }

        /// <summary>
        ///     页码
        /// </summary>
        private int _MaxCountPage = 15;

        /// <summary>
        ///     页码
        /// </summary>
        public int MaxCountPage
        {
            get => _MaxCountPage;
            set => SetProperty(ref _MaxCountPage, value);
        }

        private int countPerPage = 15;
        /// <summary>
        ///     页码改变命令
        /// </summary>
        public DelegateCommand<FunctionEventArgs<int>> PageUpdatedCmd => new(PageUpdated);

        /// <summary>
        ///     页码改变
        /// </summary>
        private void PageUpdated(FunctionEventArgs<int> info)
        {
            DataList = _totalDataList.Skip((info.Info - 1) * countPerPage).Take(countPerPage).ToList();
        }

    }
}
