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

namespace Connected.Components;
public partial class InputNumber<NumberType>:InputBase where NumberType : INumber<NumberType>
{ 

	[Parameter]
	public double Step { get; set; }

	[Parameter]
	public NumberType Value { get; set; }

	[Parameter]
	public int DecimalPlaces { get; set; } =0;


	[Parameter]
    public EventCallback<NumberType> ValueChanged { get; set; }

    public async Task ChangeValueAsync(ChangeEventArgs args)
    {
		if (args.Value is not null)
		{
			if (args.Value.ToString().Length > 0)
			{
				await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(args.Value, typeof(NumberType)));
			}
			else
			{
				await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(0, typeof(NumberType)));
			}
		}
	}

	private async Task Clear()
	{
		await ValueChanged.InvokeAsync((NumberType)Convert.ChangeType(0, typeof(NumberType)));
	}

	#region Lifecycle

	protected override async Task OnParametersSetAsync()
    {
		if (typeof(NumberType).Name.ToLower().Contains("int"))
		{
			if (Step-(int)Step>0) Step= (int)Step;
			if (Step < 1) Step = 1;
		}
		
		await base.OnParametersSetAsync();
    }

    #endregion
}