using EDPlayerJournal;
using EDPlayerJournal.Entries;
using System.Diagnostics.Metrics;

namespace EDPlayerJournalTests;

[TestClass]
public class MissionTest {

    [TestMethod]
    public void TestMissionAccepted() {
        string altruism = /*lang=json,strict*/ """{ "timestamp":"2022-11-23T08:05:54Z", "event":"MissionAccepted", "Faction":"HIP 6182 Federal Inc", "Name":"Mission_AltruismCredits", "LocalisedName":"Donate 1,000,000 Cr to the cause", "Donation":"1000000", "Expiry":"2022-11-23T11:50:52Z", "Wing":false, "Influence":"++", "Reputation":"++", "MissionID":901601358 }""";

        Entry? entry = Entry.Parse(altruism);

        Assert.IsNotNull(entry);
        Assert.IsInstanceOfType(entry, typeof(MissionAcceptedEntry));

        MissionAcceptedEntry? accepted = entry as MissionAcceptedEntry;

        Assert.IsNotNull(accepted);
        Assert.IsNotNull(accepted.Mission);

        Assert.AreEqual(accepted.Mission.MissionID, 901601358UL);
        Assert.AreEqual(accepted.Mission.Faction, "HIP 6182 Federal Inc");
        Assert.AreEqual(accepted.Mission.Name, "Mission_AltruismCredits");
        Assert.AreEqual(accepted.Mission.LocalisedName, "Donate 1,000,000 Cr to the cause");
        Assert.AreEqual(accepted.Mission.Donation, 1000000UL);
        Assert.AreEqual(accepted.Mission.Expiry, "2022-11-23T11:50:52Z");
        Assert.AreEqual(accepted.Mission.Wing, false);
        Assert.AreEqual(accepted.Mission.Reputation, "++");
        Assert.AreEqual(accepted.Mission.Influence, "++");
    }

    [TestMethod]
    public void NoInfluence() {
        string noinfluence = """{ "timestamp":"2022-02-24T19:42:38Z", "event":"MissionCompleted", "Faction":"Social LHS 6103 Confederation", "Name":"Mission_Hack_BLOPS_Elections_name", "MissionID":849749964, "NewDestinationSystem":"Dewikum", "DestinationSystem":"LF 8 +16 41", "Target":"$MissionUtil_Settlement_Target_PostBox;", "Target_Localised":"Hub Access Terminal", "Reward":14266, "FactionEffects":[ { "Faction":"", "Effects":[ { "Effect":"$MISSIONUTIL_Interaction_Summary_EP_down;", "Effect_Localised":"The economic status of $#MinorFaction; has declined in the $#System; system.", "Trend":"DownBad" } ], "Influence":[ { "SystemAddress":251012319587, "Trend":"DownBad", "Influence":"+" } ], "ReputationTrend":"DownBad", "Reputation":"+" }, { "Faction":"Social LHS 6103 Confederation", "Effects":[  ], "Influence":[  ], "ReputationTrend":"UpGood", "Reputation":"+" } ] }""";

        Entry? entry = Entry.Parse(noinfluence);

        Assert.IsNotNull(entry);
        Assert.IsInstanceOfType(entry, typeof(MissionCompletedEntry));

        MissionCompletedEntry? completed = entry as MissionCompletedEntry;

        Assert.IsNotNull(completed);
        Assert.IsNotNull(completed.Mission);

        Mission m = completed.Mission;

        Assert.AreEqual(m.FactionEffects.Count, 2);

        // Empty faction should not count towards affected factions
        Assert.AreEqual(m.AffectedFactions.Length, 1);

        MissionFactionEffects e;
        
        e = m.FactionEffects[0];
        Assert.IsTrue(e.IsEmptyFaction);
        Assert.AreEqual(e.Faction, string.Empty);

        var influence = m.GetInfluenceForFaction("", 251012319587UL);
        Assert.IsNotNull(influence);
        Assert.AreEqual(influence[0].Influence, "+");

        e = m.FactionEffects[1];
        Assert.AreEqual(e.Faction, "Social LHS 6103 Confederation");
    }

