@inherits Dynamicweb.Rendering.RazorTemplateBase<Dynamicweb.Rendering.RazorTemplateModel<Dynamicweb.Rendering.Template>>
@{
	string shippingMethod = GetString("Ecom:Order.ShippingMethod");

	if (!string.IsNullOrEmpty(shippingMethod))
	{

		List<string> shippingDetails = new List<string>();

		if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Company")))
		{
			shippingDetails.Add(GetString("Ecom:Order.Delivery.Company"));
		}
		if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Name")))
		{
			if (GetString("Ecom:Order.Delivery.Company") != GetString("Ecom:Order.Delivery.Name"))
			{
				shippingDetails.Add(GetString("Ecom:Order.Delivery.Name"));
			}
		}
		if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Address")))
		{
			shippingDetails.Add(GetString("Ecom:Order.Delivery.Address"));
		}
		if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Address2")))
		{
			shippingDetails.Add(GetString("Ecom:Order.Delivery.Address2"));
		}
		if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Zip")))
		{
			shippingDetails.Add(GetString("Ecom:Order.Delivery.Zip"));
		}
		if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.City")))
		{
			shippingDetails.Add(GetString("Ecom:Order.Delivery.City"));
		}
		if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Region")))
		{
			string regionName = GetString("Ecom:Order.Delivery.Region");
			foreach (LoopItem region in GetLoop("DeliveryRegions"))
			{
				if (region.GetString("Ecom:DeliveryRegion.IsDeliveryRegion") == "true")
				{
					regionName = region.GetString("Ecom:DeliveryRegion.Name");
				}
			}
			shippingDetails.Add(regionName);
		}
		if (!string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Country")))
		{
			shippingDetails.Add(GetString("Ecom:Order.Delivery.Country"));
		}
		if (GetBoolean("Ecom:Order.Delivery.isPickupPoint"))
		{
			shippingDetails.Clear();
			var order = Dynamicweb.Ecommerce.Services.Orders.GetById(GetString("Ecom:Order.ID"));
			if (order.ShippingProviderValues.ContainsKey("ShipmondoServicePointId"))
			{
				//Shipmondo provider
				if (order.ShippingProviderValues.TryGetValue("ServiceSelectedDataCarrierName", out object CarrierName))
				{
					shippingDetails.Add((string)CarrierName);
				}
				if (order.ShippingProviderValues.TryGetValue("ServicePointSelectedDataCompanyName", out object CompanyName))
				{
					shippingDetails.Add((string)CompanyName);
				}
				if (order.ShippingProviderValues.TryGetValue("ServicePointSelectedDataAddress", out object Address))
				{
					shippingDetails.Add((string)Address);
				}
				if (order.ShippingProviderValues.TryGetValue("ServicePointSelectedDataAddress2", out object Address2) && !string.IsNullOrEmpty((string)Address2))
				{
					shippingDetails.Add((string)Address2);
				}
				if (order.ShippingProviderValues.TryGetValue("ServicePointSelectedDataZipcode", out object Zipcode))
				{
					shippingDetails.Add((string)Zipcode);
				}
				if (order.ShippingProviderValues.TryGetValue("ServicePointSelectedDataCity", out object City))
				{
					shippingDetails.Add((string)City);
				}
				if (order.ShippingProviderValues.TryGetValue("ServicePointSelectedDataCountry", out object Country))
				{
					shippingDetails.Add((string)Country);
				}
				if (order.ShippingProviderValues.Any())
				{
					var deliveryName =
						!string.IsNullOrWhiteSpace(GetString("Ecom:Order.Delivery.Name")) ? GetString("Ecom:Order.Delivery.Name") :
						!string.IsNullOrWhiteSpace(GetString("Ecom:Order.Customer.Name")) ? GetString("Ecom:Order.Customer.Name") :
						!string.IsNullOrWhiteSpace(GetString("UserManagement:User.Name")) ? GetString("UserManagement:User.Name") : string.Empty;

					<div id="hiddenParcelshopFields" class="list-group-item visually-hidden">
						<input type="hidden" name="EcomOrderDeliveryCompany" value="@($"{(string)CompanyName} ({(string)CarrierName})")" disabled>
						<input type="hidden" name="EcomOrderDeliveryName" value="@(deliveryName)" disabled>
						<input type="hidden" name="EcomOrderDeliveryAddress" value="@((string)Address)" disabled>
						<input type="hidden" name="EcomOrderDeliveryAddress2" value="@((string)Address2)" disabled>
						<input type="hidden" name="EcomOrderDeliveryZip" value="@((string)Zipcode)" disabled>
						<input type="hidden" name="EcomOrderDeliveryCity" value="@((string)City)" disabled>
						<input type="hidden" name="EcomOrderDeliveryCountry" value="@((string)Country)" disabled>
					</div>
				}
			}

			if (order.ShippingProviderValues.ContainsKey("ParcelShopNumber"))
			{
				//GLS Provider
				if (order.ShippingProviderValues.TryGetValue("parcelShop", out object parcelShop))
				{
					if (parcelShop is System.Collections.Generic.Dictionary<System.String, System.Object>)
					{
						var selectedShop = (System.Collections.Generic.Dictionary<System.String, System.Object>)parcelShop;

						if (selectedShop.TryGetValue("CompanyName", out object CompanyName))
						{
							shippingDetails.Add((string)CompanyName);
						}
						if (selectedShop.TryGetValue("Streetname", out object Streetname))
						{
							shippingDetails.Add((string)Streetname);
						}
						if (selectedShop.TryGetValue("Streetname2", out object Streetname2) && !string.IsNullOrEmpty((string)Streetname2))
						{
							shippingDetails.Add((string)Streetname2);
						}
						if (selectedShop.TryGetValue("ZipCode", out object ZipCode))
						{
							shippingDetails.Add((string)ZipCode);
						}
						if (selectedShop.TryGetValue("CityName", out object CityName))
						{
							shippingDetails.Add((string)CityName);
						}
						if (selectedShop.TryGetValue("CountryCodeISO3166A2", out object Country))
						{
							shippingDetails.Add((string)Country);
						}
						if (order.ShippingProviderValues.Any())
						{
							var deliveryName =
							!string.IsNullOrWhiteSpace(GetString("Ecom:Order.Delivery.Name")) ? GetString("Ecom:Order.Delivery.Name") :
							!string.IsNullOrWhiteSpace(GetString("Ecom:Order.Customer.Name")) ? GetString("Ecom:Order.Customer.Name") :
							!string.IsNullOrWhiteSpace(GetString("UserManagement:User.Name")) ? GetString("UserManagement:User.Name") : string.Empty;

							<div id="hiddenParcelshopFields" class="list-group-item visually-hidden">
								<input type="hidden" name="EcomOrderDeliveryCompany" value="@($"{(string)CompanyName}")" disabled>
								<input type="hidden" name="EcomOrderDeliveryName" value="@(deliveryName)" disabled>
								<input type="hidden" name="EcomOrderDeliveryAddress" value="@((string)Streetname)" disabled>
								<input type="hidden" name="EcomOrderDeliveryAddress2" value="@((string)Streetname2)" disabled>
								<input type="hidden" name="EcomOrderDeliveryZip" value="@((string)ZipCode)" disabled>
								<input type="hidden" name="EcomOrderDeliveryCity" value="@((string)CityName)" disabled>
								<input type="hidden" name="EcomOrderDeliveryCountry" value="@((string)Country)" disabled>
							</div>
						}
					}
				}
			}
		}

		string shippingDetailsString = (string.Join(", ", shippingDetails.Select(x => x).ToArray()));

		<div class="list-group-item py-2 fs-7" id="DeliveryGroup">
			<dl class="grid gap-1 gap-sm-3 mb-0">
				<dt class="g-col-8 g-col-sm-4 fw-normal">@Translate("Delivery method")</dt>
				<dd class="d-flex flex-column g-col-12 g-col-sm-7 order-2 order-sm-1 mb-0">
					@if ((!string.IsNullOrEmpty(shippingDetailsString) && !string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Name"))))
					{
						<span>@shippingDetailsString</span>
						<span class="opacity-75 fs-8">
							@shippingMethod
						</span>
					}
					else if ((!string.IsNullOrEmpty(shippingDetailsString) && string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Name")) && string.IsNullOrEmpty(GetString("Ecom:Order.Delivery.Country")))) { 
						<span>@shippingDetailsString</span>
						<span class="opacity-75 fs-8">
							@shippingMethod
						</span>
					}
					else
					{
						<span>@Translate("Same as billing address")</span>
						<span class="opacity-75 fs-8">
							@shippingMethod
						</span>
					}
				</dd>
				<dd class="g-col-4 g-col-sm-1 order-1 order-sm-2 mb-0 text-end">
					<button class="btn btn-sm btn-link fs-8 p-0" data-dw-button="link" type="submit" name="CartV2.GotoStep1" id="EditDeliveryButton">
						<span class="text-uppercase">@Translate("Edit")</span>
					</button>
				</dd>
			</dl>
		</div>
	}
}