using EDPlayerJournal.Entries;

namespace EDPlayerJournal.BGS;

internal class MarketBuyParser : ITransactionParserPart {
    public void Parse(Entry e, TransactionParserContext context, TransactionParserOptions options, TransactionList transactions) {
        MarketBuyEntry? entry = e as MarketBuyEntry;
        if (entry == null) {
            throw new NotImplementedException();
        }

        context.BoughtCargo(entry.Type, entry.BuyPrice);

        // We still want the information on buy price for profit,
        // but if the option is on, we don't care for parsing it
        // further.
        // TODO: might be wise to split this parser into two; one for
        //       determining profit, the other for the BGS information
        if (options.IgnoreMarketBuy) {
            return;
        }

        if (context.StationOwner == null) {
            transactions.AddIncomplete(
                new BuyCargo(),
                "Could not discern the station owner for market buy.",
                e);
            return;
        }

        // Ignore if its a fleet carrier faction.
        if (string.Compare(context.StationOwner, Factions.FleetCarrier, StringComparison.OrdinalIgnoreCase) == 0 &&
            options.IgnoreFleetCarrierFaction) {
            return;
        }

        transactions.Add(new BuyCargo(entry) {
            System = context.CurrentSystem,
            Station = context.CurrentStation,
            Faction = context.StationOwner,
            IsLegacy = context.IsLegacy,
        });
    }
}
