@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Ecommerce.ProductCatalog.ProductViewModel>
@using Dynamicweb.Ecommerce.ProductCatalog

@functions {
	Dictionary<string, object> favoriteParameters { get; set; }
}

@{
	var product = Model;
	string anonymousUsersLimitations = Pageview.AreaSettings.GetRawValueString("AnonymousUsers", "");
	bool anonymousUser = Pageview.User == null;
	bool hidePrice = anonymousUsersLimitations.Contains("price") && anonymousUser;

	string ratio = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("ImageAspectRatio")) ? Dynamicweb.Context.Current.Request.Form.Get("ImageAspectRatio") : "";
	string ratioCssClass = ratio != "" ? "ratio" : "";
	string ratioVariable = ratio != "" ? "--bs-aspect-ratio: " + ratio : "";

	string link = product.GetProductLink(GetPageIdByNavigationTag("Shop"), false);

	string imagePath = product?.DefaultImage.Value.ToString() ?? string.Empty;
	string getImagePath = $"/Admin/Public/GetImage.ashx?image={imagePath}&width=350&format=webp";
	bool localImagePath = imagePath.StartsWith("/Files/", StringComparison.OrdinalIgnoreCase);
	imagePath = localImagePath ? getImagePath : imagePath;

	if (!anonymousUser)
	{
		<div class="position-absolute top-0 end-0 my-3 me-4" style="z-index: 3">
			@{@RenderPartial("Components/ToggleFavorite.cshtml", product, favoriteParameters)}
		</div>
	}

	<a href="@link" class="text-decoration-none d-block h-100">
		<div class="h-100 d-flex flex-column justify-content-between">
			<div class="@(ratioCssClass) position-relative" style="@ratioVariable">
				<img loading="lazy" decoding="async" src="@imagePath" class="h-100 w-100" style="object-fit: contain;" alt="@product.Name">
			</div>
			<div class="flex-fill d-flex flex-column justify-content-between">
				<h3 class="h6 opacity-85">@product.Name @product.VariantName</h3>

				@if (!hidePrice)
				{
					<div>
						<p class="h6 m-0">
							@if (product.HasDiscount())
							{
								<span class="text-decoration-line-through opacity-75 me-1">
									@product.PriceBeforeDiscount.PriceFormatted
								</span>
							}

							<span class="text-price fw-bold">@product.Price.PriceFormatted</span>
						</p>
						@if (product.Price.TryGetVatLabel(out string vatLabel)) {
							<small class="opacity-85 fst-normal order-3">@Translate(vatLabel)</small>
						}
					</div>
				}
			</div>
		</div>
	</a>
}
