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

namespace Connected.Components;

public partial class DrawerContainer : UIComponent
{
	protected bool Fixed { get; set; } = false;
	private List<Drawer> _drawers = new();

	#region Event callbacks

	internal void FireDrawersChanged() => StateHasChanged();

	#endregion

	#region Content placeholders

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

	[Parameter]
	public RenderFragment ChildContent { get; set; }

	private string GetDrawerClass(Drawer drawer)
	{
		if (drawer == null)
			return string.Empty;

		var className = $"drawer-{(drawer.Open ? "open" : "close")}-{drawer.Variant.ToDescription()}";
		if (drawer.Variant is DrawerVariant.Responsive or DrawerVariant.Mini)
		{
			className += $"-{drawer.Breakpoint.ToDescription()}";
		}
		className += $"-{drawer.GetPosition()}";

		className += $" mud-drawer-{drawer.GetPosition()}-clipped-{drawer.ClipMode.ToDescription()}";

		return className;
	}

	private Drawer FindLeftDrawer()
	{
		var anchor = RightToLeft ? Anchor.End : Anchor.Start;
		return _drawers.FirstOrDefault(d => d.Anchor == anchor || d.Anchor == Anchor.Left);
	}

	private Drawer FindRightDrawer()
	{
		var anchor = RightToLeft ? Anchor.Start : Anchor.End;
		return _drawers.FirstOrDefault(d => d.Anchor == anchor || d.Anchor == Anchor.Right);
	}

	private Drawer FindLeftMiniDrawer()
	{
		var anchor = RightToLeft ? Anchor.End : Anchor.Start;
		return _drawers.FirstOrDefault(d => d.Variant == DrawerVariant.Mini && (d.Anchor == anchor || d.Anchor == Anchor.Left));
	}

	private Drawer FindRightMiniDrawer()
	{
		var anchor = RightToLeft ? Anchor.Start : Anchor.End;
		return _drawers.FirstOrDefault(d => d.Variant == DrawerVariant.Mini && (d.Anchor == anchor || d.Anchor == Anchor.Right));
	}

	#endregion

	#region Styling properties

	protected virtual CssBuilder CompiledClassList
	{
		get
		{
			return new CssBuilder()
			  .AddClass(ClassList);
		}
	}

	/*
	protected virtual string Classname =>
	new CssBuilder()
		/*.AddClass(GetDrawerClass(FindLeftDrawer()))
		.AddClass(GetDrawerClass(FindRightDrawer()))
		.AddClass(ClassList)
	.Build();

	protected string Stylename =>
	new StyleBuilder()
		.AddStyle(StyleList)
		.AddStyle("--mud-drawer-width-left", GetDrawerWidth(FindLeftDrawer()), !string.IsNullOrEmpty(GetDrawerWidth(FindLeftDrawer())))
		.AddStyle("--mud-drawer-width-right", GetDrawerWidth(FindRightDrawer()), !string.IsNullOrEmpty(GetDrawerWidth(FindRightDrawer())))
		.AddStyle("--mud-drawer-width-mini-left", GetMiniDrawerWidth(FindLeftMiniDrawer()), !string.IsNullOrEmpty(GetMiniDrawerWidth(FindLeftMiniDrawer())))
		.AddStyle("--mud-drawer-width-mini-right", GetMiniDrawerWidth(FindRightMiniDrawer()), !string.IsNullOrEmpty(GetMiniDrawerWidth(FindRightMiniDrawer())))
	.Build();*/

	protected virtual StyleBuilder CompiledStyleList
	{
		get
		{
			return new StyleBuilder()
				.AddStyle(StyleList);
			//.AddStyle("width", Width, !string.IsNullOrWhiteSpace(Width) && !Fixed)
			//.AddStyle("--mud-drawer-width", Width, !string.IsNullOrWhiteSpace(Width) && (!Fixed || Variant == DrawerVariant.Temporary))
			//.AddStyle("height", Height, !string.IsNullOrWhiteSpace(Height))
			//.AddStyle("--mud-drawer-content-height",string.IsNullOrWhiteSpace(Height) ? _height.ToPx() : Height,Anchor == Anchor.Bottom || Anchor == Anchor.Top)
			//.AddStyle("visibility", "hidden", string.IsNullOrWhiteSpace(Height) && _height == 0 && (Anchor == Anchor.Bottom || Anchor == Anchor.Top));

		}
	}

	/// <summary>
	/// A space separated list of class names, added on top of the default class list.
	/// </summary>
	[Parameter]
	public string? ClassList { get; set; }

	/// <summary>
	/// A space separated list of class names, added on top of the default class list.
	/// </summary>
	[Parameter]
	public string? StyleList { get; set; }

	#endregion


	internal void Add(Drawer drawer)
	{
		if (Fixed && !drawer.Fixed)
			return;

		_drawers.Add(drawer);
		StateHasChanged();
	}

	internal void Remove(Drawer drawer)
	{
		_drawers.Remove(drawer);
		StateHasChanged();
	}
}
