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

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

import javax.annotation.Nullable;
import java.util.Iterator;
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.List;
import com.hypixel.hytale.server.core.inventory.MaterialQuantity;
import javax.annotation.Nonnull;

public class MaterialTransaction extends ListTransaction<MaterialSlotTransaction>
{
    @Nonnull
    private final ActionType action;
    @Nonnull
    private final MaterialQuantity material;
    private final int remainder;
    private final boolean allOrNothing;
    private final boolean exactAmount;
    private final boolean filter;
    
    public MaterialTransaction(final boolean succeeded, @Nonnull final ActionType action, @Nonnull final MaterialQuantity material, final int remainder, final boolean allOrNothing, final boolean exactAmount, final boolean filter, @Nonnull final List<MaterialSlotTransaction> slotTransactions) {
        super(succeeded, slotTransactions);
        this.action = action;
        this.material = material;
        this.remainder = remainder;
        this.allOrNothing = allOrNothing;
        this.exactAmount = exactAmount;
        this.filter = filter;
    }
    
    @Nonnull
    public ActionType getAction() {
        return this.action;
    }
    
    @Nonnull
    public MaterialQuantity getMaterial() {
        return this.material;
    }
    
    public int getRemainder() {
        return this.remainder;
    }
    
    public boolean isAllOrNothing() {
        return this.allOrNothing;
    }
    
    public boolean isExactAmount() {
        return this.exactAmount;
    }
    
    public boolean isFilter() {
        return this.filter;
    }
    
    @Nonnull
    @Override
    public MaterialTransaction toParent(final ItemContainer parent, final short start, final ItemContainer container) {
        final List<MaterialSlotTransaction> slotTransactions = this.getList().stream().map(transaction -> transaction.toParent(parent, start, container)).collect((Collector<? super Object, ?, List<MaterialSlotTransaction>>)Collectors.toList());
        return new MaterialTransaction(this.succeeded(), this.action, this.material, this.remainder, this.allOrNothing, this.exactAmount, this.filter, slotTransactions);
    }
    
    @Nullable
    @Override
    public MaterialTransaction fromParent(final ItemContainer parent, final short start, @Nonnull final ItemContainer container) {
        MaterialSlotTransaction transaction = null;
        final List<MaterialSlotTransaction> slotTransactions = this.getList().stream().map(transaction -> transaction.fromParent(parent, start, container)).filter(Objects::nonNull).collect((Collector<? super Object, ?, List<MaterialSlotTransaction>>)Collectors.toList());
        if (slotTransactions.isEmpty()) {
            return null;
        }
        boolean succeeded = false;
        final Iterator<MaterialSlotTransaction> iterator = slotTransactions.iterator();
        while (iterator.hasNext()) {
            transaction = iterator.next();
            if (transaction.succeeded()) {
                succeeded = true;
                break;
            }
        }
        return new MaterialTransaction(succeeded, this.action, this.material, this.remainder, this.allOrNothing, this.exactAmount, this.filter, slotTransactions);
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "MaterialTransaction{action=" + String.valueOf(this.action) + ", material=" + String.valueOf(this.material) + ", remainder=" + this.remainder + ", allOrNothing=" + this.allOrNothing + ", exactAmount=" + this.exactAmount + ", filter=" + this.filter + "} " + super.toString();
    }
}
