using Connected.Utilities;
using Microsoft.AspNetCore.Components;
using System.Numerics;

namespace Connected.Components
{
	public partial class NumberStepper<NumberType> where NumberType : INumber<NumberType>

	{
		[Parameter]
		public string Class { get; set; } = string.Empty;

		/// <summary>
		/// Increase 'Value' for the 'Step'
		/// </summary>
		/// <returns>'Value' increased for the 'Step' parameter</returns>
		private async Task StepUp()
		{
			try
			{
				var num = Helper.ConvertToType<double>(Value);

				num += Step;

				Value=Helper.ConvertToType<NumberType>(num);

			}
			catch
			{
				Value = default;
			}

			await ValueChanged.InvokeAsync(Value);
		}

		/// <summary>
		/// Decrease 'Value' for the 'Step'
		/// </summary>
		/// <returns>'Value' decreased for the 'Step' parameter</returns>
		private async Task StepDown()
		{
			try
			{
				var num = Helper.ConvertToType<double>(Value);

				num -= Step;

				Value = Helper.ConvertToType<NumberType>(num);

			}
			catch
			{
				Value = default;
			}

			await ValueChanged.InvokeAsync(Value);
		}

		[Parameter]
		public NumberType? Value
		{
			get
			{
				
					try
					{
						return Helper.ConvertToType<NumberType>(_value);
					}
					catch
					{
						return default;
					}
			}
			set
			{
				_value = Helper.ConvertToType<double>(value);
			}
		}
		private double _value { get; set; } = 0;

		/// <summary>
		/// Value change event
		/// </summary>
		[Parameter]
		public EventCallback<NumberType> ValueChanged { get; set; }

		[Parameter]
		public double Step { get; set; } = 1;
 
		public async Task Change(ChangeEventArgs args)
		{
			if (args.Value is not null)
				Value = (Helper.ConvertToType<NumberType>(args.Value));

			await ValueChanged.InvokeAsync(Value);
		}
	}
}