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

namespace EliteBGS.LogGenerator;

class CombatZoneFormat : LogFormatter {
    public string GenerateLog(Objective objective) {
        var logs = objective
            .EnabledOfType<CombatZone>()
            .OrderBy(x => (CombatZones.DifficultyRank(x.Grade) ?? 0))
            .GroupBy(x => new { x.Type, x.Grade, x.Settlement })
            .ToDictionary(x => x.Key, x => x.ToList())
            ;
        StringBuilder builder = new StringBuilder();

        if (logs == null || logs.Count() <= 0) {
            return "";
        }

        foreach (var log in logs) {
            int optionals = log.Value
                                .Sum(x => x.OptionalObjectivesCompleted)
                                ;
            var settlements = log.Value
                                .Select(x => x.Settlement)
                                .Distinct()
                                ;
            string settl = string.Join(", ", settlements);
            if (!string.IsNullOrEmpty(log.Key.Grade)) {
                builder.AppendFormat("Won {0}x {1} {2} Combat Zone(s)",
                    log.Value.Count,
                    log.Key.Grade,
                    log.Key.Type
                    );
            } else {
                builder.AppendFormat("Won {0}x {1} Combat Zone(s)",
                    log.Value.Count,
                    log.Key.Type
                    );
            }

            if (optionals > 0) {
                builder.AppendFormat(" (with {0} optional objectives)", optionals);
            }
            if (!string.IsNullOrEmpty(settl)) {
                builder.AppendFormat(" (at {0})", settl);
            }
            builder.Append("\n");
        }

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

    public string GenerateSummary(Objective objective) {
        var logs = objective
            .EnabledOfType<CombatZone>()
            .OrderBy(x => (CombatZones.DifficultyRank(x.Grade) ?? 0))
            .GroupBy(x => new { x.Type, x.Grade })
            .ToDictionary(x => x.Key, x => x.ToList())
            ;
        StringBuilder builder = new StringBuilder();

        if (logs == null || logs.Count() <= 0) {
            return "";
        }

        foreach (var log in logs) {
            int optionals = log.Value
                                .Sum(x => x.OptionalObjectivesCompleted)
                                ;
            if (builder.Length > 0) {
                builder.Append(", ");
            }
            if (!string.IsNullOrEmpty(log.Key.Grade)) {
                string grade = log.Key.Grade.Substring(0, 1);
                if (log.Key.Grade == CombatZones.DifficultyVeryHigh) {
                    grade = "VH";
                }
                builder.AppendFormat("CZ: {0}x{1}{2}",
                    log.Value.Count,
                    grade,
                    log.Key.Type.Substring(0, 1)
                    );
            } else {
                builder.AppendFormat("CZ: {0}x?{1}",
                    log.Value.Count,
                    log.Key.Type.Substring(0, 1)
                    );
            }

            if (optionals > 0) {
                builder.AppendFormat("+ {0} OPTS", optionals);
            }
        }

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