using NEIntelligentControl2.Models;
using NEIntelligentControl2.Models.Windturbine;
using NEIntelligentControl2.Service.WebSocket;
using NEIntelligentControl2.Service.Windturbine;
using NEIntelligentControl2.Views.Basic;
using NEIntelligentControl2.Views.Infos;
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.Controls.Primitives;
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.Matrix
{
    /// <summary>
    /// 风机矩阵块（小）
    /// </summary>
    public partial class WindBlockSmall : UserControl, IOptional
    {
        // ----------依赖属性------------
        public static DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(WindBlockSmall));
        public static DependencyProperty TopValueProperty = DependencyProperty.Register("TopValue", typeof(string), typeof(WindBlockSmall));
        public static DependencyProperty MiddleValueProperty = DependencyProperty.Register("MiddleValue", typeof(string), typeof(WindBlockSmall));
        public static DependencyProperty BottomValueProperty = DependencyProperty.Register("BottomValue", typeof(string), typeof(WindBlockSmall));
        public static DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(int), typeof(WindBlockSmall));
        public static DependencyProperty ToolTipValueProperty = DependencyProperty.Register("ToolTipValue", typeof(string), typeof(WindBlockSmall));
        public static DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(WindBlockSmall));

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get => GetValue(TitleProperty) as string; set => SetValue(TitleProperty, value); }
        /// <summary>
        /// 顶部数值
        /// </summary>
        public string TopValue { get => GetValue(TopValueProperty) as string; set => SetValue(TopValueProperty, value); }
        /// <summary>
        /// 中间数值
        /// </summary>
        public string MiddleValue { get => GetValue(MiddleValueProperty) as string; set => SetValue(MiddleValueProperty, value); }
        /// <summary>
        /// 底部数值
        /// </summary>
        public string BottomValue { get => GetValue(BottomValueProperty) as string; set => SetValue(BottomValueProperty, value); }

        /// <summary>
        /// 状态
        /// </summary>
        public int State
        {
            get
            {
                var v = GetValue(StateProperty);
                return v == null ? -1 : (int)v;
            }
            set => SetValue(StateProperty, value);
        }
        /// <summary>
        /// 是否被选中
        /// </summary>
        public bool IsSelected { get => (bool)GetValue(IsSelectedProperty); set => SetValue(IsSelectedProperty, value); }
        /// <summary>
        /// 状态改变时间
        /// </summary>
        public DateTime StateChangedTime { get; set; }

        private long _Timestamp { get; set; }// 状态持续时长

        /// <summary>
        /// 提示消息
        /// </summary>
        public string ToolTipValue { get => GetValue(ToolTipValueProperty) as string; set => SetValue(ToolTipValueProperty, value); }
        /// <summary>
        /// 标签
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// 风机ID
        /// </summary>
        public string WindturbineId { get; set; }
        /// <summary>
        /// 被操作时间
        /// </summary>
        public DateTime OperatedTime { get; set; }
        // 鼠标点击事件
        public event SelectedEventHandler SelectedChanged;

        private WindturbineInfo _WindturbineInfo;   // 风机数据
        /// <summary>
        /// 风机数据
        /// </summary>
        public WindturbineInfo WindturbineInfo { get => _WindturbineInfo; }

        /// <summary>
        /// 自定义挂牌信息
        /// </summary>
        public CustomLockInfo CustomLockInfo { get; internal set; }

        private int _PopupInterval = 0;
        public WindBlockSmall(WindturbineInfo v)
        {
            InitializeComponent();
            _WindturbineInfo = v;
            _PopupInterval = App.GetService<UrlManager>().WindturbineDetailsInterval;

            InitToolTip();
        }

        private void InitToolTip()
        {
            if (_PopupInterval <= 0) return;
            ToolTip tp = new ToolTip();
            tp.Opened += ToolTipOpened;
            g.ToolTip = tp;
            ToolTipService.SetInitialShowDelay(g, _PopupInterval);

        }

        private void ToolTipOpened(object sender, RoutedEventArgs e)
        {
            ToolTip tp = sender as ToolTip;
            if (tp == null) return;
            if (tp.Content == null)
            {
                var  wdv = new WindturbineDetailsView(WindturbineInfo) { ToolTipValue =this.ToolTipValue };
                tp.Content = wdv;
            }
            (tp.Content as WindturbineDetailsView)?.RefreshData();
        }

        internal void Update(WindturbineInfo ap)
        {
            _WindturbineInfo = ap;
            TopValue = $"{ap.WindSpeed.ToString("F2")} m/s";
            MiddleValue = $"{ap.Power.ToString("F2")} kW";
            var rs = ap.ModelId.ToLower().Contains("up105") ? ap.RollSpeed * 9.55 : ap.RollSpeed;
            BottomValue = $"{rs.ToString("F2")} rpm";
            State = (int)ap.Status;

            _Timestamp = ap.Ts;
            string time = _Timestamp.GetTimeString();
            string timespan = _Timestamp.GetTimeSpanString();
            ToolTipValue = $"开始时间：{time} 时长：{timespan}分钟";
            if (this.CustomLockInfo != null)
            {
                ToolTipValue += $"\n挂牌：{CustomLockInfo.Value}  挂牌时间：{CustomLockInfo.FollowTime.ToString("yyyy-MM-dd HH:mm:ss")}";
            }
            else if (_WindturbineInfo.IsLocked)
            {
                ToolTipValue += $"\n挂牌：{((HungType)_WindturbineInfo.LockValue).GetStringValue()}";
            }

            var v = (g.ToolTip as ToolTip)?.Content as WindturbineDetailsView;
            if (v == null) return;
            v.ToolTipValue = ToolTipValue;
        }

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var b = Keyboard.Modifiers == ModifierKeys.Shift;
            if (SelectedChanged == null)
            {
                IsSelected = !IsSelected;
            }
            else
            {
                SelectedChanged(this, b);
            }

            if (e.ClickCount <= 1 || _WindturbineInfo == null) return;

            Windows.WindturbineInfoWindow.ShowWindow(this.WindturbineInfo);
            //WinForms.WindtrubineInfoWindow.ShowWindow(_WindturbineInfo);
            //ShowForm.Show(_WindturbineInfo.StationId, _WindturbineInfo.ModelId, _WindturbineInfo.WindturbineId, Title);
        }
    }
}
