using System;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using bahnplan.web.database;
using bahnplan.web.database.Tables;
using bahnplan.web.JSON;
using LinqToDB;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Leg = bahnplan.web.database.Tables.Leg;

// ReSharper disable PossibleInvalidOperationException

namespace bahnplan.web.Pages {
	public class OEAPIModel : PageModel {
		public int TripId;

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

			var link      = Request.Query["link"].ToString();
			var shortcode = link;
			var oepage    = "oeffisear.ch";
			if (link.Contains("oeffisear.ch"))
				shortcode = link.Split("/#/").Last();
			else if (link.Contains("öv.ztn.sh") || link.Contains("xn--v-0ga.ztn.sh")) {
				shortcode = link.Split("/#/").Last();
				oepage    = "xn--v-0ga.ztn.sh";
			}
			var jid = shortcode.Split("/").Last();
			shortcode = shortcode.Split("/").First();

			using var db       = new Database.DbConn();
			var       response = new WebClient().DownloadString($"https://{oepage}/journeys?{{\"reqId\":\"{shortcode}\"}}");

			var parsed = OeapiResponse.FromJson(response);

			if (!string.IsNullOrWhiteSpace(Request.Query["tripid"].ToString()))
				if (db.Trips.First(p => p.TripId == int.Parse(Request.Query["tripid"].ToString())).UserId != int.Parse(HttpContext.Session.GetString("uid")))
					return;

			var tripId = Request.Query["action"] == "addleg"
				? int.Parse(Request.Query["tripid"])
				: db.InsertWithInt32Identity(new Trip {UserId = int.Parse(HttpContext.Session.GetString("uid"))});

			foreach (var journey in parsed.Data.Journeys[jid].Legs.Where(p => p.IsTransfer != true && p.IsWalking != true)) {
				var arrtime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
				var deptime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
				arrtime = arrtime.AddSeconds((long) journey.Arrival.PlannedTime).ToLocalTime();
				deptime = deptime.AddSeconds((long) journey.Departure.PlannedTime).ToLocalTime();
				var trainNo = int.Parse(journey.Line.TripNum);
				if (trainNo == 0) {
					var extractedTrainNo = Regex.Match(journey.Line.Name, @"\d+").Value;
					if (extractedTrainNo.Length > 0)
						trainNo = int.Parse(extractedTrainNo);
				}
				db.InsertWithInt32Identity(new Leg {
					TripId       = tripId,
					UserId       = int.Parse(HttpContext.Session.GetString("uid")),
					TrainType    = journey.Line.ProductName,
					TrainNr      = trainNo,
					ArrStation   = journey.Arrival.Point.Stop.Name,
					ArrStationId = int.Parse(journey.Arrival.Point.Stop.Id),
					ArrTime      = arrtime.ToString("yyyy-MM-ddTHH:mm:ss"),
					DepStation   = journey.Departure.Point.Stop.Name,
					DepStationId = int.Parse(journey.Departure.Point.Stop.Id),
					DepTime      = deptime.ToString("yyyy-MM-ddTHH:mm:ss")
				});
			}

			TripId = tripId;
		}
	}
}