// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.builtin.adventure.shop.barter;

import java.util.List;
import java.util.Collection;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.Nullable;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import java.time.temporal.TemporalAmount;
import java.time.temporal.Temporal;
import java.time.Duration;
import com.hypixel.hytale.server.core.modules.time.WorldTimeResource;
import java.time.ZoneId;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.Instant;
import java.util.concurrent.ConcurrentHashMap;
import java.io.IOException;
import com.hypixel.hytale.codec.Codec;
import java.nio.file.attribute.FileAttribute;
import org.bson.BsonDocument;
import org.bson.BsonValue;
import com.hypixel.hytale.codec.ExtraInfo;
import com.hypixel.hytale.server.core.util.BsonUtil;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.util.logging.Level;
import javax.annotation.Nonnull;
import java.util.Map;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import java.nio.file.Path;
import com.hypixel.hytale.logger.HytaleLogger;

public class BarterShopState
{
    private static final HytaleLogger LOGGER;
    private static BarterShopState instance;
    private static Path saveDirectory;
    public static final BuilderCodec<ShopInstanceState> SHOP_INSTANCE_CODEC;
    public static final BuilderCodec<BarterShopState> CODEC;
    private final Map<String, ShopInstanceState> shopStates;
    
    public static void initialize(@Nonnull final Path dataDirectory) {
        BarterShopState.saveDirectory = dataDirectory;
        load();
    }
    
    @Nonnull
    public static BarterShopState get() {
        if (BarterShopState.instance == null) {
            BarterShopState.instance = new BarterShopState();
        }
        return BarterShopState.instance;
    }
    
    public static void load() {
        if (BarterShopState.saveDirectory == null) {
            BarterShopState.LOGGER.at(Level.WARNING).log("Cannot load barter shop state: save directory not set");
            BarterShopState.instance = new BarterShopState();
            return;
        }
        final Path file = BarterShopState.saveDirectory.resolve("barter_shop_state.json");
        if (!Files.exists(file, new LinkOption[0])) {
            BarterShopState.LOGGER.at(Level.INFO).log("No saved barter shop state found, starting fresh");
            BarterShopState.instance = new BarterShopState();
            return;
        }
        try {
            final BsonDocument document = BsonUtil.readDocumentNow(file);
            if (document != null) {
                final ExtraInfo extraInfo = ExtraInfo.THREAD_LOCAL.get();
                BarterShopState.instance = BarterShopState.CODEC.decode(document, extraInfo);
                extraInfo.getValidationResults().logOrThrowValidatorExceptions(BarterShopState.LOGGER);
                BarterShopState.LOGGER.at(Level.INFO).log("Loaded barter shop state with %d shops", BarterShopState.instance.shopStates.size());
            }
            else {
                BarterShopState.instance = new BarterShopState();
            }
        }
        catch (final Exception e) {
            BarterShopState.LOGGER.at(Level.WARNING).withCause(e).log("Failed to load barter shop state, starting fresh");
            BarterShopState.instance = new BarterShopState();
        }
    }
    
    public static void save() {
        if (BarterShopState.saveDirectory == null || BarterShopState.instance == null) {
            return;
        }
        try {
            if (!Files.exists(BarterShopState.saveDirectory, new LinkOption[0])) {
                Files.createDirectories(BarterShopState.saveDirectory, (FileAttribute<?>[])new FileAttribute[0]);
            }
            final Path file = BarterShopState.saveDirectory.resolve("barter_shop_state.json");
            BsonUtil.writeSync(file, BarterShopState.CODEC, BarterShopState.instance, BarterShopState.LOGGER);
            BarterShopState.LOGGER.at(Level.FINE).log("Saved barter shop state");
        }
        catch (final IOException e) {
            BarterShopState.LOGGER.at(Level.WARNING).withCause(e).log("Failed to save barter shop state");
        }
    }
    
    public static void shutdown() {
        save();
        BarterShopState.instance = null;
    }
    
