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

namespace Connected.Models;
public class InputBase : ComponentBase
{
	[Parameter]
	public string Class { get; set; } = string.Empty;

	public string InputFieldClassList
	{
		get
		{
			return new CssBuilder("form-group")
				.AddClass("error", ErrorText.Length > 0)
				.AddClass(Class)
				.Build();
		}
	}

	/// <summary>
	/// Show clear button.
	/// </summary>
	[Parameter]
	public bool Clearable { get; set; } = true;

	/// <summary>
	/// Required property
	/// </summary>
	[Parameter]
	public bool Required { get; set; } = false;

	public Dictionary<string, object> InputAttributes { get; set; } = new();

	/// <summary>
	/// Disable input component
	/// Options: true or false
	/// Default: false
	/// </summary>
	[Parameter]
	public bool Disabled { get; set; } = false;

	/// <summary>
	/// Make input component readonly
	/// Options: true or false
	/// Default: false
	/// </summary>
	[Parameter]
	public bool Readonly { get; set; } = false;

	/// <summary>
	/// The show input character counter
	/// </summary>
	[Parameter]
	public bool ShowCharacterCounter { get; set; }

	/// <summary>
	/// 
	/// </summary>
	[Parameter]
	public string ErrorText { get; set; } = string.Empty;

	public bool IsError
	{
		get
		{
			return !string.IsNullOrEmpty(ErrorText);
		}
	}

	/// <summary>
	/// Fired when the text value changes.
	/// </summary>
	[Parameter] public EventCallback<string> TextChanged { get; set; }
	public string Text { get; set; }

	protected virtual async Task SetTextAsync(string text)
	{
		if (Text == text)
			return;

		Text = text;
		await TextChanged.InvokeAsync(text);
	}


	private string _helperText = string.Empty;

	[Parameter]
	public string HelperText
	{
		get
		{
			return _helperText;
		}

		set
		{
			_helperText = value;
		}
	}

	public bool IsHelperText
	{
		get
		{
			return !string.IsNullOrEmpty(HelperText);
		}
	}

	[Parameter]
	public string Label { get; set; } = string.Empty;
	public bool IsLabel
	{
		get
		{
			return !string.IsNullOrEmpty(Label);
		}
	}

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

	protected override void OnInitialized()
	{
		if (InputAttributes is null) InputAttributes = new();
	}
}
