@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.FileViewModel>
@using System.IO
@using Dynamicweb.Frontend

@if (!string.IsNullOrEmpty(Model.Path))
{
	string imagePath = Model?.Path ?? "";
	string imagePathUrlEncoded = Dynamicweb.Context.Current.Server.UrlEncode(imagePath);

	var eagerLoadNewImages = GetViewParameterBoolean("eagerLoadNewImages");
	var doNotUseGetimage = GetViewParameterBoolean("doNotUseGetimage");
	bool imageIsNewAndNeedsEagerLoading = false;
	if (eagerLoadNewImages)
	{
		var fullImagePath = Dynamicweb.Context.Current.Server.MapPath(imagePath);
		if (System.IO.File.Exists(fullImagePath))
		{
			var fileInfo = new System.IO.FileInfo(fullImagePath);
			if (fileInfo.LastWriteTime.AddDays(7) > DateTime.Now)
			{
				//image was changed in the last 7 days - eager load it.
				imageIsNewAndNeedsEagerLoading = true;
			}
		}
	}



	int xPos = Model?.FocalPositionFromLeft ?? 50;
	int yPos = Model?.FocalPositionFromTop ?? 50;
	string cssPosition = $"{xPos}% {yPos}%";

	string imageQuality = GetViewParameterInt32("quality") > 0 ? $"&quality={GetViewParameterInt32("quality")}" : "";

	string imgSizeSelector = "50vw";
	bool isFullScreenWidth = GetViewParameterBoolean("fullwidth");
	if (isFullScreenWidth)
	{
		imgSizeSelector = "100vw";
	}
	else
	{
		int imageColumnCount = GetViewParameterInt32("columns");
		if (imageColumnCount == 1)
		{
			imgSizeSelector = "100vw";
		}
		else if (imageColumnCount == 2)
		{
			imgSizeSelector = "50vw";
		}
		else if (imageColumnCount == 3)
		{
			imgSizeSelector = "33vw";
		}
		else if (imageColumnCount == 4)
		{
			imgSizeSelector = "25vw";
		}
		else if (imageColumnCount == 6)
		{
			imgSizeSelector = "17vw";
		}
	}


	@* Image sizes + a fallback image *@
	string imagePathM = $"/Admin/Public/GetImage.ashx?image={imagePathUrlEncoded}&width=640&format=webp{imageQuality}";
	string imagePathL = $"/Admin/Public/GetImage.ashx?image={imagePathUrlEncoded}&width=960&format=webp{imageQuality}";
	string imagePathXL = $"/Admin/Public/GetImage.ashx?image={imagePathUrlEncoded}&width=1280&format=webp{imageQuality}";
	string imagePathXXL = $"/Admin/Public/GetImage.ashx?image={imagePathUrlEncoded}&width=1920&format=webp{imageQuality}";
	string imagePathFallBack = imagePathM;

	@* attributes to send to the image tag *@
	string loading = !string.IsNullOrEmpty(GetViewParameterString("loading")) ? GetViewParameterString("loading") : "lazy";
	if (imageIsNewAndNeedsEagerLoading)
	{
		loading = "eager";
	}

	var cssClass = GetViewParameter("cssClass");
	var itemprop = GetViewParameter("itemprop");
	var style = GetViewParameter("style");
	string alt = GetViewParameterString("alt");
	var dataImageNumber = GetViewParameter("data-image-number");
	var id = GetViewParameter("id"); //	GetViewParameter returns null if no value is found leaving out the id attribute if no value is present for this parameter.
	var title = GetViewParameter("title");
	if (title != null)
	{
		title = title.ToString() != "" ? "title=\"" + title + "\"" : "";
	}

	if (imagePath.EndsWith(".svg", StringComparison.OrdinalIgnoreCase))
	{
		<div class="icon-auto">
			@ReadFile(Model.Path)
		</div>
	}
	else if (!imagePath.StartsWith("/Files/", StringComparison.OrdinalIgnoreCase) || doNotUseGetimage)
	{
		<img src="@Model.Path"
			 loading="@loading"
			 decoding="async"
			 class="@cssClass"
			 style="object-fit: cover;object-position: @cssPosition;@style"
			 alt="@alt"
			 id="@id"
			 itemprop="@itemprop"
			 data-image-number="@dataImageNumber"
			 @title>
	}
	else
	{
		<img srcset="
			@imagePathM   640w,
			@imagePathL   960w,
			@imagePathXL  1280w,
			@imagePathXXL 1920w"
			 src="@imagePathFallBack"
			 sizes="(max-width: 992px) 100vw, @imgSizeSelector"
			 loading="@loading"
			 decoding="async"
			 class="@cssClass"
			 style="object-fit: cover;object-position: @cssPosition;@style"
			 alt="@alt"
			 id="@id"
			 itemprop="@itemprop"
			 data-image-number="@dataImageNumber"
			 @title>
	}
}
