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

@{
	ProductListViewModel productList = null;
	if (Dynamicweb.Context.Current.Items.Contains("ProductList"))
	{
		productList = (ProductListViewModel)Dynamicweb.Context.Current.Items["ProductList"];
	}
}

@if (productList is object)
{
	string layout = Model.Item.GetRawValueString("Layout", "align-middle-center-text-center");
	string alignment = "";

	string searchParameter = Dynamicweb.Context.Current.Request.GetString("q");

	bool hideTitle = Model.Item.GetBoolean("HideGroupTitle");
	string titleFontSize = Model.Item.GetRawValueString("TitleFontSize", "display-4");

	string groupName = productList?.Group?.Name != null ? productList.Group.Name : "";
	string headerName = searchParameter != "" ? Translate("Search results for") + ": " + searchParameter : groupName;

	bool hideDescription = Model.Item.GetBoolean("HideGroupDescription");
	string groupDescription = productList?.Group?.Description != null ? productList.Group.Description : "";
	string descriptionFontSize = Model.Item.GetRawValueString("DescriptionFontSize", "fs-6");

	string groupImage = productList?.Group?.Assets != null ? productList.Group.Assets.FirstOrDefault(x => x.Name == "LargeImage").Value : "";
	if (groupImage.StartsWith("/") && !groupImage.StartsWith("/Files", StringComparison.OrdinalIgnoreCase))
	{
		groupImage = $"/Files{groupImage}";
	}

	string posterHeight = Model.Item.GetRawValueString("PosterHeight", "");
	posterHeight = posterHeight == "small" ? "min-vh-25" : posterHeight;
	posterHeight = posterHeight == "medium" ? "min-vh-50" : posterHeight;
	posterHeight = posterHeight == "large" ? "min-vh-75" : posterHeight;

	string maxWidth = Model.Item.GetRawValueString("TextReadability", "max-width-on");
	maxWidth = maxWidth == "max-width-on" ? " mw-75ch" : maxWidth;
	maxWidth = maxWidth == "max-width-off" ? "" : maxWidth;

	if (!string.IsNullOrEmpty(groupImage))
	{
		var parms = new Dictionary<string, object>();
		parms.Add("cssClass", "h-100 w-100");
		parms.Add("columns", Model.GridRowColumnCount);

		switch (layout)
		{
			case "align-top-left-text-left":
				alignment = "text-start justify-content-start align-items-start";
				break;
			case "align-top-center-text-center":
				alignment = "text-center justify-content-center align-items-start";
				break;
			case "align-top-right-text-right":
				alignment = "text-end justify-content-end align-items-start";
				break;
			case "align-middle-left-text-left":
				alignment = "text-start justify-content-start align-items-center";
				break;
			case "align-middle-center-text-center":
				alignment = "text-center justify-content-center align-items-center";
				break;
			case "align-middle-right-text-right":
				alignment = "text-end justify-content-end align-items-center";
				break;
			case "align-bottom-left-text-left":
				alignment = "text-start justify-content-start align-items-end";
				break;
			case "align-bottom-center-text-center":
				alignment = "text-center justify-content-center align-items-end";
				break;
			case "align-bottom-right-text-right":
				alignment = "text-end justify-content-end align-items-end";
				break;
		}

		<div class="position-relative h-100 @(posterHeight) item_@Model.Item.SystemName.ToLower()" data-dw-colorscheme="@Model.ColorScheme?.Id">
			@if (!string.IsNullOrEmpty(groupImage))
			{
				string imageFilter = Model.Item.GetRawValueString("ImageFilter", "");
				imageFilter = imageFilter == "no-filter" ? "" : imageFilter;
				imageFilter = imageFilter == "filter" ? " image-filter" : imageFilter;

				<div class="position-absolute top-0 bottom-0 end-0 start-0@(imageFilter)">
					@RenderPartial("Components/Image.cshtml", new FileViewModel { Path = groupImage }, parms)
				</div>
			}

			@if (!hideTitle || !hideDescription)
			{
				<div data-swift-container class="h-100">
					<div class="h-100 w-100 position-relative d-flex @(alignment)">
						<div class="@(maxWidth)">
							@if (!hideTitle)
							{
								<h1 class="@titleFontSize">@headerName</h1>
							}

							@if (!hideDescription && !string.IsNullOrEmpty(groupDescription))
							{
								<div class="@descriptionFontSize mb-0-last-child">
									@groupDescription
								</div>
							}
						</div>
					</div>
				</div>
			}

		</div>
	}
	else if (Pageview.IsVisualEditorMode)
	{
		<div class="alert alert-dark m-0" role="alert">
			<span>@Translate("Product group poster: The poster paragraph will be shown here, if any")</span>
		</div>
	}
}
else if (Pageview.IsVisualEditorMode)
{
	<div class="alert alert-dark m-0" role="alert">
		<span>@Translate("Product group poster: The poster paragraph will be shown here, if any")</span>
	</div>
}
