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

@functions {
	public ProductViewModel GetProduct()
	{
		ProductViewModel product = null;
		if (Dynamicweb.Context.Current.Items.Contains("ProductDetails"))
		{
			product = (ProductViewModel)Dynamicweb.Context.Current.Items["ProductDetails"];
		}

		return product;
	}
}

@{
	string googleTagManagerID = Pageview.AreaSettings.GetString("GoogleTagManagerID");

	bool allowTracking = true;
	if (CookieManager.IsCookieManagementActive)
	{
		var cookieOptInLevel = CookieManager.GetCookieOptInLevel();
		allowTracking = cookieOptInLevel == CookieOptInLevel.All || (cookieOptInLevel == CookieOptInLevel.Functional && CookieManager.GetCookieOptInCategories().Contains("Statistical"));
	}

	int paragraphId = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("ParagraphId")) ? Convert.ToInt32(Dynamicweb.Context.Current.Request.Form.Get("ParagraphId")) : 0;
	var paragraph = Dynamicweb.Content.Services.Paragraphs?.GetParagraph(paragraphId) ?? null;
	ParagraphInfoViewModel paragraphViewModel = Dynamicweb.Frontend.ContentViewModelFactory.CreateParagraphInfoViewModel(paragraph);

	string sourceType = paragraphViewModel.Item.GetRawValueString("RelationType", "Trending");

	int itemSourcePageId = Convert.ToInt32(paragraphViewModel.Item.GetRawValueString("ListComponentSource", "0"));

	var pageService = new Dynamicweb.Content.PageService();
	itemSourcePageId = itemSourcePageId > 0 && pageService.GetPageOrLanguage(itemSourcePageId, Pageview.AreaID) != null ? pageService.GetPageOrLanguage(itemSourcePageId, Pageview.AreaID).ID : itemSourcePageId;

	var page = itemSourcePageId != 0 ? Dynamicweb.Content.Services.Pages.GetPage(itemSourcePageId) : null;
}


@if (paragraphViewModel != null && page != null)
{
	if (sourceType == "selected")
	{
		string productIds = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.Form.Get("MainProductID")) ? Dynamicweb.Context.Current.Request.Form.Get("MainProductID") : string.Empty;
		productIds = string.IsNullOrEmpty(productIds) ? Dynamicweb.Context.Current.Request.Form.Get("ProductVariantId") : productIds;

		Model.Products = Model.Products.OrderBy(product => productIds.IndexOf(product.Id)).ToList();
	}

	if (Model.Products.Any() || Pageview.IsVisualEditorMode)
	{
		string gridColumnSize = paragraphViewModel.Item.GetRawValueString("GridLayout", "3");
		gridColumnSize = gridColumnSize == "2" ? "g-col-lg-6" : gridColumnSize;
		gridColumnSize = gridColumnSize == "3" ? "g-col-lg-4" : gridColumnSize;
		gridColumnSize = gridColumnSize == "4" ? "g-col-lg-3" : gridColumnSize;
		gridColumnSize = gridColumnSize == "5" ? "g-col-lg-2" : gridColumnSize;
		gridColumnSize = gridColumnSize == "1" ? "g-col-lg-12" : gridColumnSize;

		var pageViewModel = Dynamicweb.Frontend.ContentViewModelFactory.CreatePageInfoViewModel(page);

		<div class="grid gap-3">
			@foreach (ProductViewModel product in Model.Products)
			{
				string link = product.GetProductLink(GetPageIdByNavigationTag("Shop"), false);

				string clickProductLink = string.Empty;
				if (!string.IsNullOrWhiteSpace(googleTagManagerID) && allowTracking)
				{
					clickProductLink = "onclick=\"return clickProductLink('" + @product.Id + "', '" + @Dynamicweb.Core.Encoders.HtmlEncoder.JavaScriptStringEncode(product.Name) + "', '" + @Dynamicweb.Core.Encoders.HtmlEncoder.JavaScriptStringEncode(product.VariantName) + "', '" + @product.Price.CurrencyCode + "', '" + @product.Price.ToStringInvariant() + "', '" + @product.Discount.ToStringInvariant() + "')\"";
				}

				if (Dynamicweb.Context.Current.Items.Contains("ProductDetails"))
				{
					Dynamicweb.Context.Current.Items["ProductDetails"] = product;
				}
				else
				{
					Dynamicweb.Context.Current.Items.Add("ProductDetails", product);
				}

				<div class="d-flex flex-column g-col-6 @gridColumnSize position-relative" >
					<a href="@link" class="stretched-link" @clickProductLink>
						<span class="visually-hidden">@product.Name</span>
					</a>
					@{
						@RenderGrid(itemSourcePageId)
					}
				</div>
			}

			@if (!Model.Products.Any() || Pageview.IsVisualEditorMode)
			{
				if (Dynamicweb.Context.Current.Items.Contains("ProductDetails"))
				{
					Dynamicweb.Context.Current.Items["ProductDetails"] = GetProduct();
				}
				else
				{
					Dynamicweb.Context.Current.Items.Add("ProductDetails", GetProduct());
				}

				for (int i = 0; i < 4; i++)
				{
					<div class="d-flex flex-column g-col-6 @gridColumnSize position-relative">
						@{
							@RenderGrid(itemSourcePageId)
						}
					</div>
				}
			}
		</div>
	}
}
else if (Pageview.IsVisualEditorMode == true)
{
	<div class="alert alert-dark" role="alert">
		<span>@Translate("Product component slider: The slider will be rendered here, if there is anything to show")</span>
	</div>
}

