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

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

import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.inventory.container.ItemContainer;
import javax.annotation.Nonnull;

public class MoveTransaction<T extends Transaction> implements Transaction
{
    private final boolean succeeded;
    @Nonnull
    private final SlotTransaction removeTransaction;
    @Nonnull
    private final MoveType moveType;
    @Nonnull
    private final ItemContainer otherContainer;
    private final T addTransaction;
    
    public MoveTransaction(final boolean succeeded, @Nonnull final SlotTransaction removeTransaction, @Nonnull final MoveType moveType, @Nonnull final ItemContainer otherContainer, final T addTransaction) {
        this.succeeded = succeeded;
        this.removeTransaction = removeTransaction;
        this.moveType = moveType;
        this.otherContainer = otherContainer;
        this.addTransaction = addTransaction;
    }
    
    @Override
    public boolean succeeded() {
        return this.succeeded;
    }
    
    @Nonnull
    public SlotTransaction getRemoveTransaction() {
        return this.removeTransaction;
    }
    
    @Nonnull
    public MoveType getMoveType() {
        return this.moveType;
    }
    
    @Nonnull
    public ItemContainer getOtherContainer() {
        return this.otherContainer;
    }
    
    public T getAddTransaction() {
        return this.addTransaction;
    }
    
    @Nonnull
    public MoveTransaction<T> toInverted(@Nonnull final ItemContainer itemContainer) {
        return new MoveTransaction<T>(this.succeeded(), this.removeTransaction, MoveType.MOVE_TO_SELF, itemContainer, this.addTransaction);
    }
    
    @Override
    public boolean wasSlotModified(final short slot) {
        return this.succeeded && ((this.addTransaction.succeeded() && this.addTransaction.wasSlotModified(slot)) || (this.removeTransaction.succeeded() && this.removeTransaction.wasSlotModified(slot)));
    }
    
    @Nonnull
    @Override
    public MoveTransaction<T> toParent(final ItemContainer parent, final short start, final ItemContainer container) {
        final MoveType moveType = this.getMoveType();
        return switch (moveType) {
            default -> throw new MatchException(null, null);
            case MOVE_TO_SELF -> new MoveTransaction<T>(this.succeeded(), this.removeTransaction, moveType, this.getOtherContainer(), (T)this.addTransaction.toParent(parent, start, container));
            case MOVE_FROM_SELF -> new MoveTransaction<T>(this.succeeded(), this.removeTransaction.toParent(parent, start, container), moveType, this.getOtherContainer(), this.addTransaction);
        };
    }
    
    @Nullable
    @Override
    public MoveTransaction<T> fromParent(final ItemContainer parent, final short start, @Nonnull final ItemContainer container) {
        final MoveType moveType = this.getMoveType();
        switch (moveType) {
            case MOVE_TO_SELF: {
                final T newAddTransaction = (T)this.addTransaction.fromParent(parent, start, container);
                if (newAddTransaction == null) {
                    return null;
                }
                return new MoveTransaction<T>(this.succeeded(), this.getRemoveTransaction(), this.getMoveType(), this.getOtherContainer(), newAddTransaction);
            }
            case MOVE_FROM_SELF: {
                final SlotTransaction newRemoveTransaction = this.getRemoveTransaction().fromParent(parent, start, container);
                if (newRemoveTransaction == null) {
                    return null;
                }
                return new MoveTransaction<T>(this.succeeded(), newRemoveTransaction, this.getMoveType(), this.getOtherContainer(), this.addTransaction);
            }
            default: {
                throw new IllegalStateException("Unexpected value: " + String.valueOf(moveType));
            }
        }
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "MoveTransaction{succeeded=" + this.succeeded + ", removeTransaction=" + String.valueOf(this.removeTransaction) + ", moveType=" + String.valueOf(this.moveType) + ", otherContainer=" + String.valueOf(this.otherContainer) + ", addTransaction=" + String.valueOf(this.addTransaction);
    }
}
