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

<div class="paper">
    <PieChart @ref="ChartRef" IsDoughnutChart="true" Width="100" WidthUnit="Unit.Percentage" Height="100" HeightUnit="Unit.Percentage" />
    <div class="text-percent">@($"{CompletedPercent}%")</div>
</div>

@code {
    [Parameter]
    public string ChartName { get; set; } = "";

    private PieChart ChartRef = default!;
    private PieChartOptions ChartOptions = default!;
    private ChartData ChartData = default!;
    private PieChartDataset ChartDataSet = default!;

    private int CompletedPercent = 0;

    protected override void OnInitialized()
    {
        base.OnInitialized();

        ChartDataSet = new PieChartDataset() { BackgroundColor = [ChartColors.GreenStr, ChartColors.OrangeStr, ChartColors.GrayStr], Data = [90, 10, 10] };
        ChartData = new ChartData { Labels = ["Completed", "Error", "Other"], Datasets = [ChartDataSet] };

        ChartOptions = new()
            {
                Responsive = true,
                Cutout = "50%",
                Plugins = new()
                {
                    Datalabels = new()
                    {
                        Color = "#d6d6db",
                        BorderColor = "transparent",
                    },
                    Title = new()
                    {
                        Color = "#d6d6db",
                        Text = ChartName,
                        Font = new()
                        {
                            Family = "Roboto",
                            Size = 20,
                            Weight = "normal",
                        },
                    },
                    Legend = new()
                    {
                        Display = false,
                        Position = LegendPosition.Top,
                        Labels = new()
                        {
                            Color = "#d6d6db",
                            Font = new()
                            {
                                Family = "Roboto",
                                Weight = "normal",
                                Size = 15,
                            }
                        },
                    }
                },
            };
    }

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

    public async Task UpdateData(DailyPerformanceDto data)
    {
        ChartDataSet = new PieChartDataset() { BackgroundColor = [ChartColors.GreenStr, ChartColors.OrangeStr, ChartColors.GrayStr], Data = [data.Completed, data.Error, data.Other] };
        ChartData = new ChartData { Labels = ["Completed", "Error", "Other"], Datasets = [ChartDataSet] };
        await ChartRef.UpdateValuesAsync(ChartData);

        if (data.Completed + data.Error + data.Other != 0) CompletedPercent = (int)(data.Completed * 100.0 / (data.Completed + data.Error + data.Other) + 0.5);
        else CompletedPercent = 0;

        StateHasChanged();
    }
}