    public BarterShopState() {
        this.shopStates = new ConcurrentHashMap<String, ShopInstanceState>();
    }
    
    private static Instant calculateNextScheduledRestock(@Nonnull final Instant gameTime, final int intervalDays, final int restockHour) {
        final LocalDateTime dateTime = LocalDateTime.ofInstant(gameTime, ZoneOffset.UTC);
        final long daysSinceEpoch = Duration.between(WorldTimeResource.ZERO_YEAR, gameTime).toDays();
        final long currentCycle = daysSinceEpoch / intervalDays;
        long nextRestockDaySinceEpoch = (currentCycle + 1L) * intervalDays;
        final boolean isTodayRestockDay = daysSinceEpoch % intervalDays == 0L;
        if (isTodayRestockDay && dateTime.getHour() < restockHour) {
            nextRestockDaySinceEpoch = daysSinceEpoch;
        }
        final Instant nextRestockInstant = WorldTimeResource.ZERO_YEAR.plus((TemporalAmount)Duration.ofDays(nextRestockDaySinceEpoch)).plus((TemporalAmount)Duration.ofHours(restockHour));
        return nextRestockInstant;
    }
    
    @Nonnull
    public ShopInstanceState getOrCreateShopState(final BarterShopAsset asset, @Nonnull final Instant gameTime) {
        return this.shopStates.computeIfAbsent(asset.getId(), id -> {
            final ShopInstanceState state = new ShopInstanceState();
            state.resetStockAndResolve(asset);
            final RefreshInterval interval = asset.getRefreshInterval();
            if (interval != null) {
                state.setNextRefreshTime(calculateNextScheduledRestock(gameTime, interval.getDays(), asset.getRestockHour()));
            }
            return state;
        });
    }
    
    public void checkRefresh(final BarterShopAsset asset, @Nonnull final Instant gameTime) {
        final RefreshInterval interval = asset.getRefreshInterval();
        if (interval == null) {
            return;
        }
        final ShopInstanceState state = this.getOrCreateShopState(asset, gameTime);
        final Instant nextRefresh = state.getNextRefreshTime();
        if (nextRefresh == null) {
            state.setNextRefreshTime(calculateNextScheduledRestock(gameTime, interval.getDays(), asset.getRestockHour()));
            save();
            return;
        }
        if (!gameTime.isBefore(nextRefresh)) {
            state.resetStockAndResolve(asset);
            state.setNextRefreshTime(calculateNextScheduledRestock(gameTime, interval.getDays(), asset.getRestockHour()));
            save();
        }
    }
    
    public int[] getStockArray(final BarterShopAsset asset, @Nonnull final Instant gameTime) {
        this.checkRefresh(asset, gameTime);
        final ShopInstanceState state = this.getOrCreateShopState(asset, gameTime);
        if (state.expandStockIfNeeded(asset)) {
            save();
        }
        return state.getCurrentStock().clone();
    }
    
    @Nonnull
    public BarterTrade[] getResolvedTrades(final BarterShopAsset asset, @Nonnull final Instant gameTime) {
        this.checkRefresh(asset, gameTime);
        final ShopInstanceState state = this.getOrCreateShopState(asset, gameTime);
        return state.getResolvedTrades(asset);
    }
    
    public boolean executeTrade(final BarterShopAsset asset, final int tradeIndex, final int quantity, @Nonnull final Instant gameTime) {
        this.checkRefresh(asset, gameTime);
        final ShopInstanceState state = this.getOrCreateShopState(asset, gameTime);
        final boolean success = state.decrementStock(tradeIndex, quantity);
        if (success) {
            save();
        }
        return success;
    }
    
