@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.QueryPublisher.QueryResultViewModel>
@using Dynamicweb.Core.Encoders
@using Dynamicweb
@using Dynamicweb.Frontend

@{
	string uniqueId = Pageview?.CurrentParagraph?.ID != null ? Pageview.CurrentParagraph.ID.ToString() :
	Guid.NewGuid().ToString("N");
}

@if (Model.Results.Count() > 0)
{
	AddStylesheet("/Files/Templates/Designs/Swift-v2/Assets/lib/swiffy-slider/dist/css/swiffy-slider.min.css");
	AddScript("/Files/Templates/Designs/Swift-v2/Assets/lib/swiffy-slider/dist/js/swiffy-slider.min.js");

	<div id="Slider_@uniqueId" class="
			swiffy-slider  
			slider-item-reveal  
			slider-nav-round 
			slider-nav-sm
			slider-nav-page 
			slider-indicators-sm
			slider-indicators-round 
			slider-indicators-outside">

		<ul class="slider-container pb-4"
			style="--swiffy-slider-item-width: min(calc(100% - var(--swiffy-slider-item-reveal)), 25rem);">
			@foreach (var post in Model.Results)
			{
				if (post.TryGetValue("PageId", out object? pageId) && pageId is int)
				{
					var pageObject = Dynamicweb.Content.Services.Pages?.GetPage((int)pageId);
					var pageInfo = pageObject != null ? Dynamicweb.Frontend.ContentViewModelFactory.CreatePageInfoViewModel(pageObject) : null;
					string? parentCategory = pageObject?.Parent != null && pageObject.Parent.Level != 1 ? pageObject.Parent.GetDisplayName() : null;

					<li>
						<a href="/Default.aspx?ID=@pageInfo.ID" class="card shadow text-decoration-none h-100"
							style="min-height:25rem;">
							@if (pageInfo.TryGetImageFile(out ImageFileViewModel imageFile))
							{
								<figure class="m-0 position-relative">
									<img class="card-img-top" src="@imageFile.ToGetImage(new GetImageSettings { Ratio = "16/9" })" title="@pageInfo.Name">
									@if (parentCategory != null) {
										<div class="badge badge-neutral-light position-absolute top-0 end-0 m-2">@parentCategory</div>
									}
								</figure>
							}
							<div class="card-body d-flex flex-column justify-content-between">
								<div data-swift-text class="mb-0-last-child">
									<h5 class="card-title">@pageInfo.Name</h5>
									<p class="card-text">
										@pageInfo.Description
									</p>
								</div>
							</div>
						</a>
					</li>
				}
			}
		</ul>

		<button type="button" title="@Translate("Previous slide")" class="slider-nav" style="z-index:1;">
			<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:1;">
			<span class="visually-hidden">@Translate("Next slide")</span>
		</button>

		<ul class="slider-indicators mb-2">
			@for (int slideCount = 1; Model.Results.Count() >= slideCount; slideCount++)
			{
				<li title='@Translate("Go to slide") @slideCount'>
					<span class="visually-hidden">@Translate("Go to slide") @slideCount</span>
				</li>
			}
		</ul>
	</div>

	<script>
		window.swiffyslider.initSlider(document.querySelector('#Slider_@uniqueId'));
	</script>
}
