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

namespace EliteBGS.LogGenerator;

public class VoucherFormat : LogFormatter {
    public string GenerateLog(Objective objective) {
        StringBuilder builder = new StringBuilder();
        var missions = objective
            .EnabledOfType<Vouchers>()
            .GroupBy(x => x.Type)
            .ToDictionary(x => x.Key, x => x.ToList())
            ;

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

        foreach (var m in missions) {
            long total = (long)m.Value.Sum(x => (decimal)x.TotalSum);
            builder.AppendFormat("Handed in {0} vouchers: {1}\n", m.Key, Credits.FormatMillions(total));
        }

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

    public string GenerateSummary(Objective objective) {
        long bounties = objective
            .EnabledOfType<Vouchers>()
            .Where(x => x.Type == "Bounty")
            .Sum(x => x.TotalSum)
            ;
        long bonds = objective
            .EnabledOfType<Vouchers>()
            .Where(x => x.Type == "Combat Bond")
            .Sum(x => x.TotalSum)
            ;

        StringBuilder sb = new();

        if (bounties > 0) {
            sb.AppendFormat("Bounties: {0}", Credits.FormatMillions(bounties));
        }

        if (bonds > 0) {
            if (sb.Length > 0) {
                sb.Append(", ");
            }
            sb.AppendFormat("Bonds: {0}", Credits.FormatMillions(bonds));
        }

        return sb.ToString();
    }
}
