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

package com.hypixel.hytale.server.core.inventory.transaction;

import java.util.Collections;
import java.util.function.Predicate;
import java.util.Objects;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
import java.util.Iterator;
import javax.annotation.Nonnull;
import java.util.List;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import javax.annotation.Nullable;

public class ItemStackTransaction implements Transaction
{
    public static final ItemStackTransaction FAILED_ADD;
    private final boolean succeeded;
    @Nullable
    private final ActionType action;
    @Nullable
    private final ItemStack query;
    @Nullable
    private final ItemStack remainder;
    private final boolean allOrNothing;
    private final boolean filter;
    @Nonnull
    private final List<ItemStackSlotTransaction> slotTransactions;
    
    public ItemStackTransaction(final boolean succeeded, @Nullable final ActionType action, @Nullable final ItemStack query, @Nullable final ItemStack remainder, final boolean allOrNothing, final boolean filter, @Nonnull final List<ItemStackSlotTransaction> slotTransactions) {
        this.succeeded = succeeded;
        this.action = action;
        this.query = query;
        this.remainder = remainder;
        this.allOrNothing = allOrNothing;
        this.filter = filter;
        this.slotTransactions = slotTransactions;
    }
    
    @Override
    public boolean succeeded() {
        return this.succeeded;
    }
    
    @Override
    public boolean wasSlotModified(final short slot) {
        if (!this.succeeded) {
            return false;
        }
        for (final ItemStackSlotTransaction t : this.slotTransactions) {
            if (t.succeeded() && t.wasSlotModified(slot)) {
                return true;
            }
        }
        return false;
    }
    
    @Nullable
    public ActionType getAction() {
        return this.action;
    }
    
    @Nullable
    public ItemStack getQuery() {
        return this.query;
    }
    
    @Nullable
    public ItemStack getRemainder() {
        return this.remainder;
    }
    
    public boolean isAllOrNothing() {
        return this.allOrNothing;
    }
    
    public boolean isFilter() {
        return this.filter;
    }
    
    @Nonnull
    public List<ItemStackSlotTransaction> getSlotTransactions() {
        return this.slotTransactions;
    }
    
    @Nonnull
    @Override
    public ItemStackTransaction toParent(final ItemContainer parent, final short start, final ItemContainer container) {
        final List<ItemStackSlotTransaction> slotTransactions = this.slotTransactions.stream().map(transaction -> transaction.toParent(parent, start, container)).collect((Collector<? super Object, ?, List<ItemStackSlotTransaction>>)Collectors.toList());
        return new ItemStackTransaction(this.succeeded, this.action, this.query, this.remainder, this.allOrNothing, this.filter, slotTransactions);
    }
    
    @Nullable
    @Override
    public ItemStackTransaction fromParent(final ItemContainer parent, final short start, @Nonnull final ItemContainer container) {
        ItemStackSlotTransaction transaction = null;
        final List<ItemStackSlotTransaction> slotTransactions = this.slotTransactions.stream().map(transaction -> transaction.fromParent(parent, start, container)).filter(Objects::nonNull).collect((Collector<? super Object, ?, List<ItemStackSlotTransaction>>)Collectors.toList());
        if (slotTransactions.isEmpty()) {
            return null;
        }
        boolean succeeded = false;
        final Iterator<ItemStackSlotTransaction> iterator = slotTransactions.iterator();
        while (iterator.hasNext()) {
            transaction = iterator.next();
            if (transaction.succeeded()) {
                succeeded = true;
                break;
            }
        }
        return new ItemStackTransaction(succeeded, this.action, this.query, this.remainder, this.allOrNothing, this.filter, slotTransactions);
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "ItemStackTransaction{succeeded=" + this.succeeded + ", action=" + String.valueOf(this.action) + ", query=" + String.valueOf(this.query) + ", remainder=" + String.valueOf(this.remainder) + ", allOrNothing=" + this.allOrNothing + ", filter=" + this.filter + ", slotTransactions=" + String.valueOf(this.slotTransactions);
    }
    
    static {
        FAILED_ADD = new ItemStackTransaction(false, ActionType.ADD, null, null, false, false, Collections.emptyList());
    }
}
