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
{
///
/// 测点
///
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));
///
/// 总时间
///
public double TotalTime { get => (double)GetValue(TotalTimeProperty); set => SetValue(TotalTimeProperty, value); }
///
/// 时间
///
public double Time { get => (double)GetValue(TimeProperty); set => SetValue(TimeProperty, Math.Round(value, 2)); }
///
/// 状态
///
public string State { get => (string)GetValue(StateProperty); set => SetValue(StateProperty, value); }
///
/// 是否选中
///
public bool IsSelected { get => (bool)GetValue(IsSelectedProperty); set => SetValue(IsSelectedProperty, value); }
///
/// 总宽度
///
public double TotalWidth { get; set; }
///
/// 百分比
///
public double Percentage { get; set; }
///
/// 选择改变
///
public Action 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;
}
///
/// 数据更改
///
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}";
}
///
/// 选择改变
///
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);
}
///
/// 状态改变
///
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;
}
}
}