@using RobotNet.Script.Shares.Dashboard
@using RobotNet.WebApp.Charts.Components
@using RobotNet.WebApp.Charts.Core
@using RobotNet.WebApp.Charts.Models.BarChart
@using RobotNet.WebApp.Charts.Models.BarChart.Axes
@using RobotNet.WebApp.Charts.Models.Common.Dataset
@using RobotNet.WebApp.Charts.Enums

<div class="paper">
    <BarChart @ref="ChartRef" Width="100" WidthUnit="Unit.Percentage" Height="100" HeightUnit="Unit.Percentage" Style="height: 30vh; width: 50vw" />
</div>

@code {
    private BarChart ChartRef = default!;
    private BarChartOptions ChartOptions = default!;
    private ChartData ChartData = default!;

    private DailyPerformanceDto[] DataParam = [];

    protected override void OnInitialized()
    {
        var completed = new BarChartDataset() { Label = "Completed", BackgroundColor = [ChartColors.GreenStr], Data = [.. DataParam.Select(d => d.Completed)] };
        var error = new BarChartDataset() { Label = "Error", BackgroundColor = [ChartColors.OrangeStr], Data = [.. DataParam.Select(d => d.Error)] };
        var other = new BarChartDataset() { Label = "Other", BackgroundColor = [ChartColors.GrayStr], Data = [.. DataParam.Select(d => d.Other)] };
        ChartData = new ChartData { Labels = [.. DataParam.Select(d => d.Label)], Datasets = [completed, error, other] };

        ChartOptions = new()
            {
                Responsive = true,
                Scales = new()
                {
                    X = new()
                    {
                        Title = new ChartAxesTitle
                        {
                            Text = "Day",
                            Display = true,
                            Color = "#d6d6db",
                            Font = new()
                            {
                                Family = "Roboto",
                                Size = 15,
                            }
                        },
                        Stacked = true,
                        Grid = new()
                        {
                            Display = false,
                        },
                        Ticks = new()
                        {
                            Color = "#d6d6db"
                        }
                    },
                    Y = new()
                    {
                        Title = new ChartAxesTitle
                        {
                            Text = "Mission",
                            Display = true,
                            Color = "#d6d6db",
                            Font = new()
                            {
                                Family = "Roboto",
                                Size = 15,
                            },
                        },
                        Min = 0,
                        Stacked = true,
                        Grid = new()
                        {
                            Display = false,
                        },
                        Ticks = new()
                        {
                            Color = "#d6d6db"
                        }
                    },
                },
                Plugins = new()
                {
                    Datalabels = new()
                    {
                        Color = "#d6d6db",
                        BorderColor = "transparent",
                    },
                    Title = new()
                    {
                        Color = "#d6d6db",
                        Text = "Weekly AMR Performance",
                        Font = new()
                        {
                            Family = "Roboto",
                            Size = 20,
                            Weight = "normal",
                        },
                    },
                    Legend = new()
                    {
                        Display = true,
                        Labels = new()
                        {
                            Color = "#d6d6db",
                            Font = new()
                            {
                                Family = "Roboto",
                                Weight = "normal",
                                Size = 15,
                            }
                        }
                    }
                },
            };
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await ChartRef.InitializeAsync(chartData: ChartData, chartOptions: ChartOptions);
        }
        await base.OnAfterRenderAsync(firstRender);
    }

    public async Task UpdateData(DailyPerformanceDto[] dataParam)
    {
        var completed = new BarChartDataset() { Label = "Completed", BackgroundColor = [ChartColors.GreenStr], Data = [.. dataParam.Select(d => d.Completed)] };
        var error = new BarChartDataset() { Label = "Error", BackgroundColor = [ChartColors.OrangeStr], Data = [.. dataParam.Select(d => d.Error)] };
        var other = new BarChartDataset() { Label = "Other", BackgroundColor = [ChartColors.GrayStr], Data = [.. dataParam.Select(d => d.Other)] };
        ChartData = new ChartData { Labels = [.. dataParam.Select(d => d.Label)], Datasets = [completed, error, other] };

        await ChartRef.UpdateValuesAsync(ChartData);
    }
}
