using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;
using Khd.Core.Library.Mapper;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Khd.Core.Wpf.WindowPage
{
    /// <summary>
    /// HoistWarnInfoWindow.xaml 的交互逻辑
    /// </summary>
    public partial class HoistWarnInfoWindow : Window
    {
        private IHost _host;
        public HoistWarnInfoWindow(IHost host)
        {
            _host = host;
            InitializeComponent();
            Task.Run(async () =>
            {
                await InitList();
            });
        }

        /// <summary>
        /// 显示序号事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgData_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = e.Row.GetIndex() + 1;
        }


        /// <summary>
        /// 初始加载500条数据
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        private async Task InitList()
        {
            try
            {
                using var scope = _host.Services.CreateScope();
                using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
                List<DmsRecordAlarmInfo> list = await dbContext.DmsRecordAlarmInfo.Where(x => x.DeviceId == 6 ).OrderByDescending(x => x.CreateTime).Take(500).ToListAsync();
                list.ForEach(x =>
                {
                    if (x.AlarmStatus == "0")
                    {
                        x.AlarmStatus = "未处理";
                    }
                    else
                    {
                        x.AlarmStatus = "已恢复";
                    }
                });
                Dispatcher.Invoke(() =>
                {
                    this.LoadMaterial0.ItemsSource = null;
                    this.LoadMaterial0.ItemsSource = list;
                    this.LoadMaterial0.Items.Refresh();
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show($"查询报警出现异常,请重新尝试:{ex.Message}");
            }
        }



        private async Task GetHistory(DateTime startTime, DateTime endTime)
        {
            try
            {
                using var scope = _host.Services.CreateScope();
                using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
               List< DmsRecordAlarmInfo > list  = await dbContext.DmsRecordAlarmInfo.Where(x => x.DeviceId ==6  &&  x.CreateTime>= startTime && x.CreateTime <= endTime).OrderByDescending(x => x.CreateTime).ToListAsync();
                list.ForEach(x =>
                {
                    if (x.AlarmStatus == "0")
                    {
                        x.AlarmStatus = "未处理";
                    }
                    else
                    {
                        x.AlarmStatus = "已恢复";
                    }
                });
                Dispatcher.Invoke(() =>
                {
                    this.LoadMaterial0.ItemsSource = null;
                    this.LoadMaterial0.ItemsSource = list;
                    this.LoadMaterial0.Items.Refresh();
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show($"查询报警出现异常,请重新尝试:{ex.Message}");
            }
        }

        private async void SelectByTime_Click(object sender, RoutedEventArgs e)
        {
            DateTime? begin = this.BeginTime.SelectedDate;
            DateTime? end = this.EndTime.SelectedDate;
            if (begin == null || end == null)
            {
                MessageBox.Show($"请选择开始或者结束时间");
                return;
            }
            await GetHistory(begin.Value, end.Value);

        }

    }
}
