using EDPlayerJournal.Entries;

namespace EDPlayerJournal.BGS;

/// <summary>
/// With ship targeted we might find out to which faction a given NPC belonged. This is
/// useful later when said NPC gets killed or murdered.
/// </summary>
internal class ShipTargetedParser : ITransactionParserPart {
    public void Parse(Entry e, TransactionParserContext context, TransactionParserOptions options, TransactionList transactions) {
        ShipTargetedEntry? entry = e as ShipTargetedEntry;
        if (entry == null) {
            throw new NotImplementedException();
        }

        // Scan happens in stages, and sometimes this information is not known
        // yet. Do now throw an error, this is expected behaviour.
        if (!string.IsNullOrEmpty(entry.PilotNameLocalised) &&
            !string.IsNullOrEmpty(entry.Faction)) {
            context.NPCFaction.TryAdd(entry.PilotNameLocalised, entry.Faction);
        }

        string? faction = context.LastRecordedAwardingFaction;

        // We have seen a captain?
        if (NPCs.IsWarzoneCaptain(entry.PilotName)) {
            // if we have faction information, we can compare it to figure out
            // whether it is the enemy or allied faction. but this is not always
            // possible. In such a case we assume we have seen the enemy captain.
            if (!string.IsNullOrEmpty(entry.Faction) &&
                !string.IsNullOrEmpty(faction)) {
                if (string.Compare(faction, entry.Faction) != 0) {
                    context.HaveSeenEnemyCaptain = true;
                } else {
                    context.HaveSeenAlliedCaptain = true;
                }
            } else {
                context.HaveSeenEnemyCaptain = true;
            }
        }

        // Spec ops?
        if (NPCs.IsSpecOps(entry.PilotName)) {
            context.HaveSeenSpecOps = true;
        }

        // Correspondent?
        if (NPCs.IsWarzoneCorrespondent(entry.PilotName)) {
            // if we have faction information, we can compare it to figure out
            // whether it is the enemy or allied faction. but this is not always
            // possible. In such a case we assume we have seen the enemy
            // correspondent.
            if (!string.IsNullOrEmpty(entry.Faction) &&
                !string.IsNullOrEmpty(faction)) {
                if (string.Compare(faction, entry.Faction) != 0) {
                    context.HaveSeenEnemyCorrespondent = true;
                } else {
                    context.HaveSeenAlliedCorrespondent = true;
                }
            } else {
                context.HaveSeenEnemyCorrespondent = true;
            }
        }
    }
}