package net.minecraft.world.level.block;

import java.util.Iterator;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.core.Direction;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import java.util.Random;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.phys.shapes.VoxelShape;

public abstract class BaseFireBlock extends Block {
    private final float fireDamage;
    protected static final VoxelShape UP_AABB;
    protected static final VoxelShape DOWN_AABB;
    protected static final VoxelShape WEST_AABB;
    protected static final VoxelShape EAST_AABB;
    protected static final VoxelShape NORTH_AABB;
    protected static final VoxelShape SOUTH_AABB;
    
    public BaseFireBlock(final Properties c, final float float2) {
        super(c);
        this.fireDamage = float2;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        return getState(bin.getLevel(), bin.getClickedPos());
    }
    
    public static BlockState getState(final BlockGetter bpg, final BlockPos fu) {
        final BlockPos fu2 = fu.below();
        final BlockState cfj4 = bpg.getBlockState(fu2);
        if (SoulFireBlock.canSurviveOnBlock(cfj4.getBlock())) {
            return Blocks.SOUL_FIRE.defaultBlockState();
        }
        return ((FireBlock)Blocks.FIRE).getStateForPlacement(bpg, fu);
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return BaseFireBlock.DOWN_AABB;
    }
    
    @Override
    public void animateTick(final BlockState cfj, final Level bqb, final BlockPos fu, final Random random) {
        if (random.nextInt(24) == 0) {
            bqb.playLocalSound(fu.getX() + 0.5, fu.getY() + 0.5, fu.getZ() + 0.5, SoundEvents.FIRE_AMBIENT, SoundSource.BLOCKS, 1.0f + random.nextFloat(), random.nextFloat() * 0.7f + 0.3f, false);
        }
        final BlockPos fu2 = fu.below();
        final BlockState cfj2 = bqb.getBlockState(fu2);
        if (this.canBurn(cfj2) || cfj2.isFaceSturdy(bqb, fu2, Direction.UP)) {
            for (int integer8 = 0; integer8 < 3; ++integer8) {
                final double double9 = fu.getX() + random.nextDouble();
                final double double10 = fu.getY() + random.nextDouble() * 0.5 + 0.5;
                final double double11 = fu.getZ() + random.nextDouble();
                bqb.addParticle(ParticleTypes.LARGE_SMOKE, double9, double10, double11, 0.0, 0.0, 0.0);
            }
        }
        else {
            if (this.canBurn(bqb.getBlockState(fu.west()))) {
                for (int integer8 = 0; integer8 < 2; ++integer8) {
                    final double double9 = fu.getX() + random.nextDouble() * 0.10000000149011612;
                    final double double10 = fu.getY() + random.nextDouble();
                    final double double11 = fu.getZ() + random.nextDouble();
                    bqb.addParticle(ParticleTypes.LARGE_SMOKE, double9, double10, double11, 0.0, 0.0, 0.0);
                }
            }
            if (this.canBurn(bqb.getBlockState(fu.east()))) {
                for (int integer8 = 0; integer8 < 2; ++integer8) {
                    final double double9 = fu.getX() + 1 - random.nextDouble() * 0.10000000149011612;
                    final double double10 = fu.getY() + random.nextDouble();
                    final double double11 = fu.getZ() + random.nextDouble();
                    bqb.addParticle(ParticleTypes.LARGE_SMOKE, double9, double10, double11, 0.0, 0.0, 0.0);
                }
            }
            if (this.canBurn(bqb.getBlockState(fu.north()))) {
                for (int integer8 = 0; integer8 < 2; ++integer8) {
                    final double double9 = fu.getX() + random.nextDouble();
                    final double double10 = fu.getY() + random.nextDouble();
                    final double double11 = fu.getZ() + random.nextDouble() * 0.10000000149011612;
                    bqb.addParticle(ParticleTypes.LARGE_SMOKE, double9, double10, double11, 0.0, 0.0, 0.0);
                }
            }
            if (this.canBurn(bqb.getBlockState(fu.south()))) {
                for (int integer8 = 0; integer8 < 2; ++integer8) {
                    final double double9 = fu.getX() + random.nextDouble();
                    final double double10 = fu.getY() + random.nextDouble();
                    final double double11 = fu.getZ() + 1 - random.nextDouble() * 0.10000000149011612;
                    bqb.addParticle(ParticleTypes.LARGE_SMOKE, double9, double10, double11, 0.0, 0.0, 0.0);
                }
            }
            if (this.canBurn(bqb.getBlockState(fu.above()))) {
                for (int integer8 = 0; integer8 < 2; ++integer8) {
                    final double double9 = fu.getX() + random.nextDouble();
                    final double double10 = fu.getY() + 1 - random.nextDouble() * 0.10000000149011612;
                    final double double11 = fu.getZ() + random.nextDouble();
                    bqb.addParticle(ParticleTypes.LARGE_SMOKE, double9, double10, double11, 0.0, 0.0, 0.0);
                }
            }
        }
    }
    
    protected abstract boolean canBurn(final BlockState cfj);
    
    @Override
    public void entityInside(final BlockState cfj, final Level bqb, final BlockPos fu, final Entity aom) {
        if (!aom.fireImmune()) {
            aom.setRemainingFireTicks(aom.getRemainingFireTicks() + 1);
            if (aom.getRemainingFireTicks() == 0) {
                aom.setSecondsOnFire(8);
            }
            aom.hurt(DamageSource.IN_FIRE, this.fireDamage);
        }
        super.entityInside(cfj, bqb, fu, aom);
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (cfj4.is(cfj1.getBlock())) {
            return;
        }
        if ((bqb.dimension() == Level.OVERWORLD || bqb.dimension() == Level.NETHER) && NetherPortalBlock.trySpawnPortal(bqb, fu)) {
            return;
        }
        if (!cfj1.canSurvive(bqb, fu)) {
            bqb.removeBlock(fu, false);
        }
    }
    
    @Override
    public void playerWillDestroy(final Level bqb, final BlockPos fu, final BlockState cfj, final Player bec) {
        if (!bqb.isClientSide()) {
            bqb.levelEvent(null, 1009, fu, 0);
        }
    }
    
    public static boolean canBePlacedAt(final LevelAccessor bqc, final BlockPos fu) {
        final BlockState cfj3 = bqc.getBlockState(fu);
        final BlockState cfj4 = getState(bqc, fu);
        return cfj3.isAir() && (cfj4.canSurvive(bqc, fu) || isPortal(bqc, fu));
    }
    
    private static boolean isPortal(final LevelAccessor bqc, final BlockPos fu) {
        for (final Direction fz4 : Direction.Plane.HORIZONTAL) {
            if (bqc.getBlockState(fu.relative(fz4)).is(Blocks.OBSIDIAN) && NetherPortalBlock.isPortal(bqc, fu) != null) {
                return true;
            }
        }
        return false;
    }
    
    static {
        UP_AABB = Block.box(0.0, 15.0, 0.0, 16.0, 16.0, 16.0);
        DOWN_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 1.0, 16.0);
        WEST_AABB = Block.box(0.0, 0.0, 0.0, 1.0, 16.0, 16.0);
        EAST_AABB = Block.box(15.0, 0.0, 0.0, 16.0, 16.0, 16.0);
        NORTH_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 16.0, 1.0);
        SOUTH_AABB = Block.box(0.0, 0.0, 15.0, 16.0, 16.0, 16.0);
    }
}
