using ScottPlot;
using ScottPlot.Plottables;
using ScottPlot.WPF;
using System.Windows.Media;

namespace ProximaAnalizer.Helpers;

internal class PlotHepler(WpfPlot PlotControl)
{
    public void InitPlot()
    {
        PlotControl.Plot.Grid.XAxisStyle.MajorLineStyle.Width = 1f;
        PlotControl.Plot.Grid.YAxisStyle.MajorLineStyle.Width = 1f;
        PlotControl.Plot.Axes.Bottom.TickLabelStyle.Alignment = Alignment.MiddleLeft;
        PlotControl.Plot.Axes.DateTimeTicksBottom();
        PlotControl.Background = (Brush)App.Current.Resources.FindName("BackgroundColor");
        PlotControl.Plot.RenderManager.RenderStarting += (s, e) =>
        {
            Tick[] ticks = PlotControl.Plot.Axes.Bottom.TickGenerator.Ticks;
            for (int i = 0; i < ticks.Length; i++)
            {
                DateTime dt = DateTime.FromOADate(ticks[i].Position);
                //string label = $"{dt:MM-dd  HH:mm:ss}";
                string label = $"{dt:HH:mm:ss}";
                ticks[i] = new Tick(ticks[i].Position, label);
            }
        };

        PlotControl.Plot.FigureBackground.Color = ScottPlot.Colors.Transparent;
        PlotControl.Plot.Axes.Bottom.TickLabelStyle.Rotation = 90;
        PlotControl.Plot.Axes.Bottom.TickLabelStyle.Alignment = Alignment.MiddleLeft;
        PixelPadding padding = new(48, 48, 56, 40);
        PlotControl.Plot.Layout.Fixed(padding);
        PlotControl.Plot.FigureBackground.Color = ScottPlot.Colors.Transparent;

        PlotControl.Plot.Grid.MajorLineColor = ScottPlot.Colors.LightGray.WithOpacity(1);
        PlotControl.Plot.Grid.MajorLineWidth = 2;

        PlotControl.Plot.Grid.MinorLineColor = ScottPlot.Colors.Black.WithOpacity(.2);
        PlotControl.Plot.Grid.MinorLineWidth = 0f;
    }


    public void AddLeftLine(IEnumerable<DateTime> time, List<float> value, LinePattern linePattern, MarkerStyle markerStyle, float lineWidth, string color)
    {
        Scatter mainScatter = PlotControl.Plot.Add.Scatter(time.ToList(), value);
        mainScatter.MarkerStyle = markerStyle;
        mainScatter.LineWidth = lineWidth;
        mainScatter.LinePattern = linePattern;
        mainScatter.Color = new ScottPlot.Color(color);
        mainScatter.Axes.YAxis = PlotControl.Plot.Axes.Left;
    }

    public void AddRightLine(IEnumerable<DateTime> time, List<float> value, LinePattern linePattern, MarkerStyle markerStyle, float lineWidth, string color)
    {
        Scatter mainScatter = PlotControl.Plot.Add.Scatter(time.ToList(), value);
        mainScatter.MarkerStyle = markerStyle;
        mainScatter.LineWidth = lineWidth;
        mainScatter.LinePattern = linePattern;
        mainScatter.Color = new ScottPlot.Color(color);
        mainScatter.Axes.YAxis = PlotControl.Plot.Axes.Right;
    }

    public bool AddAlarmLine(DateTime dateTime, string text)
    {
        var line = PlotControl.Plot.Add.VerticalLine(dateTime.ToOADate());
        line.Color = new ScottPlot.Color("DC143C");
        line.Text = text;
        line.LineWidth = 1f;
        line.LabelOppositeAxis = true;
        return true;
    }

    public bool AddWarningLine(DateTime dateTime, string text)
    {
        var line = PlotControl.Plot.Add.VerticalLine(dateTime.ToOADate());
        line.Color = new ScottPlot.Color("FFA500");
        line.LineWidth = 1f;
        line.Text = text;
        line.LabelOppositeAxis = true;
        return true;
    }
}
