// Copyright © Selmo Technology GmbH. All Rights Reserved

using BlazorBootstrap;
using Microsoft.AspNetCore.Components;
using SelmoStudio.Hmi.Server.ProcessValue.Model;
using SelmoStudio.Hmi.Server.ProcessValue.Services;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Service;

namespace SelmoStudio.Hmi.Server.SelmoHmi.DataRecords.Pages
{
    public partial class DataRecordPage
    {
        [Inject]
        private IProjectService _projectService { get; set; }

        [Inject]
        private IHistoryProcessValueService _historyService { get; set; }

        [Parameter]
        public List<ProcessInfoDataCollection> ProcessInfoCollections { get; set; }

        private ProcessInfoDataCollection currentCollection;

        private bool recording = false;

        private Task testTask;
        private float testFloat = 0;

        private LineChart lineChart = default!;
        private LineChartOptions lineChartOptions = default!;
        private ChartData chartData = default!;

        protected override void OnInitialized()
        {
            ProcessInfoCollections = _projectService.GetTestProcessInfoCollections();
            currentCollection = ProcessInfoCollections.First();
            chartData = new ChartData { Labels = GetLabelsForTime(), Datasets = GetDefaultDataSets() };
            lineChartOptions = GetLineChartOptions();
        }

        private List<string> GetLabelsForTime()
        {
            var coll = currentCollection.processInfoValues.Select(s => $"{s.Values.timestamp.Minute.ToString()}:{s.Values.timestamp.Minute.ToString()}").ToList();
            coll.Reverse();
            return coll;
        }

        private List<IChartDataset> GetDefaultDataSets()
        {
            var data = currentCollection.processInfoValues.Select(s => double.Parse(s.Values.value.ToString())).ToList();
            data.Reverse();
            var datasets = new List<IChartDataset>
                       {
                           new LineChartDataset
                           {
                               Label = currentCollection.processValue.Name,
                               Data = data,
                               BackgroundColor = new List<string> { "rgb(88, 80, 141)" },
                               BorderColor = new List<string> { "rgb(88, 80, 141)" },
                               BorderWidth = new List<double> { 2 },
                               HoverBorderWidth = new List<double> { 4 },
                               PointBackgroundColor = new List<string> { "rgb(88, 80, 141)" },
                               PointBorderColor = new List<string> { "rgb(88, 80, 141)" },
                               PointRadius = new List<int> { 2 },
                               PointHoverRadius = new List<int> { 4 }
                           }
                       };

            return datasets;
        }

        private LineChartOptions GetLineChartOptions()
        {
            var options = new LineChartOptions();

            options.Interaction.Mode = InteractionMode.Index;

            options.Plugins.Title!.Text = "Chart";
            options.Plugins.Title.Display = true;
            options.Plugins.Title!.Font!.Size = 20;

            options.Responsive = true;

            options.Scales.X!.Title!.Text = "Value";
            options.Scales.X.Title.Display = true;

            options.Scales.Y!.Title!.Text = "Time";
            options.Scales.Y.Title.Display = true;

            return options;
        }

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
                await lineChart.InitializeAsync(chartData, lineChartOptions);
        }

        private async Task Callback(object value)
        {
            if (value is ProcessInfoDataCollection parsedValue)
            {
                currentCollection = parsedValue;
                var resp = await _historyService.GetValues(DateTime.Now.AddDays(testFloat - 1), DateTime.Now, currentCollection.processValue);
                currentCollection.processInfoValues = resp.Value;
            }
        }
    }
}