@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.ParagraphViewModel>
@using Dynamicweb.Frontend.Navigation

@{
    var navigationSettings = new NavigationSettings();
    navigationSettings.IncludeFoldersAndHidden = true;
    navigationSettings.ExpandMode = ExpandMode.PathOnly;

    NavigationTreeViewModel navigationModel = Navigation.GetNavigationViewModel(navigationSettings);
}

@if (navigationModel.Nodes.Count() > 0)
{
	string menuId = "menu_breadcrumb";
	bool showProductInBreadcrumb = Model.Item.GetBoolean("ShowProductInBreadcrumb");
	var breadCrumbNavigationTree = new Stack<Dynamicweb.Frontend.Navigation.NavigationTreeNodeViewModel>(navigationModel.Nodes);

	<nav aria-label="breadcrumb">
		<ol class="breadcrumb mb-0 @menuId" style="--bs-breadcrumb-divider: '>'; --bs-breadcrumb-item-padding-x: 1rem; --bs-breadcrumb-font-size: .875em;">
			@while (breadCrumbNavigationTree.Count > 0)
			{
				var currentNode = breadCrumbNavigationTree.Pop();
				if (currentNode.ShowInBreadcrumb)
				{
					if (!string.IsNullOrEmpty(currentNode.Link) && (!currentNode.IsActive || showProductInBreadcrumb && !string.IsNullOrEmpty(Dynamicweb.Context.Current?.Request.QueryString["ProductID"])))
					{
						<li class="breadcrumb-item">
							<a href="@currentNode.Link">
								@currentNode.Name
							</a>
						</li>
					}
					else
					{
						string isActive = currentNode.IsActive && string.IsNullOrEmpty(Dynamicweb.Context.Current?.Request.QueryString["ProductID"]) ? "active" : string.Empty;
						string currentPage = currentNode.IsActive && string.IsNullOrEmpty(Dynamicweb.Context.Current?.Request.QueryString["ProductID"]) ? "aria-current=\"page\"" : string.Empty;

						<li class="breadcrumb-item @isActive" @currentPage>
							@currentNode.Name
						</li>
					}
				}
				foreach (var nextNode in currentNode.Nodes)
				{
					breadCrumbNavigationTree.Push(nextNode);
				}
			}

			@if (showProductInBreadcrumb && !string.IsNullOrEmpty(Dynamicweb.Context.Current?.Request.QueryString["ProductID"]))
			{
				string productid = Dynamicweb.Context.Current?.Request.QueryString["ProductID"] ?? string.Empty;
				string variantid = Dynamicweb.Context.Current?.Request.QueryString["VariantID"] ?? string.Empty;
				var product = Dynamicweb.Ecommerce.Services.Products.GetProductById(productid, variantid, Dynamicweb.Ecommerce.Common.Context.LanguageID);
				if (product != null)
				{
					<li class="breadcrumb-item active" aria-current="page">
						@product.Name
					</li>
				}
			}
		</ol>
	</nav>
}
else if (Pageview.IsVisualEditorMode)
{
	<div class="alert alert-dark m-0" role="alert">
		<span>@Translate("Breadcrumb navigation: The navigation will be shown here, if any")</span>
	</div>
}
