@namespace Lumeo

<Chart Option="@_option" Width="@Width" Height="@Height" Theme="@Theme" Class="@Class" IsLoading="@IsLoading" ShowLoadingSkeleton="@ShowLoadingSkeleton" SkeletonKind="@SkeletonKind" SkeletonStyle="@SkeletonStyle" ShowLoadingLabel="@ShowLoadingLabel" LoadingText="@LoadingText" PhantomCycleMs="@PhantomCycleMs" @attributes="AdditionalAttributes" />

@code {
    [Parameter] public List<NightingaleData> Data { get; set; } = new();
    [Parameter] public bool ShowLegend { get; set; } = true;
    [Parameter] public bool ShowTooltip { get; set; } = true;
    [Parameter] public bool ShowLabels { get; set; } = true;
    [Parameter] public List<string>? Colors { get; set; }
    [Parameter] public string Width { get; set; } = "100%";
    [Parameter] public string Height { get; set; } = "350px";
    [Parameter] public string? Theme { get; set; }
    [Parameter] public string? Class { get; set; }
    [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object>? AdditionalAttributes { get; set; }
    /// <summary>When true, overlays a shape-aware loading skeleton on top of the chart (consumer is fetching data). Flips back to the real chart once set to false.</summary>
    [Parameter] public bool IsLoading { get; set; }
    /// <summary>When true (default), renders a shape-aware skeleton while the chart has not yet mounted and whenever <see cref="IsLoading"/> is true. Set to <c>false</c> to opt out.</summary>
    [Parameter] public bool ShowLoadingSkeleton { get; set; } = true;
    /// <summary>Shape family for the loading skeleton. Defaults to <c>ChartSkeletonKind.Pie</c> for this chart type; override if you want a different silhouette.</summary>
    [Parameter] public ChartSkeletonKind SkeletonKind { get; set; } = ChartSkeletonKind.Pie;
    /// <summary>Phantom (default) renders the real chart with placeholder data and morphs to real data via ECharts' option-change animation. Silhouette falls back to the legacy SVG skeleton overlay.</summary>
    [Parameter] public ChartSkeletonStyle SkeletonStyle { get; set; } = ChartSkeletonStyle.Phantom;
    /// <summary>Phantom-mode refresh interval in milliseconds. Lower = faster data cycling while loading. Defaults to 1400.</summary>
    [Parameter] public int PhantomCycleMs { get; set; } = 1400;
    /// <summary>When true (default), shows a small "Loading…" pill in the top-right corner whenever <see cref="IsLoading"/> is active. Works on top of any <see cref="SkeletonStyle"/>.</summary>
    [Parameter] public bool ShowLoadingLabel { get; set; } = true;
    /// <summary>Label rendered next to the spinner inside the loading pill. Set to empty to hide. Defaults to "Loading…".</summary>
    [Parameter] public string? LoadingText { get; set; } = "Loading…";

    [Parameter] public bool ShowDataLabels { get; set; }
    [Parameter] public string LabelPosition { get; set; } = "outside";
    [Parameter] public string? LabelFormat { get; set; }
    [Parameter] public bool DataZoom { get; set; }
    [Parameter] public bool Toolbox { get; set; }
    [Parameter] public List<string>? ColorPalette { get; set; }
    [Parameter] public int? AnimationDuration { get; set; }
    [Parameter] public string? AnimationEasing { get; set; }
    [Parameter] public Dictionary<string, object>? OptionOverride { get; set; }

    private EChartOption _option = new();

    protected override void OnParametersSet()
    {
        var pieData = Data.Select(d => new EChartPieData
        {
            Name = d.Name,
            Value = d.Value
        }).ToList();

        var series = new EChartSeries
        {
            Type = "pie",
            RoseType = "area",
            RadiusList = new List<string> { "10%", "70%" },
            CenterList = new List<string> { "50%", "50%" },
            Data = pieData,
            Label = new EChartLabel
            {
                Show = ShowLabels || ShowDataLabels,
                Position = ShowDataLabels ? LabelPosition : "outside",
                Formatter = ShowDataLabels && LabelFormat is not null ? LabelFormat : "{b}: {d}%"
            },
            Emphasis = new EChartEmphasis
            {
                Focus = "self",
                ItemStyle = new EChartItemStyle { ShadowBlur = 10 }
            }
        };

        _option = new EChartOption
        {
            Series = new List<EChartSeries> { series },
            Color = ColorPalette ?? Colors
        };

        if (ShowTooltip)
            _option.Tooltip = new EChartTooltip { Trigger = "item" };

        if (ShowLegend)
            _option.Legend = new EChartLegend { Orient = "horizontal", Bottom = "0%", Data = Data.Select(d => d.Name).ToList() };

        if (Toolbox)
            _option.Toolbox = new EChartToolbox { Show = true, Feature = new EChartToolboxFeature { SaveAsImage = new() { Show = true }, Restore = new() { Show = true } } };

        if (AnimationDuration.HasValue) _option.AnimationDuration = AnimationDuration;
        if (!string.IsNullOrEmpty(AnimationEasing)) _option.AnimationEasing = AnimationEasing;

        ChartHelper.ApplyOptionOverride(_option, OptionOverride);
    }

    public class NightingaleData
    {
        public string Name { get; set; } = "";
        public double Value { get; set; }
    }
}
