using System.Linq;
using Microsoft.Extensions.Logging;
using NBitcoin;
using Stratis.Bitcoin.Configuration;
using Stratis.Bitcoin.Features.MemoryPool.Fee;
using Stratis.Bitcoin.Utilities;

namespace Stratis.Bitcoin.Features.MemoryPool
{
    public class DeStreamTxMempool : TxMempool
    {
        public DeStreamTxMempool(
            IDateTimeProvider dateTimeProvider,
            BlockPolicyEstimator blockPolicyEstimator,
            ILoggerFactory loggerFactory,
            NodeSettings nodeSettings)
            : base(dateTimeProvider, blockPolicyEstimator, loggerFactory, nodeSettings)
        {
        }

        /// <inheritdoc />
        protected override void RemoveConflicts(Transaction tx)
        {
            foreach (TxIn txInput in tx.Inputs.RemoveChangePointer())
            {
                NextTxPair it = this.MapNextTx.FirstOrDefault(p => p.OutPoint == txInput.PrevOut);
                if (it == null) continue;

                Transaction txConflict = it.Transaction;
                if (txConflict == tx) continue;

                this.ClearPrioritisation(txConflict.GetHash());
                this.RemoveRecursive(txConflict);
            }
        }
    }
}