using System.Collections.Generic;
using System.Linq;
using System.Text;
using EDPlayerJournal.BGS;

namespace EliteBGS.LogGenerator;

public class MissionFormat : LogFormatter {
    public string GenerateLog(Objective objective) {
        Dictionary<string, Dictionary<string, int>> collated = new();
        Dictionary<string, ulong> passengers = new();
        StringBuilder output = new StringBuilder();
        int total_influence = 0;

        var missions = objective.EnabledOfType<MissionCompleted>();
        var support = objective.EnabledOfType<InfluenceSupport>();

        if ((missions == null || missions.Count == 0) &&
            (support == null || support.Count == 0)) {
            return "";
        }

        foreach (MissionCompleted m in missions) {
            if (!collated.ContainsKey(m.MissionName)) {
                collated[m.MissionName] = new Dictionary<string, int>();
            }
            if (!collated[m.MissionName].ContainsKey(m.Influence)) {
                collated[m.MissionName][m.Influence] = 0;
            }

            ++collated[m.MissionName][m.Influence];

            total_influence += m.Influence.Length;

            if (m.AcceptedEntry != null &&
                m.AcceptedEntry.Mission != null &&
                m.AcceptedEntry.Mission.IsPassengerMission) {
                if (!passengers.ContainsKey(m.MissionName)) {
                    passengers[m.MissionName] = 0;
                }
                passengers[m.MissionName] += (m.AcceptedEntry.Mission.PassengerCount ?? 0);
            }
        }

        foreach (var mission in collated) {
            output.AppendFormat("{0}: ", mission.Key);
            output.Append("(");
            foreach (var influence in mission.Value.OrderBy(x => x.Key.Length)) {
                output.AppendFormat("Inf{0} x{1}, ", influence.Key, influence.Value);
            }
            output.Remove(output.Length - 2, 2); // remove last ", "
            output.Append(")");

            if (passengers.ContainsKey(mission.Key)) {
                output.AppendFormat(" ({0} Passengers)", passengers[mission.Key]);
            }

            output.Append("\n");
        }

        output.Append("\n");

        foreach (InfluenceSupport inf in support) {
            output.Append(inf.ToString());
            output.Append("\n");
            total_influence += inf.Influence.Length;
        }

        if (support.Count() > 0) {
            output.Append("\n");
        }

        if (total_influence > 0) {
            output.AppendFormat("Total Influence: {0}", total_influence);
        }

        return output.ToString().Trim();
    }

    public string GenerateSummary(Objective objective) {
        long influence = objective
            .EnabledOfType<MissionCompleted>()
            .Sum(x => x.Influence.Length)
            ;
        long support = objective
            .EnabledOfType<InfluenceSupport>()
            .Sum(x => x.Influence.Length)
            ;

        if (influence + support <= 0) {
            return "";
        }

        return string.Format("INF: {0}", influence + support);
    }
}
