using System;
using System.Linq;
using System.Text;
using bahnplan.web.database;
using bahnplan.web.database.Tables;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace bahnplan.web.Pages {
	public class InspectionModel : PageModel {
		public Leg    Leg;
		public Ticket Ticket;

		public void OnGet() {
			if (HttpContext.Session.GetString("authorized") != "true")
				return;

			using var db = new Database.DbConn();
			Leg    = db.Legs.First(p => p.LegId == int.Parse(Request.Query["leg"]));
			Ticket = db.Tickets.First(p => p.TicketId == Leg.TicketId);

			if (!Ticket.TicketQr.StartsWith("data:image/png"))
				Ticket.TicketQr = Encoding.UTF8.GetString(Convert.FromBase64String(Ticket.TicketQr));

			if (!Ticket.TicketSecCode.StartsWith("data:image/png"))
				Ticket.TicketSecCode = Encoding.UTF8.GetString(Convert.FromBase64String(Ticket.TicketSecCode));
		}
	}
}
