package su.a71.new_soviet.blocks;

import net.minecraft.block.*;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.enums.BlockHalf;
import net.minecraft.block.enums.StairShape;
import net.minecraft.block.piston.PistonBehavior;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.IntProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.gen.feature.util.CaveSurface;
import su.a71.new_soviet.entity.TVBlockEntity;
import su.a71.new_soviet.registration.NSE_Blocks;
import su.a71.new_soviet.util.Shapes;

import java.util.List;
import java.util.Objects;

import static net.minecraft.block.StairsBlock.HALF;
import static net.minecraft.block.StairsBlock.SHAPE;

public class HandrailBlock extends HorizontalFacingBlock implements BlockEntityProvider {
    protected static final Shapes.HorizontalShape2 SHAPE1;
    protected static final Shapes.HorizontalShape2 SHAPE2;
    protected static final Shapes.HorizontalShape2 SHAPE3;
    public static final BooleanProperty WATERLOGGED;

    public static final IntProperty ROTATE;

    public HandrailBlock(Settings settings) {
        super(settings.sounds(BlockSoundGroup.METAL).pistonBehavior(PistonBehavior.BLOCK).strength(1f, 2f));
        setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));
    }

    @Override
    protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
        builder.add(Properties.HORIZONTAL_FACING, WATERLOGGED, ROTATE);
    }

    public FluidState getFluidState(BlockState state) {
        return (Boolean)state.get(WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(state);
    }

    @Override
    public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext ctx) {
        Direction dir = state.get(FACING);
        int rot = state.get(ROTATE);
        return switch (dir) {
            case NORTH -> switch (rot) {
                case 1 -> SHAPE1.north();
                case 2 -> SHAPE2.north();
                case 3 -> SHAPE3.north();
                default -> VoxelShapes.fullCube();
            };
            case SOUTH -> switch (rot) {
                case 1 -> SHAPE1.south();
                case 2 -> SHAPE2.south();
                case 3 -> SHAPE3.south();
                default -> VoxelShapes.fullCube();
            };
            case EAST -> switch (rot) {
                case 1 -> SHAPE1.east();
                case 2 -> SHAPE2.east();
                case 3 -> SHAPE3.east();
                default -> VoxelShapes.fullCube();
            };
            case WEST -> switch (rot) {
                case 1 -> SHAPE1.west();
                case 2 -> SHAPE2.west();
                case 3 -> SHAPE3.west();
                default -> VoxelShapes.fullCube();
            };
            default -> VoxelShapes.fullCube();
        };
    }

    @Override
    public BlockState getPlacementState(ItemPlacementContext ctx) {
        FluidState fluidState = ctx.getWorld().getFluidState(ctx.getBlockPos());
        boolean bl = fluidState.getFluid() == Fluids.WATER;
        int rot = 2;
        BlockPos under = new BlockPos(ctx.getBlockPos().getX(), ctx.getBlockPos().getY() - 1, ctx.getBlockPos().getZ());
        if (istruestairs1(ctx.getWorld().getBlockState(ctx.getBlockPos().down(1)), ctx)) {
            rot = 1;
        } else if (istruestairs2(ctx.getWorld().getBlockState(ctx.getBlockPos().down(1)), ctx)) {
            rot = 3;
        }
        return (BlockState)this.getDefaultState()
                .with(WATERLOGGED, ctx.getWorld().getFluidState(ctx.getBlockPos()).getFluid() == Fluids.WATER)
                .with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite())
                .with(ROTATE, rot);

    }

    private boolean istruestairs1(BlockState blockState, ItemPlacementContext ctx) {
        boolean output = false;
        if (blockState.isIn(BlockTags.STAIRS)) {
            boolean h = blockState.get(SHAPE) == StairShape.STRAIGHT;
            if (h && blockState.get(HALF) == BlockHalf.BOTTOM && returnFacing(blockState.get(FACING).getName()) == ctx.getHorizontalPlayerFacing().getOpposite().getName()) {
                output = true;
            }
        }
        return output;
    }

    private boolean istruestairs2(BlockState blockState, ItemPlacementContext ctx) {
        boolean output = false;
        if (blockState.isIn(BlockTags.STAIRS)) {
            boolean h = blockState.get(SHAPE) == StairShape.STRAIGHT;
            if (h && blockState.get(HALF) == BlockHalf.BOTTOM && blockState.get(FACING).getName() == returnFacing(ctx.getHorizontalPlayerFacing().getOpposite().getName())) {
                output = true;
            }
        }
        return output;
    }

    private String returnFacing(String facing1) {
        String facing2 = "north";
        if (facing1 == "north") {
            facing2 = "west";
        } else if (facing1 == "west") {
            facing2 = "south";
        } else if (facing1 == "south") {
            facing2 = "east";
        }
        return facing2;
    }

    @Override
    public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
        return new TVBlockEntity(pos, state);
    }

    public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
        if (!state.canPlaceAt(world, pos)) {
            return Blocks.AIR.getDefaultState();
        } else {
            if ((Boolean)state.get(WATERLOGGED)) {
                world.scheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));
            }

            return super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos);
        }
    }

    static {
        SHAPE2 = new Shapes.HorizontalShape2(List.of(List.of(0.0, 0.0, 15.0, 16.0, 17.0, 16.0), List.of(0.0, 17.0, 14.5, 16.0, 19.0, 16.5)));
        SHAPE1 = new Shapes.HorizontalShape2(List.of(List.of(0.0, 0.0, 15.0, 8.0, 18.0, 16.0), List.of(8.0, -8.0, 15.0, 16.0, 10.0, 16.0)));
        SHAPE3 = new Shapes.HorizontalShape2(List.of(List.of(0.0, -8.0, 15.0, 8.0, 10.0, 16.0), List.of(8.0, 0.0, 15.0, 16.0, 18.0, 16.0)));
        ROTATE = IntProperty.of("rotate", 1, 3);;
        WATERLOGGED = Properties.WATERLOGGED;
    }
}