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

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

import javax.annotation.Nullable;
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 java.util.Collections;
import javax.annotation.Nonnull;
import java.util.List;

public class ListTransaction<T extends Transaction> implements Transaction
{
    public static final ListTransaction<?> EMPTY_SUCCESSFUL_TRANSACTION;
    public static final ListTransaction<?> EMPTY_FAILED_TRANSACTION;
    private final boolean succeeded;
    @Nonnull
    private final List<T> list;
    
    public static <T extends Transaction> ListTransaction<T> getEmptyTransaction(final boolean succeeded) {
        return (ListTransaction<T>)(succeeded ? ListTransaction.EMPTY_SUCCESSFUL_TRANSACTION : ListTransaction.EMPTY_FAILED_TRANSACTION);
    }
    
    private ListTransaction(final boolean succeeded) {
        this.succeeded = succeeded;
        this.list = Collections.emptyList();
    }
    
    public ListTransaction(final boolean succeeded, @Nonnull final List<T> list) {
        this.succeeded = succeeded;
        this.list = Collections.unmodifiableList((List<? extends T>)list);
    }
    
    @Override
    public boolean succeeded() {
        return this.succeeded;
    }
    
    @Override
    public boolean wasSlotModified(final short slot) {
        if (!this.succeeded) {
            return false;
        }
        for (final T t : this.list) {
            if (t.succeeded() && t.wasSlotModified(slot)) {
                return true;
            }
        }
        return false;
    }
    
    @Nonnull
    public List<T> getList() {
        return this.list;
    }
    
    public int size() {
        return this.list.size();
    }
    
    @Nonnull
    @Override
    public ListTransaction<T> toParent(final ItemContainer parent, final short start, final ItemContainer container) {
        final List<T> list = this.list.stream().map(transaction -> transaction.toParent(parent, start, container)).collect((Collector<? super Object, ?, List<T>>)Collectors.toList());
        return new ListTransaction<T>(this.succeeded, list);
    }
    
    @Nullable
    @Override
    public ListTransaction<T> fromParent(final ItemContainer parent, final short start, final ItemContainer container) {
        T transaction = null;
        final List<T> list = this.list.stream().map(transaction -> transaction.fromParent(parent, start, container)).filter(Objects::nonNull).collect((Collector<? super Object, ?, List<T>>)Collectors.toList());
        if (list.isEmpty()) {
            return null;
        }
        boolean succeeded = false;
        final Iterator<T> iterator = list.iterator();
        while (iterator.hasNext()) {
            transaction = iterator.next();
            if (transaction.succeeded()) {
                succeeded = true;
                break;
            }
        }
        return new ListTransaction<T>(succeeded, list);
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "ListTransaction{succeeded=" + this.succeeded + ", list=" + String.valueOf(this.list);
    }
    
    static {
        EMPTY_SUCCESSFUL_TRANSACTION = new ListTransaction<Object>(true);
        EMPTY_FAILED_TRANSACTION = new ListTransaction<Object>(false);
    }
}
