package me.km.blocks;

import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.SlabBlock;
import net.minecraft.block.material.Material;
import net.minecraft.entity.item.FallingBlockEntity;
import net.minecraft.particles.BlockParticleData;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.state.properties.SlabType;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.IWorldReader;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.ToolType;

public class BlockGravelSlab extends SlabBlock {
    public BlockGravelSlab(Properties properties) {
        super(properties);
    }

    @Override
    public ToolType getHarvestTool(BlockState state) {
        return ToolType.SHOVEL;
    }

    @Override
    public void onBlockAdded(BlockState state, World w, BlockPos pos, BlockState oldState, boolean isMoving) {
        w.getPendingBlockTicks().scheduleTick(pos, this, this.tickRate(w));
    }

    @Override
    public BlockState updatePostPlacement(BlockState state, Direction facing, BlockState facingState, IWorld w, BlockPos currentPos, BlockPos facingPos) {
        w.getPendingBlockTicks().scheduleTick(currentPos, this, this.tickRate(w));
        return super.updatePostPlacement(state, facing, facingState, w, currentPos, facingPos);
    }

    @Override
    public void tick(BlockState state, ServerWorld w, BlockPos pos, Random r) {
        if(w.isAirBlock(pos.down()) || canFallThrough(w.getBlockState(pos.down())) && pos.getY() >= 0) {
            BlockState fallState;
            double y = pos.getY();
            if(state == getDefaultState().with(TYPE, SlabType.TOP)) {
                fallState = w.getBlockState(pos).with(TYPE, SlabType.BOTTOM);
                y += 0.5;
            } else {
                fallState = w.getBlockState(pos);
            }
            FallingBlockEntity fallingblockentity = new FallingBlockEntity(w, pos.getX() + 0.5, y, pos.getZ() + 0.5, fallState);
            w.addEntity(fallingblockentity);
        }
    }

    @Override
    public int tickRate(IWorldReader w) {
        return 2;
    }

    public static boolean canFallThrough(BlockState state) {
        Block block = state.getBlock();
        Material material = state.getMaterial();
        return state.isAir() || block == Blocks.FIRE || material.isLiquid() || material.isReplaceable();
    }

    @OnlyIn(Dist.CLIENT)
    @Override
    public void animateTick(BlockState state, World w, BlockPos pos, Random rand) {
        if(rand.nextInt(16) == 0) {
            BlockPos blockpos = pos.down();
            if(w.isAirBlock(blockpos) || canFallThrough(w.getBlockState(blockpos))) {
                double x = pos.getX() + rand.nextFloat();
                double y = pos.getY() - 0.05;
                double z = pos.getZ() + rand.nextFloat();
                w.addParticle(new BlockParticleData(ParticleTypes.FALLING_DUST, state), x, y, z, 0.0, 0.0, 0.0);
            }
        }

    }

    @OnlyIn(Dist.CLIENT)
    public int getDustColor(BlockState state) {
        return -8356741;
    }
}
