package org.geysermc.geyser.level.block;

import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.geysermc.geyser.api.block.custom.nonvanilla.JavaBlockState;
import org.geysermc.geyser.api.block.custom.nonvanilla.JavaBoundingBox;

public class GeyserJavaBlockState implements JavaBlockState {
    String identifier;
    int javaId;
    int stateGroupId;
    float blockHardness;
    boolean waterlogged;
    JavaBoundingBox[] collision;
    boolean canBreakWithHand;
    String pistonBehavior;

    private GeyserJavaBlockState(Builder builder) {
        this.identifier = builder.identifier;
        this.javaId = builder.javaId;
        this.stateGroupId = builder.stateGroupId;
        this.blockHardness = builder.blockHardness;
        this.waterlogged = builder.waterlogged;
        this.collision = builder.collision;
        this.canBreakWithHand = builder.canBreakWithHand;
        this.pistonBehavior = builder.pistonBehavior;
    }

    @Override
    public @NonNull String identifier() {
        return identifier;
    }

    @Override
    public @NonNegative int javaId() {
        return javaId;
    }

    @Override
    public @NonNegative int stateGroupId() {
        return stateGroupId;
    }

    @Override
    public @NonNegative float blockHardness() {
        return blockHardness;
    }

    @Override
    public boolean waterlogged() {
        return waterlogged;
    }

    @Override
    public @NonNull JavaBoundingBox[] collision() {
        return collision;
    }

    @Override
    public boolean canBreakWithHand() {
        return canBreakWithHand;
    }

    @Override
    public @Nullable String pickItem() {
        return null;
    }

    @Override
    public @Nullable String pistonBehavior() {
        return pistonBehavior;
    }

    @SuppressWarnings("removal")
    @Override
    public boolean hasBlockEntity() {
        return false;
    }

    public static class Builder implements JavaBlockState.Builder {
        private String identifier;
        private int javaId;
        private int stateGroupId;
        private float blockHardness;
        private boolean waterlogged;
        private JavaBoundingBox[] collision;
        private boolean canBreakWithHand;
        private String pistonBehavior;

        @Override
        public Builder identifier(@NonNull String identifier) {
            this.identifier = identifier;
            return this;
        }

        @Override
        public Builder javaId(@NonNegative int javaId) {
            this.javaId = javaId;
            return this;
        }

        @Override
        public Builder stateGroupId(@NonNegative int stateGroupId) {
            this.stateGroupId = stateGroupId;
            return this;
        }

        @Override
        public Builder blockHardness(@NonNegative float blockHardness) {
            this.blockHardness = blockHardness;
            return this;
        }

        @Override
        public Builder waterlogged(boolean waterlogged) {
            this.waterlogged = waterlogged;
            return this;
        }

        @Override
        public Builder collision(@NonNull JavaBoundingBox[] collision) {
            this.collision = collision;
            return this;
        }

        @Override
        public Builder canBreakWithHand(boolean canBreakWithHand) {
            this.canBreakWithHand = canBreakWithHand;
            return this;
        }

        @Override
        @Deprecated
        public Builder pickItem(@Nullable String pickItem) {
            return this;
        }

        @Override
        public Builder pistonBehavior(@Nullable String pistonBehavior) {
            this.pistonBehavior = pistonBehavior;
            return this;
        }

        @SuppressWarnings("removal")
        @Override
        public Builder hasBlockEntity(boolean hasBlockEntity) {
            // keep the current behavior
            if (this.pistonBehavior == null && hasBlockEntity) {
                this.pistonBehavior = "BLOCK";
            }
            return this;
        }

        @Override
        public JavaBlockState build() {
            return new GeyserJavaBlockState(this);
        }
    }
}
