using System.Windows.Media;

namespace HistoryView.Controls;

/// <summary>
/// Interaction logic for HisProgressBar.xaml
/// </summary>
public partial class HisProgressBar : UserControl
{
    public HisProgressBar()
    {
        InitializeComponent();
    }



    private BarData? _current;

    public BarData? CurrentData
    {
        get
        {
            lock (this)
                return _current;
        }
        set
        {
            lock (this)
            {
                _current = value;
                if (value is null)
                    return;

                App.Current.Dispatcher.Invoke(() =>
                {
                    for (int i = 0; i < RealtimeData.Count; i++)
                    {
                        if (i == RealtimeData.Count - 1)
                        {
                            RealtimeData[i].Value = value.Value < 3 ? 3 : value.Value;
                            RealtimeData[i].Bursh = value.Bursh;
                            break;
                        }
                        RealtimeData[i].Value = RealtimeData[i + 1].Value;
                        RealtimeData[i].Bursh = RealtimeData[i + 1].Bursh;
                    }
                });

            }
        }
    }

    public int RefreshRate
    {
        get { return (int)GetValue(RefreshRateProperty); }
        set { SetValue(RefreshRateProperty, value); }
    }
    public static readonly DependencyProperty RefreshRateProperty =
        DependencyProperty.Register("RefreshRate", typeof(int), typeof(HisProgressBar), new PropertyMetadata(1000));

    private ObservableCollection<BarData> RealtimeData
    {
        get { return (ObservableCollection<BarData>)GetValue(RealtimeDataProperty); }
        set { SetValue(RealtimeDataProperty, value); }
    }

    public static readonly DependencyProperty RealtimeDataProperty =
        DependencyProperty.Register("RealtimeData", typeof(ObservableCollection<BarData>), typeof(HisProgressBar), new PropertyMetadata(new ObservableCollection<BarData>()));

    public double CurrentValue
    {
        get { return (double)GetValue(CurrentValueProperty); }
        set { SetValue(CurrentValueProperty, value); }
    }

    public static readonly DependencyProperty CurrentValueProperty =
        DependencyProperty.Register("CurrentValue", typeof(double), typeof(HisProgressBar), new PropertyMetadata(0d, PropertyChangedCallback));


    private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is not HisProgressBar info)
            return;

        //double value = Math.Round(Convert.ToDouble(e.NewValue) / info.MaxValue, 1);
        lock (info)
            info.CurrentData = new((double)e.NewValue, info.WarningValue, info.AlarmValue);

        if (info.RealtimeData is null || info.RealtimeData.Count == 0)
        {
            info.RealtimeData ??= [];
            var width = info.Width / 3;
            for (int i = 0; i < width; i++)
                info.RealtimeData.Add(new(1, info.WarningValue, info.AlarmValue));
        }
    }

    public double MaxValue
    {
        get { return (double)GetValue(MaxValueProperty); }
        set { SetValue(MaxValueProperty, value); }
    }

    public static readonly DependencyProperty MaxValueProperty =
        DependencyProperty.Register("MaxValue", typeof(double), typeof(HisProgressBar), new PropertyMetadata(100d));

    public double WarningValue
    {
        get { return (double)GetValue(WarningValueProperty); }
        set { SetValue(WarningValueProperty, value); }
    }

    public static readonly DependencyProperty WarningValueProperty =
        DependencyProperty.Register("WarningValue", typeof(double), typeof(HisProgressBar), new PropertyMetadata(25d));

    public double AlarmValue
    {
        get { return (double)GetValue(AlarmValueProperty); }
        set { SetValue(AlarmValueProperty, value); }
    }

    public static readonly DependencyProperty AlarmValueProperty =
        DependencyProperty.Register("AlarmValue", typeof(double), typeof(HisProgressBar), new PropertyMetadata(75d));

}


public partial class BarData : ObservableObject
{
    public BarData(double value, double warningValue, double alarmValue)
    {
        string hexString = "A0";
        this.Value = value;

        this.Bursh = new SolidColorBrush((Color)ColorConverter.ConvertFromString($"#{hexString}32CD32"));

        if (value > alarmValue || value < warningValue)
        {
            this.Bursh = new SolidColorBrush((Color)ColorConverter.ConvertFromString($"#{hexString}FF4040"));
            return;
        }

        //if (value >= warningValue)
        //{
        //    this.Bursh = new SolidColorBrush((Color)ColorConverter.ConvertFromString($"#{hexString}FF8C00"));
        //    return;
        //}
    }

    [ObservableProperty]
    private double _value;
    [ObservableProperty]
    private SolidColorBrush _bursh;
}