// Copyright © Selmo Technology GmbH. All Rights Reserved

using BlazorBootstrap;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Shared
{
    internal class TestMeasurementData
    {
        internal static List<string> Labels = new List<string> { "12:18:01","12:18:02","12:18:03","12:18:04","12:18:05",
            "12:18:06","12:18:07","12:18:08","12:18:09","12:18:10",  };

        internal static List<List<double>> DataSets =
            new List<List<double>>() {
                new(){ 261.503, 266.940, 161.397, 98.588, 288.316, 1.809, 130.837, 290.108, 141.668, 252.596, },
                new(){ 103.200, 124.816, 48.553, 91.716, 73.617, 134.343, 295.141, 172.250, 103.035, 195.452, },
                new(){ 109.769, 114.355, 93.802, 258.763, 111.264, 40.300, 106.882, 93.694, 209.392, 208.215, },
                new(){ 29.638, 248.677, 299.887, 131.378, 201.304, 29.346, 196.180, 46.628, 63.195, 191.194, },
                new(){ 102.001, 195.592, 236.800, 78.261, 120.751, 159.251, 103.129, 39.398, 185.073, 135.276, },
                new(){ 67.673, 214.761, 124.815, 106.860, 260.282, 12.778, 220.302, 24.031, 28.973, 252.494, },
            };
    }

    public class LineChartBuilder
    {
        public LineChartOptions CreateTestLineChartOptions()
        {
            LineChartOptions lineChartOptions = default!;

            lineChartOptions = new();
            lineChartOptions.Responsive = true;
            lineChartOptions.Interaction = new Interaction { Mode = InteractionMode.Index };

            lineChartOptions.Scales.X.Title.Text = "Time";
            lineChartOptions.Scales.X.Title.Display = true;

            lineChartOptions.Scales.Y.Title.Text = "value";
            lineChartOptions.Scales.Y.Title.Display = true;

            lineChartOptions.Plugins.Title.Text = "Test Measurement data";
            lineChartOptions.Plugins.Title.Display = true;

            return lineChartOptions;
        }

        public ChartData CreateTestChartData()
        {
            var colors = ColorBuilder.CategoricalTwelveColors;
            int i = 0;
            var datasets = new List<IChartDataset>();
            foreach (var measDataSet in TestMeasurementData.DataSets)
            {
                var colorIndex = i % 11;
                datasets.Add(CreateLineDataSet($"testname{i}", measDataSet, colors[colorIndex]));
                i++;
            }

            var chartData = new ChartData
            {
                Labels = TestMeasurementData.Labels,
                Datasets = datasets
            };

            return chartData;
        }

        private LineChartDataset CreateLineDataSet(string name, List<double> data, string color)
        {
            var dataset = new LineChartDataset()
            {
                Label = name,
                Data = data,
                BackgroundColor = new List<string> { color },
                BorderColor = new List<string> { color },
                BorderWidth = new List<double> { 2 },
                HoverBorderWidth = new List<double> { 4 },
                PointBackgroundColor = new List<string> { color },
                PointRadius = new List<int> { 0 },
                PointHoverRadius = new List<int> { 4 },
            };

            return dataset;
        }
    }
}