using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Components;
using Radzen.Blazor.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Radzen.Blazor
{
    /// <summary>
    /// Renders bar series in <see cref="RadzenChart" />.
    /// </summary>
    /// <typeparam name="TItem">The type of the series data item.</typeparam>
    public partial class RadzenStackedBarSeries<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] TItem> : CartesianSeries<TItem>, IChartStackedBarSeries
    {
        /// <summary>
        /// Specifies the fill (background color) of the bar series.
        /// </summary>
        /// <value>The fill.</value>
        [Parameter]
        public string? Fill { get; set; }

        /// <summary>
        /// Specifies a list of colors that will be used to set the individual bar backgrounds.
        /// </summary>
        /// <value>The fills.</value>
        [Parameter]
        public IEnumerable<string>? Fills { get; set; }

        /// <summary>
        /// Specifies the stroke (border color) of the bar series.
        /// </summary>
        /// <value>The stroke.</value>
        [Parameter]
        public string? Stroke { get; set; }

        /// <summary>
        /// Specifies a list of colors that will be used to set the individual bar borders.
        /// </summary>
        /// <value>The strokes.</value>
        [Parameter]
        public IEnumerable<string>? Strokes { get; set; }

        /// <summary>
        /// Gets or sets the width of the stroke (border).
        /// </summary>
        /// <value>The width of the stroke.</value>
        [Parameter]
        public double StrokeWidth { get; set; }

        /// <summary>
        /// Gets or sets the type of the line used to render the bar border.
        /// </summary>
        /// <value>The type of the line.</value>
        [Parameter]
        public LineType LineType { get; set; }

        /// <summary>
        /// Gets or sets the color range of the fill.
        /// </summary>
        /// <value>The color range of the fill.</value>
        [Parameter]
        public IList<SeriesColorRange>? FillRange { get; set; }

        /// <summary>
        /// Gets or sets the color range of the stroke.
        /// </summary>
        /// <value>The color range of the stroke.</value>
        [Parameter]
        public IList<SeriesColorRange>? StrokeRange { get; set; }

        /// <summary>
        /// Specifies how the series is filled. Set to <see cref="FillMode.Solid"/> by default.
        /// Use <see cref="FillMode.Gradient"/> for a fill that fades toward the axis baseline, or <see cref="FillMode.None"/> to render only the outline.
        /// </summary>
        /// <value>The fill mode. Default is <see cref="FillMode.Solid"/>.</value>
        [Parameter]
        public FillMode FillMode { get; set; } = FillMode.Solid;

        /// <summary>
        /// Specifies the opacity at the value end of the gradient fill. Used when <see cref="FillMode"/> is <see cref="FillMode.Gradient"/>.
        /// </summary>
        /// <value>The gradient start opacity. Default is <c>0.85</c>.</value>
        [Parameter]
        public double GradientStartOpacity { get; set; } = 0.85;

        /// <summary>
        /// Specifies the opacity at the baseline of the gradient fill. Used when <see cref="FillMode"/> is <see cref="FillMode.Gradient"/>.
        /// </summary>
        /// <value>The gradient end opacity. Default is <c>0.4</c>.</value>
        [Parameter]
        public double GradientEndOpacity { get; set; } = 0.4;

        /// <inheritdoc />
        public override string Color
        {
            get
            {
                return Fill ?? string.Empty;
            }
        }

        /// <inheritdoc />
        public override ScaleBase TransformCategoryScale(ScaleBase scale)
        {
            ArgumentNullException.ThrowIfNull(scale);

            if (Items.Any())
            {
                var stackedBarSeries = BarSeries.Cast<IChartStackedBarSeries>();
                var count = stackedBarSeries.Max(series => series.Count);
                var categories = Enumerable.Range(0, count);

                var positiveSums = categories.Select(i => stackedBarSeries.Sum(series => Math.Max(0, series.ValueAt(i))));
                var negativeSums = categories.Select(i => stackedBarSeries.Sum(series => Math.Min(0, series.ValueAt(i))));

                var maxPositive = positiveSums.Max();
                var minNegative = negativeSums.Min();

                scale.Input.MergeWidth(new ScaleRange { Start = minNegative, End = maxPositive });
            }

            return scale;
        }

        /// <inheritdoc />
        public override ScaleBase TransformValueScale(ScaleBase scale)
        {
            ArgumentNullException.ThrowIfNull(scale);

            return base.TransformCategoryScale(scale);
        }

        /// <inheritdoc />
        protected override IList<object> GetCategories()
        {
            return base.GetCategories().Reverse().ToList();
        }

        private IList<IChartSeries> BarSeries => Chart?.Series?.Where(series => series is IChartStackedBarSeries).Cast<IChartSeries>().ToList() ?? new List<IChartSeries>();

        private IList<IChartSeries> VisibleBarSeries => BarSeries.Where(series => series.Visible).ToList();

        private IList<IChartStackedBarSeries> StackedBarSeries => VisibleBarSeries.Cast<IChartStackedBarSeries>().ToList();

        /// <inheritdoc />
        protected override string TooltipStyle(TItem item)
        {
            var style = base.TooltipStyle(item);

            var index = Items.IndexOf(item);

            if (index >= 0)
            {
                var color = PickColor(index, Fills, Fill, FillRange, Value(item));

                if (color != null)
                {
                    style = $"{style}; border-color: {color};";
                }
            }

            return style;
        }

        private double BandHeight
        {
            get
            {
                var barSeries = VisibleBarSeries;

                if (Chart?.BarOptions?.Height.HasValue == true)
                {
                    return Chart.BarOptions.Height.Value * barSeries.Count;
                }
                else if (Chart?.BarOptions?.CategoryGap is double gap)
                {
                    var step = System.Math.Abs(Chart.ValueScale.Scale(1, true) - Chart.ValueScale.Scale(0, true));
                    return step * (1 - gap);
                }
                else if (Chart != null)
                {
                    var availableHeight = Chart.ValueScale.OutputSize; // - (Chart.ValueAxis.Padding * 2);
                    var bands = barSeries.Cast<IChartStackedBarSeries>().Max(series => series.Count) + 2;
                    return availableHeight / bands;
                }

                return 0;
            }
        }

        double BarHeight
        {
            get
            {
                if (Chart == null)
                {
                    return 0;
                }

                var h = Chart.BarOptions.Height ?? (BandHeight - Chart.BarOptions.Margin);
                var max = Chart.BarOptions.EffectiveMaxHeight;
                return max is double m && h > m ? m : h;
            }
        }

        int BarIndex => VisibleBarSeries.IndexOf(this);

        private double GetBarTop(TItem item, Func<TItem, double>? category = null)
        {
            if (Chart == null)
            {
                return 0;
            }

            category = category ?? ComposeCategory(Chart.ValueScale);

            return category(item) - BarHeight / 2;
        }

        private static (double positiveSum, double negativeSum) Sum(int barIndex, IEnumerable<IChartStackedBarSeries> stackedBarSeries, double category)
        {
            var values = stackedBarSeries.Take(barIndex).SelectMany(series => series.ValuesForCategory(category)).DefaultIfEmpty(0).ToList();
            var positiveSum = values.Where(v => v >= 0).DefaultIfEmpty(0).Sum();
            var negativeSum = values.Where(v => v < 0).DefaultIfEmpty(0).Sum();
            return (positiveSum, negativeSum);
        }
        private double GetBarRight(TItem item, int barIndex, Func<TItem, double> category, IEnumerable<IChartStackedBarSeries> stackedBarSeries)
        {
            var value = Value(item);
            var (positiveSum, negativeSum) = Sum(barIndex, stackedBarSeries, category(item));

            if (Chart == null)
            {
                return 0;
            }

            if (value >= 0)
            {
                var y = Chart.CategoryScale.Scale(value + positiveSum);
                return y;
            }
            else
            {
                var y = Chart.CategoryScale.Scale(negativeSum);
                return y;
            }
        }

        private double GetBarLeft(TItem item, int barIndex, Func<TItem, double> category, IEnumerable<IChartStackedBarSeries> stackedBarSeries)
        {
            var value = Value(item);
            var (positiveSum, negativeSum) = Sum(barIndex, stackedBarSeries, category(item));

            if (Chart == null)
            {
                return 0;
            }

            if (value >= 0)
            {
                var ticks = Chart.CategoryScale.Ticks(Chart.ValueAxis.TickDistance);
                var sum = Math.Max(ticks.Start, positiveSum);
                return Chart.CategoryScale.Scale(sum);
            }
            else
            {
                var ticks = Chart.CategoryScale.Ticks(Chart.ValueAxis.TickDistance);
                var sum = Math.Max(ticks.Start, negativeSum + value);
                return Chart.CategoryScale.Scale(sum);
            }
        }

        int IChartBarSeries.Count
        {
            get
            {
                return Items.Count;
            }
        }
        double IChartStackedBarSeries.ValueAt(int index)
        {
            if (Items == null || index < 0 || index >= Items.Count)
            {
                return 0;
            }

            return Value(Items[index]);
        }

        IEnumerable<double> IChartStackedBarSeries.ValuesForCategory(double value)
        {
            if (Items == null || Chart == null)
            {
                return Enumerable.Empty<double>();
            }

            var category = ComposeCategory(Chart.ValueScale);

            return Items.Where(item => category(item) == value).Select(Value);
        }

        IEnumerable<object> IChartStackedBarSeries.ItemsForCategory(double value)
        {
            if (Items == null || Chart == null)
            {
                return Enumerable.Empty<object>();
            }

            var category = ComposeCategory(Chart.ValueScale);

            return Items.Where(item => category(item) == value).Cast<object>();
        }

        /// <inheritdoc />
        public override bool Contains(double x, double y, double tolerance)
        {
            return DataAt(x, y).Item1 != null;
        }

        /// <inheritdoc />
        internal override double TooltipX(TItem item)
        {
            if (Chart == null)
            {
                return 0;
            }

            var category = ComposeCategory(Chart.ValueScale);

            return GetBarRight(item, BarIndex, category, StackedBarSeries);
        }

        /// <inheritdoc />
        protected override string TooltipValue(TItem item)
        {
            if (Chart == null)
            {
                return "";
            }

            return Chart.ValueAxis.Format(Chart.CategoryScale, Chart.CategoryScale.Value(Value(item)));
        }

        /// <inheritdoc />
        protected override string TooltipTitle(TItem item)
        {
            if (Chart == null)
            {
                return "";
            }

            var category = Category(Chart.ValueScale);
            return Chart.CategoryAxis.Format(Chart.ValueScale, Chart.ValueScale.Value(category(item)));
        }

        /// <inheritdoc />
        public override (object, Point) DataAt(double x, double y)
        {
            if (Chart == null)
            {
                return (default!, new Point());
            }

            var category = ComposeCategory(Chart.ValueScale);
            var barSeries = VisibleBarSeries;
            var barIndex = BarIndex;

            foreach (var data in Items)
            {
                var startY = GetBarTop(data, category);
                var endY = startY + BandHeight;
                var dataX = GetBarRight(data, barIndex, category, StackedBarSeries);
                if (Chart == null)
                {
                    continue;
                }

                var x0 = GetBarLeft(data, barIndex, category, StackedBarSeries);
                var startX = Math.Min(dataX, x0);
                var endX = Math.Max(dataX, x0);

                if (startX <= x && x <= endX && startY <= y && y <= endY)
                {
                    return (data!, new Point() { X = x, Y = y });
                }
            }

            return (default!, new Point());
        }

        /// <inheritdoc />
        internal override double TooltipY(TItem item)
        {
            return GetBarTop(item) + BarHeight / 2;
        }

        /// <inheritdoc />
        public override IEnumerable<ChartDataLabel> GetDataLabels(double offsetX, double offsetY)
        {
            return GetDataLabels(offsetX, offsetY, DataLabelPosition.Auto);
        }

        /// <inheritdoc />
        public override IEnumerable<ChartDataLabel> GetDataLabels(double offsetX, double offsetY, DataLabelPosition position)
        {
            if (Chart == null)
            {
                return Enumerable.Empty<ChartDataLabel>();
            }

            var category = ComposeCategory(Chart.ValueScale);
            var list = new List<ChartDataLabel>();
            var barIndex = BarIndex;
            var stackedBarSeries = StackedBarSeries;
            const double gap = 16;
            const double inset = 8;

            foreach (var data in Items)
            {
                var left = GetBarLeft(data, barIndex, category, stackedBarSeries);
                var right = GetBarRight(data, barIndex, category, stackedBarSeries);
                var value = Value(data);
                var end = value < 0 ? left : right;
                var center = left + (right - left) / 2;
                var anchorY = TooltipY(data);
                var sign = Math.Sign(end - center);

                var (x, y, textAnchor) = position switch
                {
                    DataLabelPosition.Top => (center, anchorY - gap, "middle"),
                    DataLabelPosition.Bottom => (center, anchorY + gap, "middle"),
                    DataLabelPosition.Inside => (end - inset * sign, anchorY, sign < 0 ? "start" : sign > 0 ? "end" : "middle"),
                    _ => (center, anchorY, "middle"),
                };

                list.Add(new ChartDataLabel
                {
                    Position = new Point() { X = x + offsetX, Y = y + offsetY },
                    Anchor = new Point() { X = center, Y = anchorY },
                    Value = value,
                    TextAnchor = textAnchor,
                    Text = Chart.ValueAxis.Format(Chart.CategoryScale, value)
                });
            }

            return list;
        }
    }
}