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

@functions {
	private string GetFontStack()
	{
		return "font-family:initial,Arial,Helvetica Neue,Helvetica,sans-serif;";
	}

	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 borders = Model.Item.GetRawValueString("Borders", "none");
	string borderThicknes = Model.Item.GetRawValueString("BorderThicknes", "0") + "px";
	string borderClass = string.Empty;

	switch (borders)
	{
		case "top":
			borderClass = $"border-top:{borderThicknes} solid;";
			break;
		case "bottom":
			borderClass = $"border-bottom:{borderThicknes} solid;";
			break;
		case "top-bottom":
			borderClass = $"border-top:{borderThicknes} solid; border-bottom:{borderThicknes} solid; border-left:0;border-right:0;";
			break;
		case "all":
			borderClass = $"border:{borderThicknes} solid;";
			break;
	}

	string padding = Model.Item.GetRawValueString("Padding", "0");
	string align = Model.Item.GetRawValueString("Align", "left");
	string lineHeight = "line-height:" + (16 * Dynamicweb.Core.Converter.ToDouble(Model.Item.GetRawValueString("LineHeight", "1.5"))).ToString() + "px;";
	string linkLabel = Model.Item.GetString("LinkLabel", string.Empty);
	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;@GetTableReset();color:inherit;width:100%;@(backgroundColor)@GetFontStack()">
	<tbody>
		<tr style="@GetTableReset()">
			<td style="mso-table-lspace:0pt;mso-table-rspace:0pt;mso-line-height-rule:exactly;text-align:@align;font-weight:400;@GetFontStack()@GetTableReset()vertical-align:top;@(backgroundColor)@(foregroundColor)padding:@(padding)px;@(borderClass)" width="100%">

				@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>
				}

				@if (Model.Item.TryGetString("Title", out string title))
				{
					string fontSize = "font-size:" + Model.Item.GetRawValueString("FontSize", "24") + "px;";
					string textLineHeight = "line-height:" + (Dynamicweb.Core.Converter.ToInt32(Model.Item.GetRawValueString("FontSize", "24")) * Dynamicweb.Core.Converter.ToDouble(Model.Item.GetRawValueString("LineHeight", "1.5"))).ToString() + "px;";
					var textRaise = ((Dynamicweb.Core.Converter.ToInt32(Model.Item.GetRawValueString("FontSize", "28")) * Dynamicweb.Core.Converter.ToDouble(Model.Item.GetRawValueString("LineHeight", "1.5"))) - Dynamicweb.Core.Converter.ToInt32(Model.Item.GetRawValueString("FontSize", "28"))) / 2;
					textRaise = textRaise == 0 ? 11 : textRaise;
					string startTag = $"<p style=\"mso-line-height-rule:exactly;mso-text-raise:{textRaise}px;vertical-align:middle;display:block;{GetFontStack()}{fontSize}{lineHeight}\">";
					string endTag = $"</p>";

					<div style="display:block;padding-top:16px;">
						@startTag
						@title
						@endTag
					</div>
				}

				@if (Model.Item.TryGetString("Text", out string text))
				{
					<div style="display:block;padding-top:16px;@(lineHeight)">
						<div style="mso-line-height-rule:exactly;mso-text-raise:11px;vertical-align:middle;margin-top:0;margin-bottom:0px;@GetFontStack()@lineHeight">
							@text
						</div>
					</div>
				}
				@if (link is object && !string.IsNullOrEmpty(link.Url) && !string.IsNullOrEmpty(linkLabel))
				{
					var buttonBackgroundColor = Model?.ColorScheme?.PrimaryButtonColor?.Hex ?? string.Empty;
					var buttonForegroundColor = Model?.ColorScheme?.PrimaryButtonColor?.Contrast(100).ToString() ?? string.Empty;
					var buttonBorderColor = Model?.ColorScheme?.PrimaryButtonColor?.Hex ?? string.Empty;

					var parms = new Dictionary<string, object>();
					parms.Add("backgroundColor", buttonBackgroundColor);
					parms.Add("foregroundColor", buttonForegroundColor);
					parms.Add("borderColor", buttonBorderColor);
					parms.Add("align", align);
					parms.Add("title", linkLabel);
					
					<div style="display:block;padding-top:16px;">
						@RenderPartial("Swift-v2_Email/Components/Button.cshtml", link ?? new Dynamicweb.Frontend.LinkViewModel(), parms)
					</div>
				}
				<!--[if mso]><br/><![endif]-->
			</td>
		</tr>
	</tbody>
</table>

