using System.Text.Json; using PlotLine.Models; namespace PlotLine.Services; public static class MapValidation { public static readonly string[] Kinds = ["World", "Continent", "Region", "Country/Kingdom", "Island", "City/Town", "Local Area", "Other"]; public static readonly string[] Directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"]; public static readonly string[] Distances = ["Very close", "Close", "Moderate", "Far", "Very far"]; public static string? Validate(MapDocument d) { if (string.IsNullOrWhiteSpace(d.Map.Name) || d.Map.Name.Length > 200 || !Kinds.Contains(d.Map.Kind)) return "Choose a map name and kind."; if (d.Features.Count > 3000 || d.Relationships.Count > 10000) return "This map has too many items. Consider a detailed child map."; var ids = d.Features.Select(x => x.FeatureID).ToHashSet(); if (ids.Contains(Guid.Empty) || ids.Count != d.Features.Count) return "Feature identifiers must be unique."; var markers = d.Features.Where(x => x.GeometryClass == "Marker").Select(x => x.FeatureID).ToHashSet(); foreach (var f in d.Features) { if (string.IsNullOrWhiteSpace(f.Name) || f.Name.Length > 200 || f.Type.Length > 60 || f.Layer.Length > 30 || f.Notes?.Length > 4000) return "Check feature names and notes."; if (!new[] { "Marker", "Path", "Area" }.Contains(f.GeometryClass)) return "Unknown map piece shape."; if (new[] { f.X, f.Y, f.Width, f.Height, f.Rotation, f.LabelX, f.LabelY }.Any(x => !double.IsFinite(x) || Math.Abs(x) > 100000) || f.Width < 1 || f.Height < 1) return "Check feature dimensions."; if ((f.StartFeatureID.HasValue && !markers.Contains(f.StartFeatureID.Value)) || (f.EndFeatureID.HasValue && !markers.Contains(f.EndFeatureID.Value))) return "Choose route endpoints on this map."; if (f.CreateLocation && f.LocationID is null && f.IsProvisional) return "Establish the place before creating a story Location, or leave it as a planning feature."; if (f.GeometryJson.Length > 64000) return "Shape is too detailed."; try { using var geometry = JsonDocument.Parse(f.GeometryJson); if (geometry.RootElement.ValueKind != JsonValueKind.Object) return "Invalid shape data."; if (geometry.RootElement.TryGetProperty("points", out var points)) { if (points.ValueKind != JsonValueKind.Array || points.GetArrayLength() is < 2 or > 64) return "Use between two and 64 shape points."; foreach (var point in points.EnumerateArray()) if (point.ValueKind != JsonValueKind.Array || point.GetArrayLength() != 2 || point.EnumerateArray().Any(v => v.ValueKind != JsonValueKind.Number || !v.TryGetDouble(out var n) || !double.IsFinite(n) || Math.Abs(n) > 10)) return "Invalid shape point."; } } catch (JsonException) { return "Invalid shape data."; } } if (d.Relationships.Select(x => x.RelationshipID).Distinct().Count() != d.Relationships.Count) return "Relationship identifiers must be unique."; foreach (var r in d.Relationships) if (!ids.Contains(r.SourceID) || !ids.Contains(r.TargetID) || r.SourceID == r.TargetID || (r.Direction is not null && !Directions.Contains(r.Direction)) || (r.RelativeDistance is not null && !Distances.Contains(r.RelativeDistance)) || r.MeasuredDistance is < 0 or > 999999999999999m || r.Unit?.Length > 30 || r.TypicalTravelTime?.Length > 120 || r.Notes?.Length > 1000) return "Check relationship endpoints, direction and distance."; return null; } }