using Connected.Enums;
using Connected.Utilities;
using Microsoft.AspNetCore.Components;
using System.Reflection.Metadata;

namespace Connected.Components;
public partial class FormWizard
{

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

	/// <summary>
	/// Steps of type FormWizardStep
	/// </summary>
	[Parameter]
	public List<FormWizardStep> Steps { get; set; } = new();

	/// <summary>
	/// EventCallback for 'Finish' button click
	/// </summary>
	[Parameter]
	public EventCallback OnFinishedClick { get; set; }

	/// <summary>
	/// EventCallback for 'Cancel' button click
	/// </summary>
	[Parameter]
	public EventCallback OnCancelClick { get; set; }

	[Parameter]
	public string Id { get; set; } = Guid.NewGuid().ToString();

	private string NextBtnText = "Next";
	private string PreviousBtnText = "Previous";

	private bool WizardFinished { get; set; } = false;

	private FinishedState FinishedState { get; set; } = FinishedState.Unfinished;

	[Parameter]
	public int ActiveIndex
	{
		get
		{
			return _activeStepIndex;
		}
		set
		{
			if (value <= StepCount || value >= 0) //if value is greater than total StepCount or less than 0 first element becomes the active alament
			{
				_activeStepIndex = value;
			}
		}
	}
	private int _activeStepIndex = 0;

	private int PreviousIndex
	{
		get
		{
			if (ActiveIndex > 0)
				return ActiveIndex - 1;
			return ActiveIndex;
		}
	}

	private int NextIndex
	{
		get
		{
			try
			{
				if (ActiveIndex < StepCount - 1)
					return ActiveIndex + 1;
				return ActiveIndex;
			}
			catch
			{
				return ActiveIndex;
			}
		}
	}

	private int StepCount
	{
		get
		{
			return Steps.Count;
		}
	}

	private async Task NextSlide()
	{
		if (NextBtnText.ToLower().Equals("finish"))
		{
			FinishedState = FinishedState.Finished;
			Steps[ActiveIndex].Completed = true;
			WizardFinished = true;
			await OnFinishedClick.InvokeAsync();
		}
		if (FinishedState.Equals(FinishedState.Unfinished))
		{
			Steps[ActiveIndex].Completed = true;
			Steps[ActiveIndex].Active = false;
			Steps[PreviousIndex].IsPrevious = false;
			Steps[ActiveIndex].IsNext = false;
			ActiveIndex = NextIndex;
			Steps[ActiveIndex].IsNext = true;
			Steps[ActiveIndex].Active = true;
			Steps[PreviousIndex].IsPrevious = true;

			if (ActiveIndex == NextIndex)
				NextBtnText = "Finish";
			else
				NextBtnText = "Next";
		}
	}

	private void PreviousSlide()
	{
		if (ActiveIndex > 0)
		{
			Steps[ActiveIndex].Active = false;
			Steps[PreviousIndex].IsPrevious = false;
			Steps[ActiveIndex].IsNext = false;
			ActiveIndex = PreviousIndex;
			Steps[ActiveIndex].IsNext = true;
			Steps[ActiveIndex].Active = true;
			Steps[PreviousIndex].IsPrevious = true;

			if (ActiveIndex == NextIndex)
				NextBtnText = "Finish";
			else
				NextBtnText = "Next";
		}
		if (ActiveIndex == 0)
		{
			ResetAllChildren();
			Steps[ActiveIndex].Active = true;
		}
	}

	private async Task CancelClick()
	{
		await OnCancelClick.InvokeAsync();
	}

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

	private string ClassList
	{
		get
		{
			return new CssBuilder("form-wizard")
				.AddClass(Class)
				.Build();
		}
	}

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

	private string StyleList
	{
		get
		{
			return new StyleBuilder()
				.AddStyle(Style)
				.Build();
		}
	}

	private void ResetValuesForChild(int ChildIndex)
	{
		Steps[ChildIndex].Active = false;
		Steps[ChildIndex].IsNext = false;
		Steps[ChildIndex].IsPrevious = false;
	}

	private void ResetAllChildren()
	{
		foreach (var step in Steps)
		{
			step.Active = false;
			step.IsNext = false;
			step.IsPrevious = false;
		}
	}

	private void InitializeSteps()
	{
		ResetAllChildren();
		Steps[ActiveIndex].Active = true;

		if (ActiveIndex != 0)
		{
			Steps[ActiveIndex].IsNext = true;
			Steps[PreviousIndex].IsPrevious = true;
		}
		StateHasChanged();
	}

	/*protected override async Task OnParametersSetAsync()
	{
		if (Steps is null) Steps = new();
			Steps.Clear();
		InitializeSteps();

		await base.OnParametersSetAsync();
	}*/

	protected override async Task OnAfterRenderAsync(bool firstRender)
	{
		if (firstRender)
		{
			InitializeSteps();
		}
		await base.OnAfterRenderAsync(firstRender);
	}

}
