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

namespace Connected.Components;
public partial class FormWizardStep
{
	#region Parameters

	[CascadingParameter]
	public FormWizard Parent { get; set; }

	/// <summary>
	/// Unique name of the step (used for properly identifying steps)
	/// </summary>
	[Parameter]
	public string? Id { get; set; } = Guid.NewGuid().ToString();

	/// <summary>
	/// Text shown inside the button
	/// Options: any string variable
	/// Default: string.Empty
	/// </summary>
	[Parameter]
	public RenderFragment? ChildContent { get; set; }

	/// <summary>
	/// User defined custom class added on top of default generated classes
	/// Options: any user defined string with class names divided by space
	/// Default: string.Empty
	/// </summary>
	[Parameter]
	public string Class { get; set; } = string.Empty;

	/// <summary>
	/// User defined custom style
	/// Options: any valid CSS style
	/// Default: string.Empty
	/// </summary>
	[Parameter]
	public string Style { get; set; } = string.Empty;

	public bool Active { get; set; } = false;
	public bool IsNext { get; set; } = false;
	public bool IsPrevious { get; set; } = false;
	public bool Completed { get; set; } = false;

	public string DotClass
	{
		get
		{
			return new CssBuilder("dot")
				.AddClass("completed", Completed)
				.AddClass("next", Active)
				.Build();
		}
	}

	#endregion


	#region Styling

	public string StyleList
	{
		get
		{
			return new StyleBuilder()
				.AddStyle("display", "none", (!Active && !IsNext && !IsPrevious)) //Workarround for more than 2 steps where steps didnt hide after
				.AddStyle(Style)
				.Build();
		}
	}


	/// <summary>
	/// Generated class list for button based on user parameters
	/// </summary>
	public string ClassList
	{
		get
		{
			return new CssBuilder("form-step")
				.AddClass("next", IsNext)
				.AddClass("previous", IsPrevious)
				.AddClass(Class)
				.Build();
		}
	}

	#endregion

	#region Lifecycle

	private bool ItemExists()
	{
		if (Parent.Steps.Where(step => step.Id.Equals(this.Id)).Count() > 0) return true;
		return false;
	}


	protected override async Task OnParametersSetAsync()
	{
		if (!ItemExists())
			Parent.Steps.Add(this);
		await base.OnInitializedAsync();
	}

	#endregion
}