using System.Text;
using EDPlayerJournal.Entries;

namespace EDPlayerJournal.BGS;

/// <summary>
/// 36 Lessons of Vivec, Sermon Thirty Six
/// </summary>
public class FoulMurder : Transaction {
    public FoulMurder() { }
    public FoulMurder (CommitCrimeEntry e) {
        Entries.Add(e);
    }

    public string? CrimeType {
        get { return Entries.OfType<CommitCrimeEntry>().First().CrimeType; }
    }

    public long Bounties => Entries.OfType<CommitCrimeEntry>().Sum(x => x.Bounty);

    public long Fines => Entries.OfType<CommitCrimeEntry>().Sum(x => x.Fine);

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

        FoulMurder? hortator = other as FoulMurder;
        if (hortator != null && 
            Faction == hortator.Faction && 
            CrimeType == hortator.CrimeType) {
            return 0;
        }

        return -1;
    }

    public override string ToString() {
        StringBuilder builder = new StringBuilder();
        string type;

        if (CrimeType == CrimeTypes.Murder) {
            if (Entries.Count > 1) {
                type = "ships";
            } else {
                type = "ship";
            }
        } else {
            if (Entries.Count > 1) {
                type = "people";
            } else {
                type = "person";
            }
        }

        builder.AppendFormat("Murdered {0} {1} of {2} (Bounties: {3}, Fines: {4})",
            Entries.Count,
            type,
            Faction,
            Credits.FormatCredits(Bounties),
            Credits.FormatCredits(Fines)
            );

        return builder.ToString();
    }
}