@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);

	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 (Model.Products.Any())
	{
		string sourceType = paragraphViewModel.Item.GetRawValueString("RelationType", "Trending");
		int modelId = paragraphViewModel.ID;

		string navigationStyle = paragraphViewModel.Item.GetRawValueString("NavigationStyle", "slider-nav-round");
		string navigationPlacement = paragraphViewModel.Item.GetRawValueString("NavigationPlacement", "slider-nav-on-slides");
		string indicatorStyle = paragraphViewModel.Item.GetRawValueString("IndicatorStyle", string.Empty);
		string revealSlides = paragraphViewModel.Item.GetRawValueString("RevealSlides", "no-reveal") == "reveal" ? "slider-item-reveal" : string.Empty;
		string sliderItemsGap = paragraphViewModel.Item.GetRawValueString("SliderItemsGap", "slider-item-gap") == "slider-item-nogap" ? "slider-item-nogap" : string.Empty;
		string navigationAlwaysVisible = (paragraphViewModel.Item.GetBoolean("NavigationAlwaysVisible")) ? "slider-nav-visible" : string.Empty;
		string navigationVisibleOnTouch = (paragraphViewModel.Item.GetBoolean("NavigationVisibleOnTouch")) ? "slider-nav-touch" : string.Empty;
		string navigationShowScrollbar = (paragraphViewModel.Item.GetBoolean("NavigationShowScrollbar")) ? "slider-nav-scrollbar" : string.Empty;
		string scrollBarForceMobile = (paragraphViewModel.Item.GetBoolean("NavigationShowScrollbar")) ? "--swiffy-slider-track-height:0.5rem !important;" : string.Empty;
		string navigationSmall = (paragraphViewModel.Item.GetBoolean("NavigationSmall")) ? "slider-nav-sm" : string.Empty;
		string navigationInvertColors = (paragraphViewModel.Item.GetBoolean("NavigationInvertColors")) ? "slider-nav-dark" : string.Empty;
		string navigationSlideEntirePage = (paragraphViewModel.Item.GetBoolean("NavigationSlideEntirePage")) ? "slider-nav-page" : string.Empty;
		string navigationNoLoop = (paragraphViewModel.Item.GetBoolean("NavigationNoLoop")) ? "slider-nav-noloop" : string.Empty;
		string indicatorsOutsideSlider = (paragraphViewModel.Item.GetBoolean("IndicatorsOutsideSlider") && indicatorStyle != string.Empty) ? "slider-indicators-outside" : string.Empty;
		string indicatorsHighlightActive = (paragraphViewModel.Item.GetBoolean("IndicatorsHighlightActive")) ? "slider-indicators-highlight" : string.Empty;
		string indicatorsInvertColors = (paragraphViewModel.Item.GetBoolean("IndicatorsInvertedColors")) ? "slider-indicators-dark" : string.Empty;
		string indicatorsVisibleOnSmallDevices = (paragraphViewModel.Item.GetBoolean("IndicatorsVisibleOnSmallDevices")) ? "slider-indicators-sm" : string.Empty;
		string animation = paragraphViewModel.Item.GetRawValueString("Animation", string.Empty) != string.Empty ? $"slider-nav-animation {paragraphViewModel.Item.GetRawValueString("Animation")}" : string.Empty;
		string autoplay = (paragraphViewModel.Item.GetBoolean("Autoplay")) ? "slider-nav-autoplay" : string.Empty;
		string autoplayInterval = paragraphViewModel.Item.GetRawValueString("AutoplayInterval", string.Empty);
		bool hideSliderNavigation = false;

		string gridColumnSize = paragraphViewModel.Item.GetRawValueString("SliderLayout", "3");
		int itemsShown = Convert.ToInt32(gridColumnSize);

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

		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 (navigationStyle == "slider-nav-none")
		{
			hideSliderNavigation = true;
		}

		<div class="grid gap-0">
			<div id="slider_@(modelId)" class="g-col-12 swiffy-slider slider-item-show@(itemsShown) @(navigationStyle) @(revealSlides) @(navigationPlacement) @(navigationAlwaysVisible) @(navigationVisibleOnTouch) @(indicatorStyle) @(navigationShowScrollbar) @(navigationSmall) @(navigationInvertColors) @(indicatorsOutsideSlider) @(navigationNoLoop) @(indicatorsHighlightActive) @(indicatorsInvertColors) @(indicatorsVisibleOnSmallDevices) @(navigationSlideEntirePage)" style="@(scrollBarForceMobile)">
				<ul class="slider-container">
					@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);
						}

						<li class="d-flex flex-column">
							<a href="@link" class="stretched-link" @clickProductLink>
								<span class="visually-hidden">@product.Name</span>
							</a>
							@{
								@RenderGrid(itemSourcePageId)
							}
						</li>
					}

					@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++)
						{
							<li class="d-flex flex-column">
								@{
									@RenderGrid(itemSourcePageId)
								}
							</li>
						}
					}
				</ul>

				@if (!hideSliderNavigation)
				{
					<button type="button" title="@Translate("Previous slide")" class="slider-nav" style="z-index:2;">
						<span class="visually-hidden">@Translate("Previous slide")</span>
					</button>
					<button type="button" title="@Translate("Next slide")" class="slider-nav slider-nav-next" style="z-index:2;">
						<span class="visually-hidden">@Translate("Next slide")</span>
					</button>
				}

				@if (indicatorStyle != "slider-indicators-hidden")
				{
					bool isActive = true;
					int slideCount = 1;

					<div class="slider-indicators" style="z-index:2;">
						@foreach (var item in Model.Products)
						{
							<button @(isActive ? "class=\"active\"" : string.Empty) type="button" title='@Translate("Go to slide") @slideCount'>
								<span class="visually-hidden">@Translate("Go to slide") @slideCount</span>
							</button>
							{
								slideCount++;
							}
							isActive = false;
						}
					</div>
				}
			</div>
		</div>

		<script type="module">
			swiffyslider.initSlider(document.querySelector('#slider_@(modelId)'));
		</script>
	}
}
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>
}
