using Connected.Models;
using Microsoft.AspNetCore.Components;

namespace Connected.Components;
public partial class TextInput : InputBase
{
	#region Parameters

	/// <summary>
	/// Setter for the TextInput if input is password field (for hiding characters)
	/// Options: true or false
	/// Default: false;
	/// </summary>
	[Parameter]
	public bool IsPassword { get; set; } = false;

	/// <summary>
	/// String for the component to use if TextInput is password field
	/// </summary>
	private string InputType => IsPassword ? "password" : "text";

	/// <summary>
	/// Number of rows
	/// </summary>
	[Parameter]
	public int NumOfRows
	{
		get
		{
			return _numberOfLines;
		}
		set
		{
			_numberOfLines = Math.Max(1, value);
		}
	}
	private int _numberOfLines = 1;

	/// <summary>
	/// Value of the TextInput. Used for @bind-Value
	/// </summary>
	[Parameter]
	public string Value { get; set; } = string.Empty;

	#endregion

	#region Events, Methods

	/// <summary>
	/// Event triggered when value changes
	/// </summary>
	[Parameter]
	public EventCallback<string> ValueChanged { get; set; }

	/// <summary>
	/// Method that triggers oninput -> when value inside the component changes
	/// </summary>
	/// <param name="args"></param>
	/// <returns></returns>
	private async Task ChangeValueAsync(ChangeEventArgs args)
	{
		await ValueChanged.InvokeAsync(args?.Value?.ToString());
	}

	/// <summary>
	/// Clear the value of the TextInput
	/// </summary>
	/// <returns></returns>
	private async Task Clear()
	{
		await ValueChanged.InvokeAsync(string.Empty);
	}

	#endregion
}