    static {
        // 
        // This method could not be decompiled.
        // 
        // Original Bytecode:
        // 
        //     3: putstatic       com/hypixel/hytale/builtin/adventure/shop/barter/BarterShopState.LOGGER:Lcom/hypixel/hytale/logger/HytaleLogger;
        //     6: ldc             Lcom/hypixel/hytale/builtin/adventure/shop/barter/BarterShopState$ShopInstanceState;.class
        //     8: invokedynamic   BootstrapMethod #1, get:()Ljava/util/function/Supplier;
        //    13: invokestatic    com/hypixel/hytale/codec/builder/BuilderCodec.builder:(Ljava/lang/Class;Ljava/util/function/Supplier;)Lcom/hypixel/hytale/codec/builder/BuilderCodec$Builder;
        //    16: new             Lcom/hypixel/hytale/codec/KeyedCodec;
        //    19: dup            
        //    20: ldc_w           "Stock"
        //    23: getstatic       com/hypixel/hytale/codec/Codec.INT_ARRAY:Lcom/hypixel/hytale/codec/codecs/array/IntArrayCodec;
        //    26: invokespecial   com/hypixel/hytale/codec/KeyedCodec.<init>:(Ljava/lang/String;Lcom/hypixel/hytale/codec/Codec;)V
        //    29: invokedynamic   BootstrapMethod #2, accept:()Ljava/util/function/BiConsumer;
        //    34: invokedynamic   BootstrapMethod #3, apply:()Ljava/util/function/Function;
        //    39: invokevirtual   com/hypixel/hytale/codec/builder/BuilderCodec$Builder.append:(Lcom/hypixel/hytale/codec/KeyedCodec;Ljava/util/function/BiConsumer;Ljava/util/function/Function;)Lcom/hypixel/hytale/codec/builder/BuilderField$FieldBuilder;
        //    42: invokevirtual   com/hypixel/hytale/codec/builder/BuilderField$FieldBuilder.add:()Lcom/hypixel/hytale/codec/builder/BuilderCodec$BuilderBase;
        //    45: checkcast       Lcom/hypixel/hytale/codec/builder/BuilderCodec$Builder;
        //    48: new             Lcom/hypixel/hytale/codec/KeyedCodec;
        //    51: dup            
        //    52: ldc_w           "NextRefresh"
        //    55: getstatic       com/hypixel/hytale/codec/Codec.INSTANT:Lcom/hypixel/hytale/codec/function/FunctionCodec;
        //    58: iconst_1       
        //    59: invokespecial   com/hypixel/hytale/codec/KeyedCodec.<init>:(Ljava/lang/String;Lcom/hypixel/hytale/codec/Codec;Z)V
        //    62: invokedynamic   BootstrapMethod #4, accept:()Ljava/util/function/BiConsumer;
        //    67: invokedynamic   BootstrapMethod #5, apply:()Ljava/util/function/Function;
        //    72: invokevirtual   com/hypixel/hytale/codec/builder/BuilderCodec$Builder.append:(Lcom/hypixel/hytale/codec/KeyedCodec;Ljava/util/function/BiConsumer;Ljava/util/function/Function;)Lcom/hypixel/hytale/codec/builder/BuilderField$FieldBuilder;
        //    75: invokevirtual   com/hypixel/hytale/codec/builder/BuilderField$FieldBuilder.add:()Lcom/hypixel/hytale/codec/builder/BuilderCodec$BuilderBase;
        //    78: checkcast       Lcom/hypixel/hytale/codec/builder/BuilderCodec$Builder;
        //    81: new             Lcom/hypixel/hytale/codec/KeyedCodec;
        //    84: dup            
        //    85: ldc_w           "ResolveSeed"
        //    88: getstatic       com/hypixel/hytale/codec/Codec.LONG:Lcom/hypixel/hytale/codec/codecs/simple/LongCodec;
        //    91: iconst_1       
        //    92: invokespecial   com/hypixel/hytale/codec/KeyedCodec.<init>:(Ljava/lang/String;Lcom/hypixel/hytale/codec/Codec;Z)V
        //    95: invokedynamic   BootstrapMethod #6, accept:()Ljava/util/function/BiConsumer;
        //   100: invokedynamic   BootstrapMethod #7, apply:()Ljava/util/function/Function;
        //   105: invokevirtual   com/hypixel/hytale/codec/builder/BuilderCodec$Builder.append:(Lcom/hypixel/hytale/codec/KeyedCodec;Ljava/util/function/BiConsumer;Ljava/util/function/Function;)Lcom/hypixel/hytale/codec/builder/BuilderField$FieldBuilder;
        //   108: invokevirtual   com/hypixel/hytale/codec/builder/BuilderField$FieldBuilder.add:()Lcom/hypixel/hytale/codec/builder/BuilderCodec$BuilderBase;
        //   111: checkcast       Lcom/hypixel/hytale/codec/builder/BuilderCodec$Builder;
        //   114: invokevirtual   com/hypixel/hytale/codec/builder/BuilderCodec$Builder.build:()Lcom/hypixel/hytale/codec/builder/BuilderCodec;
        //   117: putstatic       com/hypixel/hytale/builtin/adventure/shop/barter/BarterShopState.SHOP_INSTANCE_CODEC:Lcom/hypixel/hytale/codec/builder/BuilderCodec;
        //   120: ldc             Lcom/hypixel/hytale/builtin/adventure/shop/barter/BarterShopState;.class
        //   122: invokedynamic   BootstrapMethod #8, get:()Ljava/util/function/Supplier;
        //   127: invokestatic    com/hypixel/hytale/codec/builder/BuilderCodec.builder:(Ljava/lang/Class;Ljava/util/function/Supplier;)Lcom/hypixel/hytale/codec/builder/BuilderCodec$Builder;
        //   130: new             Lcom/hypixel/hytale/codec/KeyedCodec;
        //   133: dup            
        //   134: ldc_w           "Shops"
        //   137: new             Lcom/hypixel/hytale/codec/codecs/map/MapCodec;
        //   140: dup            
        //   141: getstatic       com/hypixel/hytale/builtin/adventure/shop/barter/BarterShopState.SHOP_INSTANCE_CODEC:Lcom/hypixel/hytale/codec/builder/BuilderCodec;
        //   144: invokedynamic   BootstrapMethod #9, get:()Ljava/util/function/Supplier;
        //   149: iconst_0       
        //   150: invokespecial   com/hypixel/hytale/codec/codecs/map/MapCodec.<init>:(Lcom/hypixel/hytale/codec/Codec;Ljava/util/function/Supplier;Z)V
        //   153: invokespecial   com/hypixel/hytale/codec/KeyedCodec.<init>:(Ljava/lang/String;Lcom/hypixel/hytale/codec/Codec;)V
        //   156: invokedynamic   BootstrapMethod #10, accept:()Ljava/util/function/BiConsumer;
        //   161: invokedynamic   BootstrapMethod #11, apply:()Ljava/util/function/Function;
        //   166: invokevirtual   com/hypixel/hytale/codec/builder/BuilderCodec$Builder.append:(Lcom/hypixel/hytale/codec/KeyedCodec;Ljava/util/function/BiConsumer;Ljava/util/function/Function;)Lcom/hypixel/hytale/codec/builder/BuilderField$FieldBuilder;
        //   169: invokevirtual   com/hypixel/hytale/codec/builder/BuilderField$FieldBuilder.add:()Lcom/hypixel/hytale/codec/builder/BuilderCodec$BuilderBase;
        //   172: checkcast       Lcom/hypixel/hytale/codec/builder/BuilderCodec$Builder;
        //   175: invokevirtual   com/hypixel/hytale/codec/builder/BuilderCodec$Builder.build:()Lcom/hypixel/hytale/codec/builder/BuilderCodec;
        //   178: putstatic       com/hypixel/hytale/builtin/adventure/shop/barter/BarterShopState.CODEC:Lcom/hypixel/hytale/codec/builder/BuilderCodec;
        //   181: return         
        // 
        // The error that occurred was:
        // 
        // java.lang.UnsupportedOperationException: The requested operation is not supported.
        //     at com.strobel.util.ContractUtils.unsupported(ContractUtils.java:27)
        //     at com.strobel.assembler.metadata.TypeReference.getRawType(TypeReference.java:284)
        //     at com.strobel.assembler.metadata.TypeReference.getRawType(TypeReference.java:279)
        //     at com.strobel.assembler.metadata.TypeReference.makeGenericType(TypeReference.java:154)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visitParameterizedType(TypeSubstitutionVisitor.java:225)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visitParameterizedType(TypeSubstitutionVisitor.java:25)
        //     at com.strobel.assembler.metadata.ParameterizedType.accept(ParameterizedType.java:103)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visit(TypeSubstitutionVisitor.java:40)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visitParameterizedType(TypeSubstitutionVisitor.java:211)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visitParameterizedType(TypeSubstitutionVisitor.java:25)
        //     at com.strobel.assembler.metadata.ParameterizedType.accept(ParameterizedType.java:103)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visit(TypeSubstitutionVisitor.java:40)
        //     at com.strobel.assembler.metadata.TypeSubstitutionVisitor.visitMethod(TypeSubstitutionVisitor.java:314)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferCall(TypeAnalysis.java:2611)
        //     at com.strobel.decompiler.ast.TypeAnalysis.doInferTypeForExpression(TypeAnalysis.java:1040)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:815)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:782)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:778)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferCall(TypeAnalysis.java:2483)
        //     at com.strobel.decompiler.ast.TypeAnalysis.doInferTypeForExpression(TypeAnalysis.java:1040)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:815)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:782)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:778)
        //     at com.strobel.decompiler.ast.TypeAnalysis.doInferTypeForExpression(TypeAnalysis.java:1510)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:815)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:782)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:778)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferCall(TypeAnalysis.java:2483)
        //     at com.strobel.decompiler.ast.TypeAnalysis.doInferTypeForExpression(TypeAnalysis.java:1040)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypeForExpression(TypeAnalysis.java:815)
        //     at com.strobel.decompiler.ast.TypeAnalysis.runInference(TypeAnalysis.java:684)
        //     at com.strobel.decompiler.ast.TypeAnalysis.inferTypesForVariables(TypeAnalysis.java:593)
        //     at com.strobel.decompiler.ast.TypeAnalysis.runInference(TypeAnalysis.java:405)
        //     at com.strobel.decompiler.ast.TypeAnalysis.run(TypeAnalysis.java:95)
        //     at com.strobel.decompiler.ast.AstOptimizer.optimize(AstOptimizer.java:109)
        //     at com.strobel.decompiler.ast.AstOptimizer.optimize(AstOptimizer.java:42)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:206)
        //     at com.strobel.decompiler.languages.java.ast.AstMethodBodyBuilder.createMethodBody(AstMethodBodyBuilder.java:93)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethodBody(AstBuilder.java:868)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createMethod(AstBuilder.java:761)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addTypeMembers(AstBuilder.java:638)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeCore(AstBuilder.java:605)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createTypeNoCache(AstBuilder.java:195)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.createType(AstBuilder.java:162)
        //     at com.strobel.decompiler.languages.java.ast.AstBuilder.addType(AstBuilder.java:137)
        //     at com.strobel.decompiler.languages.java.JavaLanguage.buildAst(JavaLanguage.java:71)
        //     at com.strobel.decompiler.languages.java.JavaLanguage.decompileType(JavaLanguage.java:59)
        //     at com.strobel.decompiler.DecompilerDriver.decompileType(DecompilerDriver.java:333)
        //     at com.strobel.decompiler.DecompilerDriver.decompileJar(DecompilerDriver.java:254)
        //     at com.strobel.decompiler.DecompilerDriver.main(DecompilerDriver.java:129)
        // 
        throw new IllegalStateException("An error occurred while decompiling this method.");
    }
    
