@page "{id:int}"
@using AfRApay.Web.Backend.Database
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using Microsoft.EntityFrameworkCore
@model EditUserModel
@{
	ViewData["Title"] = "Edit User";
	if (Request.Method == "POST" && Request.Form["action"] == "delete") {
		return;
	}
	var db = new DatabaseContext();
	var user = db.Users.First(p => p.Id == int.Parse(RouteData.Values["id"]!.ToString()!));
	var cards = db.Cards.Include(p => p.User).Where(p => p.User == user);

	var linkFlag = db.Config.FirstOrDefault(p => p.Name == "link");
	var lTimeFlag = db.Config.FirstOrDefault(p => p.Name == "lTime");
	var linkActive = !string.IsNullOrWhiteSpace(linkFlag?.Value) && linkFlag.Value == user.Id.ToString() && !string.IsNullOrWhiteSpace(lTimeFlag?.Value) && DateTime.UtcNow - DateTime.Parse(lTimeFlag.Value) < TimeSpan.FromMinutes(5);
}

@if (linkActive) {
@section Header {
	<meta http-equiv="refresh" content="5"/>
}}

<div class="text-center">
	<h1 class="display-5">
		Edit User
	</h1>
</div>

<form id="change_user" method="POST">
	<h3>Account</h3>
	<div class="mb-3">
		<label for="nickname" class="form-label">Nickname</label>
		<input type="text" maxlength="10" class="form-control" id="nickname" name="nickname" value="@user.Nickname" required>
	</div>
	<button type="submit" class="btn btn-lg btn-primary" name="action" value="save">Save</button>
	<button type="button" class="btn btn-lg btn-danger" name="action" data-bs-toggle="modal" data-bs-target="#deleteConfirmModal">Delete</button> <!-- Calls the below modal dialog to confirm user deletetion -->
</form>

<!-- Modal dialog to confirm user deletion -->
<div class="modal fade" id="deleteConfirmModal" tabindex="-1" aria-labelledby="deleteConfirmModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-dialog-centered">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title" id="deleteConfirmModalLabel">Delete user @user.Nickname?</h5>
				<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
			</div>
			<div class="modal-body">
				Are you sure you want to delete this user?<br>This cannot be undone.
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-lg btn-secondary" data-bs-dismiss="modal">Cancel</button>
				<button type="submit" form="change_user" class="btn btn-lg btn-danger" name="action" value="delete">Delete User</button>
			</div>
		</div>
	</div>
</div>

<br/>
<h3 class="text-center">Cards</h3>
@{
	async Task RenderCard(string cardHolder, string cardNumber = "Awooo!", string expDate = "01/2038", EditUserModel.CardType type = EditUserModel.CardType.Normal) {
		var cardTypeClasses = type switch {
		                      EditUserModel.CardType.Normal => "fake-card",
		                      EditUserModel.CardType.LinkPlaceholder => "fake-card ghost-card",
		                      EditUserModel.CardType.DeletionConfirmation => "fake-card",
		                      _ => throw new ArgumentOutOfRangeException(nameof(type), type, null)};

		var blurClasses = type == EditUserModel.CardType.Normal ? " blur-true m-2" : "m-2";

		<div class="fake-card-wrapper @blurClasses">
			@if (type == EditUserModel.CardType.Normal) {
				<form id="@cardNumber" method="POST">
					<input type="hidden" name="cardId" value="@cardNumber">
					<button type="button" class="btn btn-danger btn-lg hover-button" data-bs-toggle="modal" data-bs-target="#delete_card_@(cardNumber)_modal">Delete</button>
				</form>
			}
			else if (type == EditUserModel.CardType.LinkPlaceholder) {
				if (linkActive) {
					<button class="btn btn-secondary btn-lg link-card-button-in-progress">Link in progress</button>
				}
				else {
					<form method="POST">
						<input type="hidden" name="userId" value="@user.Id">
						<button type="submit" class="btn btn-success btn-lg link-card-button" name="action" value="linkCard">Link Card</button>
					</form>
				}
			}
			<div class="@cardTypeClasses">
				<div class="face-card front">
					<div class="chip">
						<div>
							<div>
								<div>
								</div>
							</div>
						</div>
					</div>
					<h2 class="card-number">@cardNumber</h2>
					<h3 class="card-holder">@cardHolder</h3>
					<span class="validity">
						<span class="small">VALID<br>THRU</span>
						<span>@expDate</span>
					</span>

					<div class="fake-card-logo-top">
						<img src="/img/afra.png" class="fake-card-logo-inner" alt="">
					</div>
					<div class="fake-card-logo-bottom">
						<img src="/img/matecard.png" class="fake-card-logo-inner" alt="">
					</div>
				</div>
			</div>
		</div>
	}
}

@{
	async Task RenderCardModal(string cardHolder, string cardNumber) {
		<div class="modal fade" id="delete_card_@(cardNumber)_modal" tabindex="-1" aria-labelledby="delete_card_@(cardNumber)_modal_label" aria-hidden="true">
			<div class="modal-dialog modal-dialog-centered">
				<div class="modal-content">
					<div class="modal-header">
						<h5 class="modal-title" id="delete_card_@(cardNumber)_modal_label">Delete this card?</h5>
						<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
					</div>
					<div class="modal-body d-flex justify-content-center">
						@{
							await RenderCard(cardHolder, cardNumber, type: EditUserModel.CardType.DeletionConfirmation);
						}
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-lg btn-secondary" data-bs-dismiss="modal">Cancel</button>
						<button type="submit" form="@cardNumber" class="btn btn-danger btn-lg" name="action" value="deleteCard">Delete</button>
					</div>
				</div>
			</div>
		</div>
	}
}

<div class="row justify-content-center">
	@{
		foreach (var card in cards) {
			await RenderCard(user.Nickname, card.Id);
			await RenderCardModal(user.Nickname, card.Id);
		}
		await RenderCard(user.Nickname, type: EditUserModel.CardType.LinkPlaceholder);
	}
</div>
