using NEIntelligentControl2.Models.Alarm;
using NEIntelligentControl2.Models.Messages;
using NEIntelligentControl2.Models.Windturbine;
using System;
using System.Collections.Generic;
using System.Configuration;
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.Navigation;
using System.Windows.Shapes;

namespace NEIntelligentControl2.Views.Alarm
{
    /// <summary>
    /// 风机实时报警
    /// </summary>
    public partial class WindturbineRealTimeAlarm : UserControl
    {
        private string _Url;
        private WEBHelper _WEBHelper;

        public WindturbineInfo WindturbineInfo { get; set; }
        public WindturbineRealTimeAlarm()
        {
            InitializeComponent();
            _WEBHelper = App.ServiceProvider.GetService(typeof(WEBHelper)) as WEBHelper;

            try
            {
#if (DEBUG)
                _Url = ConfigurationManager.AppSettings["AlarmPathDebug"];
#else
                _Url = ConfigurationManager.AppSettings["AlarmPath"];
#endif
            }
            catch (Exception ex)
            {
                Console.WriteLine("读取配置文件[ServicePath]出错!", ex);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (_DGMain.ItemsSource != null) return;
            Task.Run(() =>
            {
                try
                {
                    _IBMain.Inof("查询数据...");
                    var vs = _WEBHelper.HttpGetJSON<List<AlarmInfo>>($"{_Url}/alarm/snap?stationid={WindturbineInfo.StationId}&category1=windturbine&windturbineid={WindturbineInfo.WindturbineId}&isopened=1");
                    DateTime dt = DateTime.Now;
                    var ls = vs.Where(x => (dt - x.LastUpdateTime.Value).TotalDays <= 3).ToList();
                    Dispatcher.Invoke(() =>
                    {
                        _DGMain.ItemsSource = ls;
                    });

                    _IBMain.Success();
                }
                catch (Exception ex)
                {
                    _IBMain.Warning($"查询数据出现错误:{ex.Message}");
                    return;
                }
            });
        }
    }
}