@using BlazingPizza.Services;

@page "/"
@inject HttpClient HttpClient
@inject NavigationManager NavigationManager
@inject OrderState OrderState

<div class="main">
	<h1>Blazing Pizzas</h1>
	<ul class="pizza-cards">
		@if (_specials != null)
		{
			@foreach (var special in _specials)
			{
				<li @onclick="@(() => OrderState.ShowConfigurePizzaDialog(special))" style="background-image: url('@special.ImageUrl')">
					<div class="pizza-info">
						<span class="title">@special.Name</span>
						<span class="description">@special.Description</span>
						<span class="price">@special.GetFormattedBasePrice()</span>
					</div>
				</li>
			}
		}
	</ul>
</div>

@if (OrderState.ShowingConfigureDialog)
{
	<ConfigurePizzaDialog 
		Pizza="OrderState.ConfiguringPizza"
		OnCancel="OrderState.CancelConfigurePizzaDialog"
		OnConfirm="OrderState.ConfirmConfigurePizzaDialog"/>
}

<div class="sidebar">
	@if (_order.Pizzas.Any())
	{
		<div class="order-contents">
			<h2>Your order</h2>
			@foreach (var configuredPizza in _order.Pizzas)
			{
				<div class="cart-item">
					<div class="title">@(configuredPizza.Size)" @configuredPizza.Special.Name</div>
					<div class="item-price">
						@configuredPizza.GetFormattedTotalPrice()
					</div>

					<a @onclick="@(() => OrderState.RemoveConfiguredPizza(configuredPizza))" class="delete-item">Ąż</a>
				</div>
			}
		</div>
	}
	else
	{
		<div class="empty-cart">Choose a pizza<br />to get started</div>
	}

	<div class="order-total @(_order.Pizzas.Any() ? "" : "hidden")">
		Total:
		<span class="total-price">@_order.GetFormattedTotalPrice()</span>
		<a href="checkout" class="@(OrderState.Order.Pizzas.Count == 0 ? "btn btn-warning disabled" : "btn btn-warning")">
			Order >
		</a>
	</div>
</div>

@code {
	private List<PizzaSpecial> _specials = new List<PizzaSpecial>();
	private Order _order => OrderState.Order;

	protected override async Task OnInitializedAsync()
	{
		_specials = await HttpClient.GetFromJsonAsync<List<PizzaSpecial>>(NavigationManager.BaseUri + "specials");
	}
}
		