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.RadarChart;

namespace RobotNet.WebApp.Charts.Components;

public partial class RadarChart : RobotNetChart
{
    public RadarChart()
    {
        chartType = ChartType.Radar;
    }

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

    public async Task UpdateAsync(ChartData chartData, IChartOptions chartOptions)
    {
        if (chartData is not null && chartData.Datasets is not null)
        {
            var datasets = chartData.Datasets.OfType<RadarChartDataset>();
            var data = new { chartData.Labels, Datasets = datasets };
            await JSRuntime.InvokeVoidAsync($"window.blazorChart.update", Id, data, (RadarChartOptions)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<RadarChartDataset>();
            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 RadarChartDataset radarChartDataset && radarChartDataset.Label == dataLabel)
                if (data is RadarChartDatasetData radarChartDatasetData)
                {
                    if (radarChartDatasetData.Data is double dataValue) radarChartDataset.Data?.Add(dataValue);
                    radarChartDataset.BackgroundColor?.Add(radarChartDatasetData.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 RadarChartDataset radarChartDataset)
            {
                var chartDatasetData = data.FirstOrDefault(x => x is RadarChartDatasetData radarChartDatasetData && radarChartDatasetData.DatasetLabel == radarChartDataset.Label);

                if (chartDatasetData is RadarChartDatasetData radarChartDatasetData)
                {
                    if (radarChartDatasetData.Data is double dataValue) radarChartDataset.Data?.Add(dataValue);
                    radarChartDataset.BackgroundColor?.Add(radarChartDatasetData.BackgroundColor!);
                }
            }
        await JSRuntime.InvokeVoidAsync($"window.blazorChart.addDatasetsData", Id, dataLabel, data?.Select(x => (RadarChartDatasetData)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 RadarChartDataset radarChartDataset)
        {
            chartData.Datasets.Add(radarChartDataset);
            await JSRuntime.InvokeVoidAsync($"window.blazorChart.addDataset", Id, radarChartDataset);
        }

        return chartData;
    }
}