    public static class ShopInstanceState
    {
        private int[] currentStock;
        private Instant nextRefreshTime;
        private Long resolveSeed;
        private transient BarterTrade[] resolvedTrades;
        
        public ShopInstanceState() {
            this.currentStock = new int[0];
        }
        
        public ShopInstanceState(final int tradeCount) {
            this.currentStock = new int[0];
            this.currentStock = new int[tradeCount];
            this.nextRefreshTime = null;
        }
        
        public int[] getCurrentStock() {
            return this.currentStock;
        }
        
        @Nullable
        public Instant getNextRefreshTime() {
            return this.nextRefreshTime;
        }
        
        public void setNextRefreshTime(final Instant time) {
            this.nextRefreshTime = time;
        }
        
        @Nullable
        public Long getResolveSeed() {
            return this.resolveSeed;
        }
        
        public void setResolveSeed(final Long seed) {
            this.resolveSeed = seed;
        }
        
        @Nonnull
        public BarterTrade[] getResolvedTrades(@Nonnull final BarterShopAsset asset) {
            if (!asset.hasTradeSlots()) {
                return (asset.getTrades() != null) ? asset.getTrades() : new BarterTrade[0];
            }
            if (this.resolvedTrades != null) {
                return this.resolvedTrades;
            }
            if (this.resolveSeed == null) {
                this.resolveSeed = ThreadLocalRandom.current().nextLong();
            }
            return this.resolvedTrades = resolveTradeSlots(asset, this.resolveSeed);
        }
        
