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

@functions {
	private string GetTableReset()
	{
		return "border:0;margin:0;outline:0;padding:0;";
	}
}

@{
	string backgroundColor = $"background-color:{Model.ColorScheme?.BackgroundColor?.Hex ?? string.Empty};";
	string foregroundColor = $"color:{Model.ColorScheme?.ForegroundColor?.Hex ?? string.Empty};";
	string padding = Model.Item.GetRawValueString("Padding", "0");
	
	var link = new Dynamicweb.Frontend.LinkViewModel();
	string linkType = Model.Item.GetRawValueString("LinkType", "page");

	if (linkType == "page" && Model.Item.TryGetLink("PageLink", out var pageLink))
	{
		link.Url = pageLink.Url;
	}

	if (linkType == "product-group" && Model.Item.TryGetValue<IList<ProductGroupViewModel>>("ProductGroupLink", out var selectedGroups))
	{
		IList<string> groupIds = new List<string> { };

		foreach (var fromGroup in selectedGroups)
		{
			groupIds.Add(fromGroup.Id);
		}

		link.Url = "/Default.aspx?ID=" + GetPageIdByNavigationTag("Shop") + "&GroupID=" + string.Join(",", groupIds);
	}

	if (linkType == "product" && Model.Item.TryGetValue<ProductListViewModel>("ProductLink", out var products))
	{
		IList<string> productIds = new List<string> { };

		if (products.Products.Any())
		{
			foreach (var product in products.Products)
			{
				productIds.Add(product.Id);
			}

			string productParameter = productIds.Count == 1 ? "ProductID" : "MainProductId";
			string productLink = products.Products.FirstOrDefault().GetProductLink(GetPageIdByNavigationTag("Shop"), false);
			productLink = productIds.Count != 1 ? "/Default.aspx?ID=" + GetPageIdByNavigationTag("Shop") + "&" + productParameter + "=" + string.Join(",", productIds).Trim() : productLink;

			link.Url = productLink;
		}
	}
}

<table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="mso-table-lspace:0pt;mso-table-rspace:0pt;mso-line-height-rule:exactly;@GetTableReset()color:inherit;width:100%;@(backgroundColor)">
	<tbody>
		<tr style="@GetTableReset()">
			<td style="mso-table-lspace:0pt;mso-table-rspace:0pt;mso-line-height-rule:exactly;text-align:left;@GetTableReset()vertical-align:top;@(backgroundColor)@(foregroundColor)padding:@(padding)px" width="100%">
				<div align="left">
					@if (Model.Item.TryGetImageFile("Image", out ImageFileViewModel image))
					{
						var parms = new Dictionary<string, object>();
						parms.Add("alt", Model.Item.GetString("ImageAltText"));
						parms.Add("columns", Model.GridRowColumnCount);
						parms.Add("padding", (Dynamicweb.Core.Converter.ToInt32(padding) * 2) * Model.GridRowColumnCount);
						parms.Add("imageRatio", image.Ratio);

						<div align="left">
							@if (link is object && !string.IsNullOrEmpty(link.Url))
							{
								@:<a href="@link" target="_blank" style="display:block;">
							}

							@RenderPartial("Swift-v2_Email/Components/Image.cshtml", image ?? new Dynamicweb.Frontend.FileViewModel(), parms)

							@if (link is object && !string.IsNullOrEmpty(link.Url))
							{
								@:</a>
							}
						</div>
					}
				</div>
			</td>
		</tr>
	</tbody>
</table>
