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

namespace Connected.Components;

public partial class CarouselItem : UIComponent, IDisposable
{
	protected string Classname =>
				new CssBuilder("carousel-item")
					 .AddClass($"carousel-item-{Color.ToDescription()}")
					 .AddClass("carousel-item-exit", !_disposed && Parent.LastContainer == this)

					 .AddClass("carousel-transition-fade-in", !_disposed && Transition == Transition.Fade && Parent.SelectedContainer == this)
					 .AddClass("carousel-transition-fade-out", !_disposed && Transition == Transition.Fade && Parent.LastContainer == this)

					 .AddClass("carousel-transition-slide-next-enter", !_disposed && Transition == Transition.Slide && RightToLeft == false && Parent.SelectedContainer == this && Parent._moveNext)
					 .AddClass("carousel-transition-slide-next-exit", !_disposed && Transition == Transition.Slide && RightToLeft == false && Parent.LastContainer == this && Parent._moveNext)

					 .AddClass("carousel-transition-slide-prev-enter", !_disposed && Transition == Transition.Slide && RightToLeft == false && Parent.SelectedContainer == this && !Parent._moveNext)
					 .AddClass("carousel-transition-slide-prev-exit", !_disposed && Transition == Transition.Slide && RightToLeft == false && Parent.LastContainer == this && !Parent._moveNext)

					 .AddClass("carousel-transition-slide-next-rtl-enter", !_disposed && Transition == Transition.Slide && RightToLeft == true && Parent.SelectedContainer == this && Parent._moveNext)
					 .AddClass("carousel-transition-slide-next-rtl-exit", !_disposed && Transition == Transition.Slide && RightToLeft == true && Parent.LastContainer == this && Parent._moveNext)

					 .AddClass("carousel-transition-slide-prev-rtl-enter", !_disposed && Transition == Transition.Slide && RightToLeft == true && Parent.SelectedContainer == this && !Parent._moveNext)
					 .AddClass("carousel-transition-slide-prev-rtl-exit", !_disposed && Transition == Transition.Slide && RightToLeft == true && Parent.LastContainer == this && !Parent._moveNext)

					 .AddClass("carousel-transition-none", !_disposed && Transition == Transition.None && Parent.SelectedContainer != this)

					 .AddClass(CustomTransitionEnter, !_disposed && Transition == Transition.Custom && Parent.SelectedContainer == this && Parent.SelectedContainer == this)
					 .AddClass(CustomTransitionExit, !_disposed && Transition == Transition.Custom && Parent.LastContainer == this && Parent.LastContainer == this)

					 .AddClass(AdditionalClassList)
					 .Build();

	[Parameter]
	[Category(CategoryTypes.Carousel.Behavior)]
	public RenderFragment ChildContent { get; set; }

	[CascadingParameter] protected internal ItemsControlBase<CarouselItem> Parent { get; set; }

	[CascadingParameter(Name = "RightToLeft")] public bool RightToLeft { get; set; }

	/// <summary>
	/// The color of the component. It supports the theme colors.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.Carousel.Appearance)]
	public ThemeColor Color { get; set; } = ThemeColor.Default;

	/// <summary>
	/// The transition effect of the component.
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.Carousel.Appearance)]
	public Transition Transition { get; set; } = Transition.Slide;

	/// <summary>
	/// The name of custom transition on entrance time
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.Carousel.Appearance)]
	public string CustomTransitionEnter { get; set; }

	/// <summary>
	/// The name of custom transition on exiting time
	/// </summary>
	[Parameter]
	[Category(CategoryTypes.Carousel.Appearance)]
	public string CustomTransitionExit { get; set; }


	public bool IsVisible => Parent != null && (Parent.LastContainer == this || Parent.SelectedIndex == Parent.Items.IndexOf(this));


	protected override Task OnInitializedAsync()
	{
		Parent?.AddItem(this);
		return Task.CompletedTask;
	}

	private bool _disposed = false;
	public void Dispose()
	{
		_disposed = true;
		Parent?.Items.Remove(this);
	}

}
