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

package com.hypixel.hytale.builtin.adventure.objectives.transaction;

import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.Codec;
import com.hypixel.hytale.server.core.entity.Entity;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.Universe;
import javax.annotation.Nonnull;
import java.util.UUID;
import com.hypixel.hytale.codec.builder.BuilderCodec;

public class SpawnEntityTransactionRecord extends TransactionRecord
{
    public static final BuilderCodec<SpawnEntityTransactionRecord> CODEC;
    protected UUID worldUUID;
    protected UUID entityUUID;
    
    public SpawnEntityTransactionRecord(@Nonnull final UUID worldUUID, @Nonnull final UUID entityUUID) {
        this.worldUUID = worldUUID;
        this.entityUUID = entityUUID;
    }
    
    protected SpawnEntityTransactionRecord() {
    }
    
    @Override
    public void revert() {
        this.removeEntity();
    }
    
    @Override
    public void complete() {
        this.removeEntity();
    }
    
    @Override
    public void unload() {
    }
    
    @Override
    public boolean shouldBeSerialized() {
        return true;
    }
    
    private void removeEntity() {
        final World world = Universe.get().getWorld(this.worldUUID);
        final Entity entity = world.getEntity(this.entityUUID);
        if (entity == null) {
            return;
        }
        if (!entity.remove()) {
            throw new RuntimeException("Failed to revert record!");
        }
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "SpawnEntityTransactionRecord{worldUUID=" + String.valueOf(this.worldUUID) + ", entityUUID=" + String.valueOf(this.entityUUID) + "} " + super.toString();
    }
    
    static {
        CODEC = BuilderCodec.builder(SpawnEntityTransactionRecord.class, SpawnEntityTransactionRecord::new, SpawnEntityTransactionRecord.BASE_CODEC).append(new KeyedCodec<UUID>("WorldUUID", Codec.UUID_BINARY), (spawnEntityTransactionRecord, uuid) -> spawnEntityTransactionRecord.worldUUID = uuid, spawnEntityTransactionRecord -> spawnEntityTransactionRecord.worldUUID).add().append(new KeyedCodec("EntityUUID", Codec.UUID_BINARY), (spawnEntityTransactionRecord, uuid) -> spawnEntityTransactionRecord.entityUUID = uuid, spawnEntityTransactionRecord -> spawnEntityTransactionRecord.entityUUID).add().build();
    }
}
