using HandyControl.Data;
using Microsoft.Win32;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
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 IDialogService _dialogService;

        public LogManagementViewModel(
            IRegionManager regionManager,
            IEventAggregator ea, IDialogService dialogService)
        
        {
            this._dialogService = dialogService;
            InitInfo();
        }

        private string _title = "日志查看";
        public string Title => _title;

        public ICommand UploadCommand { get; set; }

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

        public  void Refresh()
        {
            QueryData();
        }


        public void QueryData()
        {
            
            Task.Run(() =>
            {
                //this.ShowLoading();
                //var logsDTO = _systemLogBLL.Query(StartTime, EndTime);
                //int index = logsDTO.Count;
                //MaxCountPage = (index % countPerPage == 0 ? index / countPerPage : index / countPerPage + 1);
                //_totalDataList = logsDTO;
                //DataList = _totalDataList.Take(countPerPage).ToList();
                //this.HideLoading();
            });
        }
        private void HFWarning()
        {

            DialogParameters param = new DialogParameters();
            param.Add("type", 0);
           // param.Add("menu", currentMenu);

            ShowEditDialog(param);
        }
        private void ShowEditDialog(DialogParameters param)
        {
            _dialogService.ShowDialog(
                "HFLogDialog",
                param,
                new Action<IDialogResult>(result =>
                {
                    if (result != null && result.Result == ButtonResult.OK)
                    {
                        System.Windows.MessageBox.Show("数据已保存", "提示");
                        Refresh();
                    }
                }));
        }
        public  void WriteTxt(FileStream fs)
        {
            try
            {
             //   foreach (var item in _totalDataList)
                {
               //     string mesg = item.CreateTime + "\t" + item.UserName + "\t" + item.UserId + "\t" + item.Message;
                    //获得字节数组
                 //   mesg = mesg + "\n";
              //      byte[] data = System.Text.Encoding.Default.GetBytes(mesg);
                    //开始写入
             //       fs.Write(data, 0, data.Length);
                }          
                //清空缓冲区、关闭流
                fs.Flush();
                fs.Close();
            }
            catch
            {
                Console.WriteLine("WriteError");
            }
        }

        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<SystemLogDTO> _DataList;

        //public List<SystemLogDTO> DataList
        //{
        //    get { return _DataList; }
        //    set { SetProperty(ref _DataList, value); }
        //}
        ///// <summary>
        /////     所有数据
        ///// </summary>
        //private List<SystemLogDTO> _totalDataList;
        /// <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();
        }

    }
}
