using NEIntelligentControl2.Service.Station;
using NEIntelligentControl2.Service.Windturbine;
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 NEIntelligentControl2.Windows
{
    /// <summary>
    /// 控制跟踪窗口
    /// </summary>
    public partial class ControlTrackWindow : Window
    {
        private ControlManager _ControlManager;
        private StationManager _StationManager;
        private bool _IsActived;
        public ControlTrackWindow(ControlManager cm,StationManager sm)
        {
            InitializeComponent();
            _ControlManager = cm;
            _StationManager = sm;
            Init();
        }

        private void Init()
        {
            _TBStart.Text = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
            _TBEnd.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            var vs = _StationManager.GetStationInfos().Where(s => s.Type == Models.Station.StationType.Wind).ToList();
            vs.Insert(0, new Models.Station.StationInfo() { Name = "未选择", Id = "" });
            _CBStation.ItemsSource = vs;
            _CBStation.SelectedIndex = 0;
        }

        public static void ShowWindow()
        {
            ControlTrackWindow ctw = App.ServiceProvider.GetService(typeof(ControlTrackWindow)) as ControlTrackWindow;
            ctw.Owner = Application.Current.MainWindow;
            ctw.ShowDialog();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _IsActived = true;
            Task.Run(RefreshControlLog);
        }

        private async void RefreshControlLog()
        {
            while (_IsActived)
            {
                try
                {
                    var v = _ControlManager.GetControlLog();
                    Dispatcher.Invoke(() => _TBLog.Text = v);
                }
                catch { }
                await Task.Delay(5000);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            switch (((Control)sender).Tag)
            {
                case "search":
                    SearchControlLog();
                    break;
                case "close":
                    this.Close();
                    break;
                default:return;
            }
        }
        /// <summary>
        /// 查询操作日志
        /// </summary>
        private void SearchControlLog()
        {
            var start = _TBStart.Text;
            var end = _TBEnd.Text;
            var station = (_CBStation.SelectedItem as Models.Station.StationInfo)?.Id;
            Task.Run(() =>
            {
                try
                {
                    DateTime.TryParse(start, out DateTime startTime);
                    DateTime.TryParse(end, out DateTime endTime);
                    string stimestring = $"{startTime.Year}/{startTime.Month}/{startTime.Day} {startTime.Hour}:{startTime.Minute}:{startTime.Second}";
                    string etimestring = $"{endTime.Year}/{endTime.Month}/{endTime.Day} {endTime.Hour}:{endTime.Minute}:{endTime.Second}";

                    var v = _ControlManager.GetControlRecord(station, stimestring, etimestring);
                    if (v == null) return;
                    foreach(var vv in v.DataList)
                    {
                        vv.StationName = _StationManager.GetStatonInfo(vv.StationId)?.FullName;
                    }
                    Dispatcher.Invoke(() => _DGControlLog.ItemsSource = v.DataList);
                }
                catch { }
            });
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            SearchControlLog();
        }

        private void _CBStation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SearchControlLog();
        }

        private void TabItem_Loaded(object sender, RoutedEventArgs e)
        {
            SearchControlLog();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _IsActived = false;
        }
    }
}
