@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.ParagraphViewModel>

@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 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", "center");
	string size = Model.Item.GetRawValueString("IconSize", "32");
	string iconSpacing = Model.Item.GetRawValueString("IconSpacing", "16");
	string urlPrefix = $"{Dynamicweb.Context.Current?.Request?.Url?.Scheme}://{Dynamicweb.Context.Current?.Request?.Url?.Host}{(Dynamicweb.Context.Current?.Request?.Url?.Port > 0 ? ":" + Dynamicweb.Context.Current.Request.Url.Port : null)}";
}

<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;color:inherit;width:100%;@(backgroundColor)">
	<tbody>
		<tr style="@GetTableReset()">
			<td style="mso-table-lspace:0pt;mso-table-rspace:0pt;font-weight:400;@align;vertical-align:top;@(backgroundColor)@(foregroundColor)padding:@(padding)px;@(borderClass)" width="100%">
				<table class="icon-table" cellpadding="0" cellspacing="0" role="presentation" align="@align" style="mso-table-lspace:0pt;mso-table-rspace:0pt;mso-line-height-rule:exactly;@GetTableReset()margin-left:auto;margin-right:auto;">
					<tbody>
						<tr style="@GetTableReset()">
							@if (Model.Item.TryGetItems("IconItems", out var iconItems))
							{
								var last = iconItems.Last();
								foreach (var item in iconItems)
								{
									item.TryGetString("Title", out string? title);
									string spacing = !item.Equals(last) ? $"padding-right:{iconSpacing}px;" : string.Empty;

									<td style="@GetTableReset()text-align:center;@(spacing)">

										@if (item.TryGetFile("Icon", out var iconItem)) {	
											var icon = $"{urlPrefix}{item.GetFile("Icon").Path}";
											
											if (item.TryGetLink("Link", out var linkItem )) {
												string link = string.Empty;

												if (linkItem.IsExternal || linkItem.Url.Contains("://") || linkItem.Url.StartsWith("mailto:") || linkItem.Url.StartsWith("tel:")) { 
													link = linkItem.Url;
												}
												else {
													link = $"{urlPrefix}{linkItem.Url}";
												}
												
												<a href="@link" title="@title" style="display:block;">
													<img src="@icon" alt="@title" height="@size" width="@size" align="middle" style="display:block;height:auto;@GetTableReset()">
												</a>
											}
											else
											{
												<img src="@icon" alt="@title" height="@size" width="@size" align="middle" style="display:block;height:auto;@GetTableReset()">
											}
										}
										<!--[if mso]><br/><![endif]-->
									</td>
								}
							}
						</tr>
					</tbody>
				</table>
				<!--[if mso]><br/><![endif]-->
			</td>
		</tr>
	</tbody>
</table>
