using System.Diagnostics.CodeAnalysis;
using Connected.Annotations;
using Connected.Extensions;
using Connected.Utilities;
using Microsoft.AspNetCore.Components;

namespace Connected.Components;

public partial class ProgressCircular : UIComponent
{
   private const int _magicNumber = 126; // weird, but required for the SVG to work

   protected string DivClassname =>
       new CssBuilder("progress-circular")
           .AddClass($"{Color.ToDescription()}-text")
           .AddClass($"progress-{Size.ToDescription()}")
           .AddClass($"progress-indeterminate", Indeterminate)
           .AddClass($"progress-static", !Indeterminate)
           .AddClass(AdditionalClassList)
           .Build();

   protected string SvgClassname =>
       new CssBuilder("progress-circular-circle")
           .AddClass($"progress-indeterminate", Indeterminate)
           .AddClass($"progress-static", !Indeterminate)
           .Build();

   /// <summary>
   /// The color of the component. It supports the theme colors.
   /// </summary>
   [Parameter]
   [Category(CategoryTypes.ProgressCircular.Appearance)]
   public ThemeColor Color { get; set; } = ThemeColor.Default;

   /// <summary>
   /// The size of the component.
   /// </summary>
   [Parameter]
   [Category(CategoryTypes.ProgressCircular.Appearance)]
   public Size Size { get; set; } = Size.Medium;

   /// <summary>
   /// Constantly animates, does not follow any value.
   /// </summary>
   [Parameter]
   [Category(CategoryTypes.ProgressCircular.Behavior)]
   public bool Indeterminate { get; set; }

   [Parameter]
   [Category(CategoryTypes.ProgressCircular.Behavior)]
   public double Min { get; set; } = 0.0;

   [Parameter]
   [Category(CategoryTypes.ProgressCircular.Behavior)]
   public double Max { get; set; } = 100.0;

   private int _svgValue;
   private double _value;

   [Parameter]
   [Category(CategoryTypes.ProgressCircular.Behavior)]
   public double Value
   {
      get => _value;
      set
      {
         if (_value != value)
         {
            _value = value;
            _svgValue = ToSvgValue(_value);
            StateHasChanged();
         }
      }
   }

   private int ToSvgValue(double in_value)
   {
      var value = Math.Min(Math.Max(Min, in_value), Max);
      // calculate fraction, which is a value between 0 and 1
      var fraction = (value - Min) / (Max - Min);
      // now project into the range of the SVG value (126 .. 0)
      return (int)Math.Round(_magicNumber - _magicNumber * fraction);
   }

   [Parameter]
   [Category(CategoryTypes.ProgressCircular.Appearance)]
   public int StrokeWidth { get; set; } = 3;

   protected override void OnInitialized()
   {
      base.OnInitialized();
      _svgValue = ToSvgValue(_value);
   }

   #region --> Obsolete Forwarders for Backwards-Compatiblilty

   [ExcludeFromCodeCoverage]
   [Obsolete("Use Min instead.", true)]
   [Parameter] public double Minimum { get => Min; set => Min = value; }

   [ExcludeFromCodeCoverage]
   [Obsolete("Use Max instead.", true)]
   [Parameter] public double Maximum { get => Max; set => Max = value; }

   #endregion

}
