using System.Globalization;
using EDPlayerJournal.Entries;

namespace EDPlayerJournal.BGS;

public class Vouchers : Transaction {
    private string? type = null;

    public Vouchers() {
    }

    public Vouchers(RedeemVoucherEntry e) {
        Entries.Add(e);
    }

    public long TotalSum {
        get {
            if (Faction == null) {
                return 0;
            }
            return Entries
                .OfType<RedeemVoucherEntry>()
                .ToList()
                .Sum(x => (long)x.GetBountyForFaction(Faction))
                ;
        }
    }

    public string? Type {
        get {
            if (Entries == null) {
                return null;
            }
            if (type == null) {
                string? v = Entries
                             .OfType<RedeemVoucherEntry>()
                             .GroupBy(x => x.Type)
                             .Select(x => x.Key)
                             .First();
                if (v == null) {
                    return null;
                } else if (v == Bonds.CombatBond) {
                    type = "Combat Bond";
                } else if (v == Bonds.Bounty) {
                    type = "Bounty";
                } else {
                    type = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(v);
                }
            }

            return type;
        }
    }

    public override int CompareTo(Transaction? other) {
        if (other == null || other.GetType() != typeof(Vouchers)) {
            return -1;
        }

        Vouchers? b = other as Vouchers;
        if (b?.Type == Type &&
            b?.Faction == Faction &&
            b?.System == System &&
            b?.Station == Station) {
            return 0;
        }

        return -1;
    }

    public override string ToString() {
        return string.Format("{0} Vouchers: {1}", Type, Credits.FormatCredits(TotalSum));
    }
}
