using Microsoft.JSInterop;
using RobotNet.WebApp.Charts.Core;
using RobotNet.WebApp.Charts.Enums;
using RobotNet.WebApp.Charts.Models.Common;
using RobotNet.WebApp.Charts.Models.Common.Dataset;
using RobotNet.WebApp.Charts.Models.ScatterChart;

namespace RobotNet.WebApp.Charts.Components;

public partial class ScatterChart : RobotNetChart
{
    public ScatterChart()
    {
        chartType = ChartType.Scatter;
    }

    public async Task InitializeAsync(ChartData chartData, IChartOptions chartOptions)
    {
        if (chartData is not null && chartData.Datasets is not null)
        {
            var datasets = chartData.Datasets.OfType<ScatterChartDataset>();
            var data = new { chartData.Labels, Datasets = datasets };
            await JSRuntime.InvokeVoidAsync($"window.blazorChart.initialize", Id, chartType, data, (ScatterChartOptions)chartOptions);
        }
    }

    public async Task UpdateAsync(ChartData chartData, IChartOptions chartOptions)
    {
        if (chartData is not null && chartData.Datasets is not null)
        {
            var datasets = chartData.Datasets.OfType<ScatterChartDataset>();
            var data = new { chartData.Labels, Datasets = datasets };
            await JSRuntime.InvokeVoidAsync($"window.blazorChart.update", Id, data, (ScatterChartOptions)chartOptions);
        }
    }

    public async Task UpdateValuesAsync(ChartData chartData)
    {
        if (chartData is not null && chartData.Datasets is not null && chartData.Datasets.Count != 0)
        {
            var datasets = chartData.Datasets.OfType<ScatterChartDataset>();
            var data = new { chartData.Labels, Datasets = datasets };
            await JSRuntime.InvokeVoidAsync("window.blazorChart.updateData", Id, data);
        }
    }

    public async Task<ChartData> AddDataAsync(ChartData chartData, string dataLabel, IChartDatasetData data)
    {
        ArgumentNullException.ThrowIfNull(chartData);
        ArgumentNullException.ThrowIfNull(chartData.Datasets);
        ArgumentNullException.ThrowIfNull(data);

        foreach (var dataset in chartData.Datasets)
            if (dataset is ScatterChartDataset scatterChartDataset && scatterChartDataset.Label == dataLabel)
                if (data is ScatterChartDatasetData scatterChartDatasetData)
                {
                    if (scatterChartDatasetData.Data is ScatterChartDataPoint dataValue) scatterChartDataset.Data?.Add(dataValue);
                    scatterChartDataset.BackgroundColor?.Add(scatterChartDatasetData.BackgroundColor!);
                }
        await JSRuntime.InvokeVoidAsync($"window.blazorChart.addDatasetData", Id, dataLabel, data);
        return chartData;
    }

    public async Task<ChartData> AddDataAsync(ChartData chartData, string dataLabel, IReadOnlyCollection<IChartDatasetData> data)
    {
        ArgumentNullException.ThrowIfNull(chartData);
        ArgumentNullException.ThrowIfNull(chartData.Datasets);
        ArgumentNullException.ThrowIfNull(chartData.Labels);
        ArgumentNullException.ThrowIfNull(dataLabel);
        ArgumentNullException.ThrowIfNull(data);

        if (string.IsNullOrWhiteSpace(dataLabel))
            throw new Exception($"{nameof(dataLabel)} cannot be empty.");

        if (data.Count == 0)
            throw new Exception($"{nameof(data)} cannot be empty.");

        if (chartData.Datasets.Count != data.Count)
            throw new InvalidDataException("The chart dataset count and the new data points count do not match.");

        if (chartData.Labels.Contains(dataLabel))
            throw new Exception($"{dataLabel} already exists.");

        chartData.Labels.Add(dataLabel);

        foreach (var dataset in chartData.Datasets)
            if (dataset is ScatterChartDataset scatterChartDataset)
            {
                var chartDatasetData = data.FirstOrDefault(x => x is ScatterChartDatasetData scatterChartDatasetData && scatterChartDatasetData.DatasetLabel == scatterChartDataset.Label);

                if (chartDatasetData is ScatterChartDatasetData scatterChartDatasetData)
                {
                    if (scatterChartDatasetData.Data is ScatterChartDataPoint dataValue) scatterChartDataset.Data?.Add(dataValue);
                    scatterChartDataset.BackgroundColor?.Add(scatterChartDatasetData.BackgroundColor!);
                }
            }
        await JSRuntime.InvokeVoidAsync($"window.blazorChart.addDatasetsData", Id, dataLabel, data?.Select(x => (ScatterChartDatasetData)x));
        return chartData;
    }

    public async Task<ChartData> AddDatasetAsync(ChartData chartData, IChartDataset chartDataset)
    {
        ArgumentNullException.ThrowIfNull(chartData);
        ArgumentNullException.ThrowIfNull(chartData.Datasets);
        ArgumentNullException.ThrowIfNull(chartDataset);

        if (chartDataset is ScatterChartDataset scatterChartDataset)
        {
            chartData.Datasets.Add(scatterChartDataset);
            await JSRuntime.InvokeVoidAsync($"window.blazorChart.addDataset", Id, scatterChartDataset);
        }

        return chartData;
    }
}
