using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
namespace RynthCore.Plugin.RynthOracle.Data;
///
/// Coordinates in Oracle's text (quest walkthroughs, Dynamic Stamp notes) as RynthNav targets.
/// The lists write them every which way: "59.6S 88.0W", "8.8S, 53.1E", "42.6n 62.4e".
///
internal static class NavTargets
{
private static readonly Regex CoordsRx = new(
@"(?\d+(?:\.\d+)?)\s*(?[NnSs])[\s,]*(?\d+(?:\.\d+)?)\s*(?[EeWw])\b",
RegexOptions.CultureInvariant);
/// One place to go: what the text calls it (may be empty) and its coordinates ("59.6S, 88.0W").
internal readonly record struct Stop(string Label, string Coords)
{
/// The RynthNav target: the coordinates, then the label (RynthNav uses an Atlas
/// place of that name there, e.g. a dungeon's entrance).
public string Target => Label.Length > 0 ? Coords + " " + Label : Coords;
/// How a picker shows it: "Kill Mukkir Progenitor - 86.5N, 57.3E".
public string Display => Label.Length > 0 ? Label + " - " + Coords : Coords;
}
///
/// A named place with no coordinates (an exploration marker's location): its name is the whole
/// RynthNav target, and RynthNav finds it on its Atlas.
///
public static Stop Place(string name) => new("", name.Trim());
/// The first coordinates in , written "8.8S, 53.1E", or null.
public static string? FirstCoords(string? text)
{
if (string.IsNullOrEmpty(text)) return null;
Match m = CoordsRx.Match(text);
return m.Success ? Format(m) : null;
}
/// with its coordinates taken out.
public static string WithoutCoords(string? text) => string.IsNullOrEmpty(text) ? "" : CoordsRx.Replace(text, " ");
///
/// Every coordinate pair in , in order, each labelled with the words just
/// before it in its step (walkthroughs separate steps with " - "): "Speak with Hoshar ibn Jalaq
/// 59.6S 88.0W - Kill Mukkir Progenitor 86.5N 57.3E" gives two stops. The same coordinates
/// twice are one stop.
///
public static List Stops(string? text)
{
var stops = new List();
if (string.IsNullOrEmpty(text)) return stops;
foreach (Match m in CoordsRx.Matches(text))
{
string coords = Format(m);
if (stops.Exists(s => s.Coords == coords)) continue;
stops.Add(new Stop(LabelBefore(text, m.Index), coords));
}
return stops;
}
/// The words between the previous step break (" - ", ". ", "; ", "|") or coordinates and .
private static string LabelBefore(string text, int at)
{
int from = 0;
foreach (string sep in new[] { " - ", ". ", "; ", "|", "(" })
{
int i = text.LastIndexOf(sep, Math.Max(0, at - 1), StringComparison.Ordinal);
if (i >= 0 && i + sep.Length <= at) from = Math.Max(from, i + sep.Length);
}
// Not back past the previous coordinates ("Cragstone 26.0N 47.0E Speak with ...").
foreach (Match m in CoordsRx.Matches(text.Substring(0, at)))
from = Math.Max(from, m.Index + m.Length);
string label = text.Substring(from, at - from).Trim(' ', ',', ':', '-', '.', '(', ')');
// "at 42.1N ..." and "Meet at 42.1N ...": the little words around the coordinates aren't the place.
label = Regex.Replace(label, @"^(at|in|near|to)\s+|\s+(at|in|near|to)$", "", RegexOptions.IgnoreCase).Trim();
return label.Length > 60 ? label.Substring(0, 60).TrimEnd() : label;
}
private static string Format(Match m) =>
string.Create(CultureInfo.InvariantCulture,
$"{m.Groups["ns"].Value}{char.ToUpperInvariant(m.Groups["nsc"].Value[0])}, {m.Groups["ew"].Value}{char.ToUpperInvariant(m.Groups["ewc"].Value[0])}");
}