@page
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Extensions
@using bahnplan.web.database
@model InspectionModel
@{
    ViewData["Title"] = "Inspection";

    if (HttpContext.Session.GetString("authorized") != "true") {
        Response.Redirect($"/Login?&redir={Request.GetDisplayUrl().UrlEncode()}");
        return;
    }

    if (Model.Leg.UserId != int.Parse(HttpContext.Session.GetString("uid"))) {
        return;
    }
}

<p>
    Ticket for <b>@Model.Leg.TrainType @Model.Leg.TrainNr</b> from <b>@Model.Leg.DepStation</b> to <b>@Model.Leg.ArrStation</b>, starting @DateTime.Parse(Model.Leg.DepTime).ToString("yyyy-MM-dd HH:mm")
</p>
<div class="flex-grid">
    <div class="flex-col">
        <img src="@Model.Ticket.TicketQr" alt="Ticketcode"/>
    </div>
    <div class="flex-col">
        <img src="@Model.Ticket.TicketSecCode" alt="Sichtmerkmal"/>
    </div>
</div>
<br/>
<br/>
@Model.Ticket.TicketInfo
<br/>
<br/>
@if (Model.Ticket.TicketInfo.Contains("BC 50")) {
    await using var db = new Database.DbConn();
    if (db.Cards.Any(p => p.Value == 50 && p.Traveller == Model.Ticket.Traveller && p.UserId == Model.Leg.UserId)) {
        var card = db.Cards.First(p => p.Value == 50 && p.Traveller == Model.Ticket.Traveller && p.UserId == Model.Leg.UserId);
        <div class="flex-grid">
            <div class="flex-col">
                <img src="@card.CardQr" alt="Ticketcode"/>
            </div>
            <div class="flex-col">
                <img src="@card.CardSecCode" alt="Sichtmerkmal"/>
            </div>
            <div class="flex-col">
                <div class="bahncard-outer">
                    <img class="bahncard" src='data:image/svg+xml;base64,@card.CardImage.Base64Encode()' alt="Bahncardimage"/>
                </div>
            </div>
        </div>

        var from = DateTime.Parse(card.ValidFrom);
        var to = DateTime.Parse(card.ValidTo);
        var qrfrom = DateTime.Parse(card.QrValidFrom);
        var qrto = DateTime.Parse(card.QrValidTo);

        if (from > DateTime.Now) {
            <div class="alert alert-info" role="alert">
                The above card is not valid yet.
            </div>
        }
        if (to < DateTime.Now) {
            <div class="alert alert-warning" role="alert">
                The above card is expired.
            </div>
        }
        if (qrfrom > DateTime.Now && !(from > DateTime.Now)) {
            <div class="alert alert-info" role="alert">
                The above QR code is somehow not valid yet. Congrats, please open a bug report.
            </div>
        }
        if (qrto < DateTime.Now) {
            <div class="alert alert-warning" role="alert">
                The above QR code is expired. Please refresh the card. <a href="/Cards?edit=true">You can do that now</a>.
            </div>
        }
    }
    else {
        <div class="alert alert-warning" role="alert">
            I would show your BahnCard but you haven't added a card matching the value and name of the ticket yet. <a href="/Cards">You can do that now</a>.
        </div>
    }
}
else if (Model.Ticket.TicketInfo.Contains("BC 25")) {
    await using var db = new Database.DbConn();
    if (db.Cards.Any(p => p.Value == 25 && p.Traveller == Model.Ticket.Traveller && p.UserId == Model.Leg.UserId)) {
        var card = db.Cards.First(p => p.Value == 25 && p.Traveller == Model.Ticket.Traveller && p.UserId == Model.Leg.UserId);
        <div class="flex-grid">
            <div class="flex-col">
                <img src="@card.CardQr" alt="Ticketcode"/>
            </div>
            <div class="flex-col">
                <img src="@card.CardSecCode" alt="Sichtmerkmal"/>
            </div>
            <div class="flex-col">
                <div class="bahncard-outer">
                    <img class="bahncard" src='data:image/svg+xml;base64,@card.CardImage.Base64Encode()' alt="Bahncardimage"/>
                </div>
            </div>
        </div>

        var from = DateTime.Parse(card.ValidFrom);
        var to = DateTime.Parse(card.ValidTo);
        var qrfrom = DateTime.Parse(card.QrValidFrom);
        var qrto = DateTime.Parse(card.QrValidTo);

        if (from > DateTime.Now) {
            <div class="alert alert-info" role="alert">
                The above card is not valid yet.
            </div>
        }
        if (to < DateTime.Now) {
            <div class="alert alert-warning" role="alert">
                The above card is expired.
            </div>
        }
        if (qrfrom > DateTime.Now && !(from > DateTime.Now)) {
            <div class="alert alert-info" role="alert">
                The above QR code is somehow not valid yet. Congrats, please open a bug report.
            </div>
        }
        if (qrto < DateTime.Now) {
            <div class="alert alert-warning" role="alert">
                The above QR code is expired. Please refresh the card. <a href="/Cards?edit=true">You can do that now</a>.
            </div>
        }
    }
    else {
        <div class="alert alert-warning" role="alert">
            I would show your BahnCard but you haven't added a card matching the value and name of the ticket yet. <a href="/Cards">You can do that now</a>.
        </div>
    }
}