    [TestMethod]
    public void TestMissionCompleted() {
        string courier = /*lang=json,strict*/ """{ "timestamp":"2022-11-21T16:14:33Z", "event":"MissionCompleted", "Faction":"Salus Imperial Society", "Name":"Mission_Courier_Elections_name", "MissionID":901326396, "TargetFaction":"Salus Imperial Society", "DestinationSystem":"Saelishi", "DestinationStation":"Serrao Arsenal", "Reward":87404, "FactionEffects":[ { "Faction":"Salus Imperial Society", "Effects":[ { "Effect":"$MISSIONUTIL_Interaction_Summary_EP_up;", "Effect_Localised":"The economic status of $#MinorFaction; has improved in the $#System; system.", "Trend":"UpGood" }, { "Effect":"$MISSIONUTIL_Interaction_Summary_EP_up;", "Effect_Localised":"The economic status of $#MinorFaction; has improved in the $#System; system.", "Trend":"UpGood" } ], "Influence":[ { "SystemAddress":1865919973739, "Trend":"UpGood", "Influence":"++" }, { "SystemAddress":1733186884306, "Trend":"UpGood", "Influence":"++" } ], "ReputationTrend":"UpGood", "Reputation":"++" } ] }""";

        Entry? entry = Entry.Parse(courier);

        Assert.IsNotNull(entry);
        Assert.IsInstanceOfType(entry, typeof(MissionCompletedEntry));

        MissionCompletedEntry? completed = entry as MissionCompletedEntry;

        Assert.IsNotNull(completed);
        Assert.IsNotNull(completed.Mission);

        Mission m = completed.Mission;

        Assert.AreEqual(m.Faction, "Salus Imperial Society");
        Assert.AreEqual(m.Name, "Mission_Courier_Elections_name");
        Assert.AreEqual(m.MissionID, 901326396UL);
        Assert.AreEqual(m.TargetFaction, "Salus Imperial Society");
        Assert.AreEqual(m.DestinationSystem, "Saelishi");
        Assert.AreEqual(m.DestinationStation, "Serrao Arsenal");
        Assert.IsNull(m.DestinationSettlement);
        Assert.AreEqual(m.Reward, 87404UL);

        Assert.AreEqual(m.FactionEffects.Count, 1);

        var effect = m.FactionEffects[0];

        Assert.AreEqual(effect.Influences.Count, 2);
        Assert.AreEqual(effect.Effects.Count, 2);

        Assert.AreEqual(effect.Reputation, "++");

        var influence = m.GetInfluenceForFaction("Salus Imperial Society", 1865919973739UL);
        Assert.IsNotNull(influence);
        Assert.IsTrue(influence.Length > 0);
        Assert.AreEqual(influence[0].Influence, "++");

        influence = m.GetInfluenceForFaction("Salus Imperial Society", 1733186884306UL);
        Assert.IsNotNull(influence);
        Assert.IsTrue(influence.Length > 0);
        Assert.AreEqual(influence[0].Influence, "++");

        influence = m.GetInfluenceForFaction("Saelishi Saxons", 1733186884306UL);
        Assert.IsNotNull(influence);
        Assert.AreEqual(influence.Length, 0);

        // Only one entry are we only have Salus
        Assert.AreEqual(m.Influences.Count, 1);
        Assert.AreEqual(m.Influences["Salus Imperial Society"].Count, 2);
        Assert.AreEqual(m.Influences["Salus Imperial Society"][1865919973739UL].Influence, "++");
        Assert.AreEqual(m.Influences["Salus Imperial Society"][1733186884306UL].Influence, "++");
    }

    [TestMethod]
    public void MissionsTest() {
        string missionsstr = /*lang=json,strict*/ """{ "timestamp":"2022-11-15T12:40:45Z", "event":"Missions", "Active":[ { "MissionID":900147917, "Name":"Mission_Courier_name", "PassengerMission":false, "Expires":26848 }, { "MissionID":900148007, "Name":"Mission_Courier_name", "PassengerMission":false, "Expires":26848 } ], "Failed":[  ], "Complete":[  ] }""";

        Entry? entry = Entry.Parse(missionsstr);

        Assert.IsNotNull(entry);
        Assert.IsInstanceOfType(entry, typeof(MissionsEntry));

        MissionsEntry? missions = entry as MissionsEntry;

        Assert.IsNotNull(missions);

        Assert.AreEqual(missions.Active.Count, 2);
        Assert.AreEqual(missions.Failed.Count, 0);
        Assert.AreEqual(missions.Complete.Count, 0);

        Mission m = missions.Active[0];
        Assert.AreEqual(m.MissionID, 900147917UL);
        Assert.AreEqual(m.Name, "Mission_Courier_name");
        Assert.AreEqual(m.Expires, 26848UL);
    }
}
