/* as of June 25 2011:
 * Disembark ship to planet: shiplocker  disembark suit loadout  backpack  shiplocker
 * Disembark ship to station:  disembark  suit loadout  backpack  ship locker
 * Board ship:  shiplocker embark loadout shiplocker
 * Exit suit loadout menu, after loadout change, plus other places: SuitLoadout Backpack Ship locker
 * Throwing a grenade causes:  BackpackChange/Removed event.
 * Using a consumable: UseConsumable BackpackChange/Removed.
 * Collect item: CollectItem BackpackChanged/Added.
 * Drop item: DropItem BackpackChanged/Removed.
 * Odyssey 3: Buy MR: BuyMicroResource ShipLocker
 * Odyssey 3: Trade MR: Shiplocker TradeMicroResource
 * Odyssey 3: Sell MR: ShipLocker SellMicroResources
 */

        public List <HistoryEntry> ReorderRemove(HistoryEntry he)
        {
            if (EliteConfigInstance.InstanceOptions.DisableMerge)
            {
                return new List <HistoryEntry> {
                           he
                }
            }
            ;

            if (historylist.Count > 0)
            {
                // we generally try and remove these as spam if they did not do anything
                if (he.EntryType == JournalTypeEnum.Cargo || he.EntryType == JournalTypeEnum.Materials)
                {
                    var lasthe = historylist.Last();
                    if (lasthe.MaterialCommodity != he.MaterialCommodity)   // they changed the mc list, keep
                    {
                        System.Diagnostics.Debug.WriteLine(he.EventTimeUTC.ToString() + " " + he.EntryType.ToString() + " Update,keep");
                    }
                    else
                    {
                        //System.Diagnostics.Debug.WriteLine(he.EventTimeUTC.ToString() + " " + he.EntryType.ToString() + " No update, remove");
                        return(null);
                    }
                }
                // these we try and stop repeats
                else if (he.EntryType == JournalTypeEnum.Outfitting || he.EntryType == JournalTypeEnum.Shipyard)
                {
                    HistoryEntry lasthe = FindBeforeLastDockLoadGameShutdown(1000, he.EntryType);     // don't look back forever
                    if (lasthe != null)
                    {
                        System.Diagnostics.Debug.WriteLine(he.EventTimeUTC.ToString() + " " + he.EntryType.ToString() + " Duplicate with " + lasthe.EventTimeUTC.ToString() + " remove");
                        return(null);
                    }
                }
                // these we try and stop repeats
                else if (he.EntryType == JournalTypeEnum.EDDCommodityPrices || he.EntryType == JournalTypeEnum.Market)
                {
                    HistoryEntry lasthe = FindBeforeLastDockLoadGameShutdown(1000, JournalTypeEnum.Market, JournalTypeEnum.EDDCommodityPrices);     // don't look back forever
                    if (lasthe != null)
                    {
                        System.Diagnostics.Debug.WriteLine(he.EventTimeUTC.ToString() + " " + he.EntryType.ToString() + " Duplicate with " + lasthe.EntryType.ToString() + " " + lasthe.EventTimeUTC.ToString() + " remove");
                        return(null);
                    }
                }
            }

            if (he.EventTimeUTC >= new DateTime(2021, 6, 17))        // this stuff only works on journals after odyssey 3 update
            {
                JournalTypeEnum queuetype = reorderqueue.Count > 0 ? reorderqueue[0].EntryType : JournalTypeEnum.Unknown;

                //System.Diagnostics.Debug.WriteLine("-> {0} {1} Reorder queue {2} {3}", he.EventTimeUTC.ToString(), he.EntryType, queuetype, reorderqueue.Count);

                if (he.EntryType == JournalTypeEnum.Embark || he.EntryType == JournalTypeEnum.Disembark)
                {
                    if (queuetype == JournalTypeEnum.ShipLocker)        // disembark to planet : SL-DIS-SuitLoadout-Backpack-Shiplocker
                    {
                        //System.Diagnostics.Debug.WriteLine("-> Embark/disembark after shiplocker, discard shiplocker");
                        reorderqueue.Clear();
                    }

                    var prevreorder = reorderqueue;
                    reorderqueue = new List <HistoryEntry> {
                        he
                    };                                                  // reset the queue, and run with it
                    return(prevreorder);                                // if anything queued up, play it out..
                }
                else if (he.EntryType == JournalTypeEnum.SuitLoadout)   // either part of a Embark/Disembark - Suit loadout - Backpack - Ship locker sequence, or SL-BP-Ship locker
                {
                    if (queuetype == JournalTypeEnum.Disembark || queuetype == JournalTypeEnum.Embark)
                    {
                        reorderqueue.Add(he);
                        return(null);
                    }
                    else
                    {
                        var prevreorder = reorderqueue;
                        reorderqueue = new List <HistoryEntry> {
                            he
                        };                                                  // reset the queue, and run with it
                        return(prevreorder);                                // if anything queued up, play it out..
                    }
                }
                else if (he.EntryType == JournalTypeEnum.Backpack)
                {
                    if (queuetype == JournalTypeEnum.Disembark || queuetype == JournalTypeEnum.SuitLoadout)  // if part of this queue
                    {
                        reorderqueue.Add(he);
                        return(null);
                    }
                    else
                    {
                        //System.Diagnostics.Debug.WriteLine("Isolated back pack, remove");
                        return(null);
                    }
                }
                else if (he.EntryType == JournalTypeEnum.Loadout)
                {
                    if (queuetype == JournalTypeEnum.Embark)  // if part of this queue
                    {
                        reorderqueue.Add(he);
                        return(null);
                    }
                    else
                    {
                        //System.Diagnostics.Debug.WriteLine("Isolated loadout, let through");
                    }
                }
                else if (he.EntryType == JournalTypeEnum.ShipLocker)
                {
                    if (queuetype == JournalTypeEnum.Disembark || queuetype == JournalTypeEnum.Embark || queuetype == JournalTypeEnum.SuitLoadout) // if part of this queue
                    {
                        he.ReplaceJournalEntry(reorderqueue[0].journalEntry, he.EventTimeUTC);                                                     // move first entry to here
                        reorderqueue.Clear();
                        return(new List <HistoryEntry> {
                            he
                        });
                    }
                    else
                    {
                        //System.Diagnostics.Debug.WriteLine("Isolated ship locker, remove");
                        return(null);
                    }
                }
                else if (he.EntryType == JournalTypeEnum.CollectItems || he.EntryType == JournalTypeEnum.DropItems || he.EntryType == JournalTypeEnum.UseConsumable)
                {
                    var prevreorder = reorderqueue;
                    reorderqueue = new List <HistoryEntry> {
                        he
                    };                                                  // reset the queue, and run with it
                    return(prevreorder);                                // if anything queued up, play it out..
                }
                else if (he.EntryType == JournalTypeEnum.BackpackChange)
                {
                    var bp = he.journalEntry as JournalBackpackChange;

                    // if its a grenade, use alchemy to turn it back into a use consumable
                    if (bp.ThrowGrenade)
                    {
                        //System.Diagnostics.Debug.WriteLine(he.EventTimeUTC.ToString() + " Throw grenade, use Alchemy");
                        he.ReplaceJournalEntry(new JournalUseConsumable(bp.EventTimeUTC, bp.Removed[0], bp.TLUId, bp.CommanderId, bp.Id), he.EventTimeUTC);
                    }
                    else if (queuetype == JournalTypeEnum.CollectItems || queuetype == JournalTypeEnum.DropItems || queuetype == JournalTypeEnum.UseConsumable)
                    {
                        //System.Diagnostics.Debug.WriteLine(he.EventTimeUTC.ToString() + " " + queuetype + " use Alchemy ");
                        he.ReplaceJournalEntry(reorderqueue[0].journalEntry, he.EventTimeUTC);
                        reorderqueue.Clear();
                    }
                    else
                    {           // KEEP - transfer from ship does shiplocker/backpackchange
                        //System.Diagnostics.Debug.WriteLine(he.EventTimeUTC.ToString() + " Backpackchange keep");
                    }
                }
            }

            return(new List <HistoryEntry> {
                he
            });                                         // pass it through
        }