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

package com.hypixel.hytale.builtin.buildertools.utils;

import java.util.Random;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.RotationTuple;
import com.hypixel.hytale.server.core.asset.type.fluid.Fluid;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType;
import com.hypixel.hytale.server.core.prefab.selection.mask.BlockPattern;
import javax.annotation.Nonnull;

public final class Material
{
    public static final Material EMPTY;
    private final int blockId;
    private final int fluidId;
    private final byte fluidLevel;
    private final int rotation;
    
    private Material(final int blockId, final int fluidId, final byte fluidLevel, final int rotation) {
        this.blockId = blockId;
        this.fluidId = fluidId;
        this.fluidLevel = fluidLevel;
        this.rotation = rotation;
    }
    
    @Nonnull
    public static Material block(final int blockId) {
        return block(blockId, 0);
    }
    
    @Nonnull
    public static Material block(final int blockId, final int rotation) {
        if (blockId == 0) {
            return Material.EMPTY;
        }
        return new Material(blockId, 0, (byte)0, rotation);
    }
    
    @Nonnull
    public static Material fluid(final int fluidId, final byte fluidLevel) {
        if (fluidId == 0) {
            return Material.EMPTY;
        }
        return new Material(0, fluidId, fluidLevel, 0);
    }
    
    @Nullable
    public static Material fromKey(@Nonnull final String key) {
        if (key.equalsIgnoreCase("empty")) {
            return Material.EMPTY;
        }
        final BlockPattern.BlockEntry blockEntry = BlockPattern.tryParseBlockTypeKey(key);
        if (blockEntry != null) {
            final FluidPatternHelper.FluidInfo fluidInfo = FluidPatternHelper.getFluidInfo(blockEntry.blockTypeKey());
            if (fluidInfo != null) {
                return fluid(fluidInfo.fluidId(), fluidInfo.fluidLevel());
            }
            final int blockId = BlockType.getAssetMap().getIndex(blockEntry.blockTypeKey());
            if (blockId != Integer.MIN_VALUE) {
                return block(blockId, blockEntry.rotation());
            }
        }
        final FluidPatternHelper.FluidInfo fluidInfo = FluidPatternHelper.getFluidInfo(key);
        if (fluidInfo != null) {
            return fluid(fluidInfo.fluidId(), fluidInfo.fluidLevel());
        }
        final int blockId = BlockType.getAssetMap().getIndex(key);
        if (blockId != Integer.MIN_VALUE) {
            return block(blockId);
        }
        return null;
    }
    
    public boolean isFluid() {
        return this.fluidId != 0;
    }
    
    public boolean isBlock() {
        return this.blockId != 0 && this.fluidId == 0;
    }
    
    public boolean isEmpty() {
        return this.blockId == 0 && this.fluidId == 0;
    }
    
    public int getBlockId() {
        return this.blockId;
    }
    
    public int getFluidId() {
        return this.fluidId;
    }
    
    public byte getFluidLevel() {
        return this.fluidLevel;
    }
    
    public int getRotation() {
        return this.rotation;
    }
    
    public boolean hasRotation() {
        return this.rotation != 0;
    }
    
    @Override
    public String toString() {
        if (this.isEmpty()) {
            return "Material[empty]";
        }
        if (this.isFluid()) {
            final Fluid fluid = Fluid.getAssetMap().getAsset(this.fluidId);
            return "Material[fluid=" + String.valueOf((fluid != null) ? fluid.getId() : Integer.valueOf(this.fluidId)) + ", level=" + this.fluidLevel;
        }
        final BlockType block = BlockType.getAssetMap().getAsset(this.blockId);
        final String rotStr = this.hasRotation() ? (", rotation=" + String.valueOf(RotationTuple.get(this.rotation))) : "";
        return "Material[block=" + String.valueOf((block != null) ? block.getId() : Integer.valueOf(this.blockId)) + rotStr;
    }
    
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof final Material other) {
            return this.blockId == other.blockId && this.fluidId == other.fluidId && this.fluidLevel == other.fluidLevel && this.rotation == other.rotation;
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return 31 * (31 * (31 * this.blockId + this.fluidId) + this.fluidLevel) + this.rotation;
    }
    
    @Nonnull
    public static Material fromPattern(@Nonnull final BlockPattern pattern, @Nonnull final Random random) {
        final BlockPattern.BlockEntry blockEntry = pattern.nextBlockTypeKey(random);
        if (blockEntry != null) {
            final FluidPatternHelper.FluidInfo fluidInfo = FluidPatternHelper.getFluidInfo(blockEntry.blockTypeKey());
            if (fluidInfo != null) {
                return fluid(fluidInfo.fluidId(), fluidInfo.fluidLevel());
            }
            final int blockId = BlockType.getAssetMap().getIndex(blockEntry.blockTypeKey());
            if (blockId != Integer.MIN_VALUE) {
                return block(blockId, blockEntry.rotation());
            }
        }
        return block(pattern.nextBlock(random));
    }
    
    static {
        EMPTY = new Material(0, 0, (byte)0, 0);
    }
}
