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

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

import javax.annotation.Nullable;
import java.util.Arrays;
import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.inventory.ItemStack;

public class ClearTransaction implements Transaction
{
    public static final ClearTransaction EMPTY;
    private final boolean succeeded;
    private final short start;
    @Nonnull
    private final ItemStack[] items;
    
    public ClearTransaction(final boolean succeeded, final short start, @Nonnull final ItemStack[] items) {
        this.succeeded = succeeded;
        this.start = start;
        this.items = items;
    }
    
    @Override
    public boolean succeeded() {
        return this.succeeded;
    }
    
    @Override
    public boolean wasSlotModified(short slot) {
        if (!this.succeeded) {
            return false;
        }
        slot -= this.start;
        return slot >= 0 && slot < this.items.length && this.items[slot] != null && !this.items[slot].isEmpty();
    }
    
    @Nonnull
    public ItemStack[] getItems() {
        return this.items;
    }
    
    @Nonnull
    @Override
    public ClearTransaction toParent(final ItemContainer parent, final short start, final ItemContainer container) {
        final short newStart = (short)(start + this.start);
        return new ClearTransaction(this.succeeded, newStart, this.items);
    }
    
    @Nullable
    @Override
    public ClearTransaction fromParent(final ItemContainer parent, final short start, @Nonnull final ItemContainer container) {
        final short newStart = (short)(this.start - start);
        final short capacity = container.getCapacity();
        if (newStart < 0) {
            final int from = -newStart;
            if (this.items.length + newStart > capacity) {
                return new ClearTransaction(this.succeeded, (short)0, Arrays.copyOfRange(this.items, from, from + capacity));
            }
            return new ClearTransaction(this.succeeded, (short)0, Arrays.copyOfRange(this.items, from, this.items.length));
        }
        else {
            if (this.items.length > capacity) {
                return new ClearTransaction(this.succeeded, newStart, Arrays.copyOf(this.items, capacity));
            }
            return new ClearTransaction(this.succeeded, newStart, this.items);
        }
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "ClearTransaction{items=" + Arrays.toString(this.items);
    }
    
    static {
        EMPTY = new ClearTransaction(true, (short)0, ItemStack.EMPTY_ARRAY);
    }
}
