using System.Text;
using EDPlayerJournal.Entries;

namespace EDPlayerJournal.BGS;

public class Cartographics : Transaction {
    public Cartographics() { }

    public Cartographics(MultiSellExplorationDataEntry e) {
        Entries.Add(e);
    }

    public Cartographics(SellExplorationDataEntry e) {
        Entries.Add(e);
    }

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

        Cartographics? b = other as Cartographics;
        if (b != null && 
            b.System == System && 
            b.Faction == Faction && 
            b.Station == Station) {
            return 0;
        }

        return -1;
    }

    public long TotalSum {
        get {
            /* add multi sell and normal ones together */
            long total =
                Entries.OfType<MultiSellExplorationDataEntry>()
                .Sum(x => x.TotalEarnings)
                +
                Entries.OfType<SellExplorationDataEntry>()
                .Sum(x => x.TotalEarnings)
                ;
            return total;
        }
    }

    public override string ToString() {
        StringBuilder builder = new StringBuilder();
        builder.AppendFormat("Sold {0} worth of Cartographics Data", Credits.FormatCredits(TotalSum));
        return builder.ToString();
    }

    /// <summary>
    /// Cartographics only help the controlling faction.
    /// </summary>
    public override bool OnlyControllingFaction => true;
}