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.Infos
{
    /// <summary>
    /// 测点
    /// </summary>
    public partial class StateInfoPoint : Border
    {
        public static readonly DependencyProperty TotalTimeProperty = DependencyProperty.Register("TotalTime", typeof(double), typeof(StateInfoPoint), new PropertyMetadata(ValueChanged));
        public static readonly DependencyProperty TimeProperty = DependencyProperty.Register("Time", typeof(double), typeof(StateInfoPoint), new PropertyMetadata(ValueChanged));
        public static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(string), typeof(StateInfoPoint), new PropertyMetadata(StateChanged));

        public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(StateInfoPoint), new PropertyMetadata(OnSelectionChanged));


        /// <summary>
        /// 总时间
        /// </summary>
        public double TotalTime { get => (double)GetValue(TotalTimeProperty); set => SetValue(TotalTimeProperty, value); }
        /// <summary>
        /// 时间
        /// </summary>
        public double Time { get => (double)GetValue(TimeProperty); set => SetValue(TimeProperty, Math.Round(value, 2)); }
        /// <summary>
        /// 状态
        /// </summary>
        public string State { get => (string)GetValue(StateProperty); set => SetValue(StateProperty, value); }
        /// <summary>
        /// 是否选中
        /// </summary>
        public bool IsSelected { get => (bool)GetValue(IsSelectedProperty); set => SetValue(IsSelectedProperty, value); }
        /// <summary>
        /// 总宽度
        /// </summary>
        public double TotalWidth { get; set; }
        /// <summary>
        /// 百分比
        /// </summary>
        public double Percentage { get; set; }

        /// <summary>
        /// 选择改变
        /// </summary>
        public Action<bool, double> SelectionChanged { get; set; }

        public Models.Windturbine.StatusTimeInfo Info { get; set; }

        public StateInfoPoint(Models.Windturbine.StatusTimeInfo v)
        {
            InitializeComponent();
            Info = v;
            State = v.Status;
            Time = v.Duration;
        }

        /// <summary>
        /// 数据更改
        /// </summary>
        private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == e.OldValue) return;
            StateInfoPoint si = d as StateInfoPoint;
            if (si.TotalTime == 0.0) return;
            si.Width = si.Time / si.TotalTime * si.TotalWidth;
            var p = si.Time / si.TotalTime;
            si.Percentage = p;
            si.ToolTip = $"{si.Info.StatusName}  时长:{si.Time}  占比:{p.ToString("p")}   时间:{si.Info.StartTime}至{si.Info.EndTime}";
        }

        /// <summary>
        /// 选择改变
        /// </summary>
        private static void OnSelectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            StateInfoPoint si = d as StateInfoPoint;
            bool b = (bool)e.NewValue;
            Color c = b ? Color.FromRgb(0x22, 0x7E, 0xDB) : Color.FromRgb(0xF5, 0xF5, 0xF5);
            si.BorderBrush = new SolidColorBrush(c);

            si?.SelectionChanged?.Invoke(b, si.Time);
        }

        /// <summary>
        /// 状态改变
        /// </summary>
        private static void StateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            StateInfoPoint si = d as StateInfoPoint;
            switch (e.NewValue)
            {
                case "Stop":
                    si.Background = new SolidColorBrush(Color.FromRgb(0xB0, 0x97, 0x3F));
                    break;
                case "OnPower":
                    si.Background = new SolidColorBrush(Color.FromRgb(0xCF, 0xF1, 0xE8));
                    break;
                case "Standby":
                    si.Background = new SolidColorBrush(Color.FromRgb(0xCF, 0xF1, 0xE8));
                    break;
                case "Start":
                    si.Background = new SolidColorBrush(Color.FromRgb(0xCF, 0xF1, 0xE8));
                    break;
                case "Online":
                    si.Background = new SolidColorBrush(Color.FromRgb(0xC8, 0xEA, 0xFF));
                    break;
                case "Fault":
                    si.Background = new SolidColorBrush(Color.FromRgb(0xDB, 0x33, 0x33));
                    break;
                case "Maintain":
                    si.Background = new SolidColorBrush(Color.FromRgb(0xFF, 0x72, 0x0E));
                    break;
                default:
                    si.Background = new SolidColorBrush(Color.FromRgb(0xC7, 0xD3, 0xDD));
                    break;
            }
        }

        private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.IsSelected = !this.IsSelected;
        }
    }
}
