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

@{
	string iconPath = "/Files/Images/Icons/";
	string productId = Model.Id;
	string variantId = Model.VariantId;
	string productImage = "/Admin/Public/GetImage.ashx?Image=" + Model.DefaultImage.Value + "&width=80&format=webp";
	productImage = !Model.DefaultImage.Value.StartsWith("/Files/", StringComparison.OrdinalIgnoreCase) ? Model.DefaultImage.Value : productImage;
	bool isInFavoriteList;

	IEnumerable<FavoriteList> favoreiteLists = Pageview.User.GetFavoriteLists();
	int favoriteListTotal = favoreiteLists.Count();

	int favoriteListId = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.QueryString.Get("FavoriteListId")) ? Convert.ToInt32(Dynamicweb.Context.Current.Request.QueryString.Get("FavoriteListId")) : 0;
	bool showButtonText = !string.IsNullOrEmpty(Dynamicweb.Context.Current.Request.QueryString.Get("ShowButtonText")) ? Convert.ToBoolean(Dynamicweb.Context.Current.Request.QueryString.Get("ShowButtonText")) : false;

	if (favoreiteLists.Count() == 1)
	{
		favoriteListId = favoreiteLists.FirstOrDefault().ListId;
	}
}

@if (favoriteListTotal < 2)
{
	isInFavoriteList = Pageview.User.IsProductInFavoriteList(favoriteListId, productId, variantId);
	string defaultCommand = isInFavoriteList ? "remove" : "add";
	string filledFavoriteIcon = isInFavoriteList ? "" : "d-none";
	string outlineFavoriteIcon = isInFavoriteList ? "d-none" : "";

	if (favoreiteLists.Count() == 0 && favoriteListId == 0)
	{
		isInFavoriteList = false;
	}

	if (favoreiteLists.Count() == 1 && favoriteListId == 0) {
		isInFavoriteList = true;
	}

	<form action="@Pageview.SearchFriendlyUrl.Replace("FavoriteListId=0", $"FavoriteListId={favoriteListId}")" id="FavoriteToggleForm_@productId@variantId?.Replace(".", "")" data-product-id="@productId" data-variant-id="@variantId">
		<input type="hidden" name="ReloadPage" value="false">
		<input type="hidden" name="FavoriteListId" value="@favoriteListId">
		<input type="hidden" name="UserId" value="@Pageview.User.ID">
		<input type="hidden" name="ProductName" value="@Model.Name">

		@if (!string.IsNullOrEmpty(Model.DefaultImage.Value))
		{
			<input type="hidden" name="Thumbnail" value="@productImage">
		}
		@if (isInFavoriteList)
		{
			<input type="hidden" name="FavoriteCmd" value="removeproductfromfavoritelist">
		}
		else
		{
			<input type="hidden" name="FavoriteCmd" value="addproducttofavoritelist">
		}
	</form>

	<script>
		swift.Favorites.Update('FavoriteToggleForm_@productId@variantId?.Replace(".", "")');
	</script>

	<div class="favorite-icon">
		<span class="in-list text-nowrap @filledFavoriteIcon">
			<span class="icon-2">@ReadFile(iconPath + "heart-filled.svg")</span>
			@if (showButtonText)
			{
				<span class="ms-1">@Translate("Remove from favorites")</span>
			}
		</span>
		<span class="not-in-list text-nowrap @outlineFavoriteIcon">
			<span class="icon-2">@ReadFile(iconPath + "heart.svg")</span>
			@if (showButtonText)
			{
				<span class="ms-1">@Translate("Add to favorites")</span>
			}
		</span>
	</div>
}
else
{
	IEnumerable<FavoriteList> favoriteLists = Pageview.User.GetFavoriteLists();
	int favoriteListsLink = GetPageIdByNavigationTag("FavoriteListPage");

	<div class="offcanvas-header">
		<h5 class="offcanvas-title">@Translate("Save") <span class="fw-bold">@Model.Name</span> @Translate("for later")</h5>
		<button type="button" class="btn-close text-reset" data-bs-dismiss="offcanvas" aria-label="@Translate("Close")"></button>
	</div>
	<div class="offcanvas-body">
		<form method="post" action="@Pageview.SearchFriendlyUrl" class="d-flex flex-column h-100" data-product-id="@productId" data-variant-id="@variantId">
			<input type="hidden" name="FavoriteCmd" value="">
			<input type="hidden" name="FavoriteListId" value="@favoriteListId">
			<input type="hidden" name="UserId" value="@Pageview.User.ID">

			<input type="hidden" name="ProductId" value="@Model.Id">
			<input type="hidden" name="ProductName" value="@Model.Name">
			<input type="hidden" name="ProductCurrency" value="@Model.Price.CurrencyCode">
			<input type="hidden" name="ProductPrice" value="@Model.Price.ToStringInvariant()">

			@if (!string.IsNullOrEmpty(Model.DefaultImage.Value))
			{
				<input type="hidden" name="Thumbnail" value="@productImage">
			}

			<div class="flex-fill overflow-auto">
				<ul class="list-group">
					@foreach (FavoriteList list in favoriteLists)
					{
						string listName = !string.IsNullOrEmpty(list.Name) ? list.Name : Translate("My favorites");
						bool isInThisFavoriteList = Pageview.User.IsProductInFavoriteList(list.ListId, productId, variantId);
						string defaultCommand = isInThisFavoriteList ? "remove" : "add";

						<li class="list-group-item">
							<button type="button" class="btn text-start d-flex align-items-center w-100 swift_favorites-button" data-bs-dismiss="offcanvas" data-list-id="@list.ListId" data-product-id="@productId" data-variant-id="@variantId" data-command="@defaultCommand" data-in-this-list="@isInThisFavoriteList.ToString()" onclick="swift.Favorites.Update(event)">
								<span class="flex-fill me-2">@listName</span>
								@if (isInThisFavoriteList)
								{
									<div class="icon-2 favorite-icon">
										<span class="in-list">@ReadFile(iconPath + "heart-filled.svg")</span>
										<span class="not-in-list d-none">@ReadFile(iconPath + "heart.svg")</span>
									</div>
								}
								else
								{
									<div class="icon-2 favorite-icon">
										<span class="in-list d-none">@ReadFile(iconPath + "heart-filled.svg")</span>
										<span class="not-in-list">@ReadFile(iconPath + "heart.svg")</span>
									</div>
								}
							</button>
						</li>
					}
				</ul>
			</div>

			@if (favoriteListsLink > 0)
			{
				<a href="/Default.aspx?ID=@favoriteListsLink" class="btn btn-primary w-100" data-dw-button="primary" id="CreateNewFavoriteList"><span class="me-2">@ReadFile(iconPath + "clipboard.svg") </span> @Translate("Create a new favorite list")</a>
			}
		</form>
	</div>
}
