@using Radzen.Blazor.Rendering
@typeparam TItem
@inherits Radzen.Blazor.RadzenPieSeries<TItem>
@implements Radzen.Blazor.IChartDonutSeries

<CascadingValue Value="@this">
  @ChildContent
</CascadingValue>

@code {
    public RenderFragment RenderTitle(double x, double y)
    {
      if (TitleTemplate == null)
      {
        return null!;
      }

        var radius = CurrentRadius;
        var innerRadius = InnerRadius ?? radius / 2;

        var left = (x + CenterX - innerRadius).ToInvariantString();
        var top = (y + CenterY - innerRadius).ToInvariantString();
        var width = innerRadius * 2;
        var height = innerRadius * 2;

      return @<div style="position: absolute;left: @(left)px;top: @(top)px;width: @(width.ToInvariantString())px; height: @(height.ToInvariantString())px;">
                @TitleTemplate
          </div>
      ;
    }

    public override RenderFragment Render(ScaleBase categoryScale, ScaleBase valueScale)
    {
        var chart = RequireChart();
        var seriesIndex = chart.Series.IndexOf(this);
        var className = $"rz-donut-series rz-series-{seriesIndex}";
        var gradient = FillMode == FillMode.Gradient;
        var x = CenterX;
        var y = CenterY;
        var radius = CurrentRadius;
        var innerRadius = InnerRadius ?? radius / 2;
        var items = PositiveItems;
        var centerStyle = $"--rz-pie-cx: {x.ToInvariantString()}px; --rz-pie-cy: {y.ToInvariantString()}px";

        return
        @<g>
            <g class="@className" style="@centerStyle">
                @if (items.Any())
                {
                    var sum = items.Sum(Value);
                    var startAngle = StartAngle;

                    @foreach (var data in items)
                    {
                        var value = Value(data);
                        var sweepAngle = (value / sum) * TotalAngle;
                        var endAngle = startAngle - sweepAngle;

                        var itemRadius = GetItemRadius(data, radius);
                        var d = Segment(x, y, itemRadius, innerRadius, startAngle, endAngle);

                        var midAngle = startAngle - sweepAngle / 2;
                        startAngle = endAngle;

                        var index = Items.IndexOf(data);
                        var exploded = ExplodeOffset > 0 && IsExploded(data);
                        var arcClassName = $"rz-series-item-{index}" + (exploded ? " rz-state-exploded" : "");
                        var fill = PickColor(index, Fills);
                        var stroke = PickColor(index, Strokes);
                        var gradId = $"rzPieGradient{chart.UniqueID}-{seriesIndex}-{index}";
                        var fillRef = gradient ? $"url(#{gradId})" : (FillMode == FillMode.None ? "none" : fill);
                        var gradientColor = fill ?? "var(--rz-series-color)";
                        var explodeStyle = ExplodeOffset > 0 ? $"--rz-explode-x: {(ExplodeOffset * Math.Cos(DegToRad(midAngle))).ToInvariantString()}px; --rz-explode-y: {(-ExplodeOffset * Math.Sin(DegToRad(midAngle))).ToInvariantString()}px" : "";

                        <g class="@arcClassName" style="@($"--rz-segment-order: {index};{explodeStyle}")">
                            @if (!string.IsNullOrEmpty(d))
                            {
                                @if (gradient)
                                {
                                    <RadialGradientDef Id="@gradId" Color="@gradientColor" Cx="@x" Cy="@y" R="@radius" InnerOffset="@GradientInnerOffset" InnerOpacity="@GradientEndOpacity" MidOffset="@GradientMidOffset" MidOpacity="@GradientStartOpacity" EndOpacity="@GradientRimOpacity" />
                                }
                                @if (ExplodeOffset > 0)
                                {
                                    <path class="rz-hit-area" d="@d" style="fill: transparent; stroke: none; pointer-events: fill" />
                                }
                                <Path D="@d" Fill="@fillRef" StrokeWidth="@StrokeWidth" Stroke="@stroke" />
                            }
                        </g>
                    }
                }
                else
                {
                    var arcClassName = $"rz-series-item-0";
                    var d = Segment(x, y, radius, radius - 1,  StartAngle, StartAngle - TotalAngle);
                    var fill = PickColor(0, Fills);
                    var stroke = PickColor(0, Strokes);
                    <g class="@arcClassName">
                        <Path D="@d" Fill="@fill" StrokeWidth="@StrokeWidth" Stroke="@stroke" />
                    </g>
                }
            </g>

        @if (!string.IsNullOrEmpty(Title) && TitleTemplate == null)
        {
            <g class="rz-donut-title">
                <text class="rz-donut-title" x="@CenterX.ToInvariantString()" y="@CenterY.ToInvariantString()">
                    @Title
                </text>
            </g>
        }
        </g>;
    }
}