@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 groupImage = productList?.Group?.Assets != null ? productList.Group.Assets.FirstOrDefault(x => x.Name == "LargeImage")?.Value ?? string.Empty : string.Empty;
	if (groupImage.StartsWith("/") && !groupImage.StartsWith("/Files", StringComparison.OrdinalIgnoreCase))
	{
		groupImage = $"/Files{groupImage}";
	}

	string groupName = productList?.Group?.Name != null ? productList.Group.Name : string.Empty;

	string ratio = Model.Item.GetRawValueString("ImageAspectRatio", string.Empty);
	ratio = ratio != "0" ? ratio : string.Empty;
	string ratioCssClass = ratio != string.Empty && ratio != "fill" ? " ratio" : string.Empty;
	string ratioVariable = ratio != string.Empty ? "style=\"--bs-aspect-ratio: " + ratio + "\"" : string.Empty;
	string fillClass = ratio == "fill" ? " h-100" : "";

	var parms = new Dictionary<string, object>();
	parms.Add("loading", "lazy");
	parms.Add("columns", Model.GridRowColumnCount);

	if (ratio == "fill") {
		parms.Add("cssClass", "w-100 h-100");
	}
	else
	{
		parms.Add("cssClass", "mw-100 mh-100");
	}

	parms.Add("style", string.Empty);
	parms.Add("alt", groupName);

	if (!string.IsNullOrEmpty(groupImage)) {
		<div class="item_@Model.Item.SystemName.ToLower()" data-dw-colorscheme="@Model.ColorScheme?.Id">
			<figure class="m-0@(ratioCssClass)@(fillClass)" @ratioVariable>
				@RenderPartial("Components/Image.cshtml", new FileViewModel { Path = groupImage }, parms)
			</figure>
		</div>
	} else if (Pageview.IsVisualEditorMode) {
		<div class="alert alert-dark m-0" role="alert">
			<span>@Translate("Product list group image: The image will be shown here, if any")</span>
		</div>
	}
} else if (Pageview.IsVisualEditorMode)
{
	<div class="alert alert-dark m-0" role="alert">
		<span>@Translate("Product list group image: The image will be shown here, if any")</span>
	</div>
}

