using NEIntelligentControl2.Service.Alarm;
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.Navigation;
using System.Windows.Shapes;

namespace NEIntelligentControl2.Views.MainWindow
{
    /// <summary>
    /// 状态栏
    /// </summary>
    public partial class StatusBar : UserControl
    {
        // --------依赖属性-----------
        public static readonly DependencyProperty IsMenuOpendProperty = DependencyProperty.Register("IsMenuOpen", typeof(bool), typeof(StatusBar));
        // ------------事件-------------
        public event RoutedEventHandler ItemClick { add { _MWMMain.ItemClick += value; } remove { _MWMMain.ItemClick -= value; } }
        /// <summary>
        /// 菜单是否弹出
        /// </summary>
        public bool IsMenuOpen { get => (bool)GetValue(IsMenuOpendProperty); set => SetValue(IsMenuOpendProperty, value); }

        private AlarmManager _AlarmManager;
        private SuggestionManager _SuggestionManager;
        public StatusBar()
        {
            InitializeComponent();
            _AlarmManager = App.ServiceProvider.GetService(typeof(AlarmManager)) as AlarmManager;
            _AlarmManager.OnFaultCount += OnFaultCount;
            _SuggestionManager = App.ServiceProvider.GetService(typeof(SuggestionManager)) as SuggestionManager;
            _SuggestionManager.OnCountChanged = OnSuggestionCount;
        }

        private void OnSuggestionCount(int obj)
        {
            Dispatcher.Invoke(() => _TBSCount.Text = obj.ToString());
        }

        private void OnFaultCount(List<Models.Alarm.FaultInfoModel> fims)
        {
            if (fims == null || fims.Count <= 0) return;
            var info = fims.FirstOrDefault();
            Dispatcher.Invoke(() =>
            {
                _TBAlarmTime.Text = info.FaultInfo.LastUpdateTime?.ToString("HH:mm:ss");
                _TBAlarmName.Text = info.FaultInfo.ObjectName;
                _TBAlarmText.Text = info.FaultInfo.AlertText;
                _TBAlarmType.Text = info.FaultInfo.MessageType == 3 ? "[解除]" : "";
            });
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            switch (((FrameworkElement)sender).Tag)
            {
                case "menu":// 主菜单
                    IsMenuOpen = !IsMenuOpen;
                    break;
                case "track":// 控制跟踪
                    Track();
                    break;
                default:return;
            }
        }

        private void Border_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            switch (((FrameworkElement)sender).Tag)
            {
                case "recomend":// 推荐
                    Recomend();
                    break;
                default: return;
            }
        }

        /// <summary>
        /// 控制跟踪
        /// </summary>
        private void Track()
        {
            Windows.ControlTrackWindow.ShowWindow();
            //WinForms.TrackForm tf = new WinForms.TrackForm();
            //tf.Show();
        }
        /// <summary>
        /// 控制推荐
        /// </summary>
        private void Recomend()
        {
            Windows.RecomendWindow.ShowWindow();
        }

        private void Popup_MouseLeave(object sender, MouseEventArgs e) => IsMenuOpen = false;

        private void Grid_MouseEnter(object sender, MouseEventArgs e)
        {
            _PAlarm.IsOpen = true;
        }

        private void gdalarm_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _RFIVMain.IsOpened = !_RFIVMain.IsOpened;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Task.Factory.StartNew(async () => {
                while (true)
                {
                    this.Dispatcher.Invoke(() => time.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    await Task.Delay(1000);
                }
            },TaskCreationOptions.LongRunning);
        }
    }
}
