using BPA.SingleDevice.Services;
using SqlSugar.Extensions;

namespace BPA.SingleDevice.ViewModel
{
    public class UserLogViewModel : NotifyBase
    {
        private readonly ILogService logService;
        private ObservableCollection<UserLogTB> logs;

        public UserLogViewModel(ILogService logService)
        {
            this.logService = logService;

            Logs = logService.UserLogs;

            StartTime = DateTime.Now.AddDays(-1);
            EndTime = DateTime.Now;

            SwitchLogModeCommand = new(() =>
            {
                if (!IsShowHistory)
                {
                    Logs = logService.UserLogs;
                }
            });
            GetHistoryCommand = new(async () =>
            {
                Logs = null;
                //查询历史日志
                var logs = await logService.GetAllLog<UserLogTB>();
                Logs = new(logs?.Where(log => log.Date.ObjToDate() >= StartTime && log.Date.ObjToDate() <= EndTime));
            });
        }

        public bool IsShowHistory { get; set; }
        public ObservableCollection<UserLogTB> Logs
        { get => logs; set { logs = value; OnPropertyChanged(); } }

        /// <summary>开始时间</summary>
        public DateTime StartTime { get; set; }

        /// <summary>结束时间</summary>
        public DateTime EndTime { get; set; }

        /// <summary>切换日志类型</summary>
        public BPARelayCommand SwitchLogModeCommand { get; set; }

        /// <summary>查看历史日志。</summary>
        public BPARelayCommand GetHistoryCommand { get; set; }
    }
}