using System.Text.Json;
namespace XFEEDToolBox.Core.Galaxy;
/// Mission destinations observed in the current commander's journal; never inferred from a BGS state.
public sealed class GalaxyJournalMarkers
{
private sealed record Mission(string? Destination,DateTimeOffset? Expires);
private readonly Dictionary missions=[];
private readonly Dictionary goals=[];
public void Clear(){missions.Clear();goals.Clear();}
public IReadOnlySet MissionSystems(DateTimeOffset now)=>missions.Values.Where(m=>m.Destination is {Length:>0}&&(m.Expires is null||m.Expires>now)).Select(m=>m.Destination!).ToHashSet(StringComparer.OrdinalIgnoreCase);
public IReadOnlySet CommunityGoalSystems(DateTimeOffset now)=>goals.Values.Where(m=>m.Destination is {Length:>0}&&m.Expires>now).Select(m=>m.Destination!).ToHashSet(StringComparer.OrdinalIgnoreCase);
public void Apply(JsonElement e,DateTimeOffset at)
{
string? Text(string key)=>e.TryGetProperty(key,out var value)&&value.ValueKind==JsonValueKind.String&&value.GetString() is {Length:>0} text?text:null;
if(Text("event")=="CommunityGoal")
{
if(!e.TryGetProperty("CurrentGoals",out var current)||current.ValueKind!=JsonValueKind.Array)return;
goals.Clear();foreach(var row in current.EnumerateArray())
{
if(!row.TryGetProperty("CGID",out var key)||!key.TryGetInt64(out var id)||id<=0||!row.TryGetProperty("SystemName",out var name)||name.ValueKind!=JsonValueKind.String)continue;
if(!row.TryGetProperty("IsComplete",out var complete)||complete.ValueKind!=JsonValueKind.False||!row.TryGetProperty("Expiry",out var expiry)||expiry.ValueKind!=JsonValueKind.String||!DateTimeOffset.TryParse(expiry.GetString(),out var expires))continue;
goals[id]=new(name.GetString(),expires);
}
return;
}
if(Text("event") is "CommunityGoalDiscard" or "CommunityGoalReward")
{if(e.TryGetProperty("CGID",out var key)&&key.TryGetInt64(out var id))goals.Remove(id);return;}
if(Text("event")=="Missions")
{
if(!e.TryGetProperty("Active",out var active)||active.ValueKind!=JsonValueKind.Array)return;
var snapshot=new Dictionary();
foreach(var row in active.EnumerateArray())
{
if(!row.TryGetProperty("MissionID",out var number)||!number.TryGetInt64(out var id)||id<=0)continue;
var expiry=row.TryGetProperty("Expires",out var remaining)&&remaining.TryGetDouble(out var seconds)&&double.IsFinite(seconds)&&seconds is >=0 and <315360000?at.AddSeconds(seconds):(DateTimeOffset?)null;
snapshot[id]=new(missions.GetValueOrDefault(id)?.Destination,expiry);
}
missions.Clear();foreach(var entry in snapshot)missions.Add(entry.Key,entry.Value);return;
}
if(!e.TryGetProperty("MissionID",out var value)||!value.TryGetInt64(out var missionId)||missionId<=0)return;
switch(Text("event"))
{
case "MissionCompleted" or "MissionFailed" or "MissionAbandoned":missions.Remove(missionId);break;
case "MissionAccepted":
missions[missionId]=new(Text("NewDestinationSystem")??Text("DestinationSystem"),DateTimeOffset.TryParse(Text("Expiry"),out var expiry)?expiry:null);break;
case "MissionRedirected":
var old=missions.GetValueOrDefault(missionId);if(old is not null)missions[missionId]=old with{Destination=Text("NewDestinationSystem")??Text("DestinationSystem")??old.Destination};break;
}
}
}
public static class GalaxyMarkerSources
{
public static string Describe(IReadOnlySet? values)
{
var sources=new List();
bool Selected(params string[] keys)=>values is null||keys.Any(values.Contains);
if(Selected("galacticregion","fleetcarrier","damaged","repairing","underattack","abandoned","drydock","rescuemegaship","titan","counterstrike","poweractivity"))sources.Add("公共目录:42个银河大区、站点/航母状态及势力活动;资料为上报记录,银河大区与原游戏命名区域各自独立");
if(Selected("mission","ship","navigation","communitygoal"))sources.Add("当前账号:有效任务的明确目的地、已知停放飞船、当前航路和已订阅社区活动");
if(Selected("communitygoal"))sources.Add("社区活动另接原游戏已发布缓存地址;缓存完整性/时效未确认,不覆盖Journal订阅状态");
if(Selected("engineer"))sources.Add("工程师:已核验驻地目录");
if(Selected("traderoute"))sources.Add("贸易路线:账号原游戏缓存的带时间记录");
if(Selected("bookmark","squadronbookmark"))sources.Add("书签:原游戏当前账号管理器;需支持此接口的游戏连接,完整加载状态未确认");
if(Selected("friend")||Selected("wing"))sources.Add("好友/队伍位置读取原管理器,仅时效确认为实时的位置可显示");
var pending=new Dictionary{{"starterzone","新手区"},{"conflictzone","冲突区"},{"powerstrategic","势力战略"}}.Where(x=>Selected(x.Key)).Select(x=>x.Value).ToArray();
if(pending.Length>0)sources.Add("待原游戏实时图层确认:"+string.Join("、",pending));
return string.Join(";",sources)+"。缺少记录保持未知。";
}
}