        @Nonnull
        private static BarterTrade[] resolveTradeSlots(@Nonnull final BarterShopAsset asset, final long seed) {
            final TradeSlot[] slots = asset.getTradeSlots();
            if (slots == null || slots.length == 0) {
                return new BarterTrade[0];
            }
            final Random random = new Random(seed);
            final List<BarterTrade> result = new ObjectArrayList<BarterTrade>();
            for (final TradeSlot slot : slots) {
                result.addAll(slot.resolve(random));
            }
            return result.toArray(new BarterTrade[0]);
        }
        
        public void resetStockAndResolve(@Nonnull final BarterShopAsset asset) {
            if (asset.hasTradeSlots()) {
                this.resolveSeed = ThreadLocalRandom.current().nextLong();
                this.resolvedTrades = resolveTradeSlots(asset, this.resolveSeed);
            }
            else {
                this.resolvedTrades = null;
            }
            final BarterTrade[] trades = this.getResolvedTrades(asset);
            this.currentStock = new int[trades.length];
            for (int i = 0; i < trades.length; ++i) {
                this.currentStock[i] = trades[i].getMaxStock();
            }
        }
        
        @Deprecated
        public void resetStock(final BarterShopAsset asset) {
            final BarterTrade[] trades = this.getResolvedTrades(asset);
            if (this.currentStock.length != trades.length) {
                this.currentStock = new int[trades.length];
            }
            for (int i = 0; i < trades.length; ++i) {
                this.currentStock[i] = trades[i].getMaxStock();
            }
        }
        
        public boolean expandStockIfNeeded(final BarterShopAsset asset) {
            final BarterTrade[] trades = this.getResolvedTrades(asset);
            if (this.currentStock.length >= trades.length) {
                return false;
            }
            final int[] newStock = new int[trades.length];
            System.arraycopy(this.currentStock, 0, newStock, 0, this.currentStock.length);
            for (int i = this.currentStock.length; i < trades.length; ++i) {
                newStock[i] = trades[i].getMaxStock();
            }
            this.currentStock = newStock;
            return true;
        }
        
        public boolean hasStock(final int tradeIndex, final int quantity) {
            return tradeIndex >= 0 && tradeIndex < this.currentStock.length && this.currentStock[tradeIndex] >= quantity;
        }
        
        public boolean decrementStock(final int tradeIndex, final int quantity) {
            if (!this.hasStock(tradeIndex, quantity)) {
                return false;
            }
            final int[] currentStock = this.currentStock;
            currentStock[tradeIndex] -= quantity;
            return true;
        }
        
        public int getStock(final int tradeIndex) {
            if (tradeIndex < 0 || tradeIndex >= this.currentStock.length) {
                return 0;
            }
            return this.currentStock[tradeIndex];
        }
    }
}
