/*
 * Copyright (c) 2026 盐灯水母
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.remains_cave.block;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.remains_cave.block.entity.TrophyBlockEntity;

public class TrophyBlock extends Block implements EntityBlock {
    // 地面放置：Y 从 0 到 8（底部接地）
    private static final VoxelShape SHAPE_UP = box(4, 0, 4, 12, 8, 12);
    // 天花板放置：Y 从 8 到 16（顶部贴天花板）
    private static final VoxelShape SHAPE_DOWN = box(4, 8, 4, 12, 16, 12);
    // 北面（贴北墙）：Z 从 0 到 8
    private static final VoxelShape SHAPE_NORTH = box(4, 4, 0, 12, 12, 8);
    // 南面：Z 从 8 到 16
    private static final VoxelShape SHAPE_SOUTH = box(4, 4, 8, 12, 12, 16);
    // 西面：X 从 0 到 8
    private static final VoxelShape SHAPE_WEST = box(0, 4, 4, 8, 12, 12);
    // 东面：X 从 8 到 16
    private static final VoxelShape SHAPE_EAST = box(8, 4, 4, 16, 12, 12);

    public static final DirectionProperty FACING = BlockStateProperties.FACING;
    public static final IntegerProperty ROTATION = IntegerProperty.create("rotation", 0, 15);

    public TrophyBlock(Properties properties) {
        super(properties);
        this.registerDefaultState(this.stateDefinition.any()
                .setValue(FACING, Direction.UP)
                .setValue(ROTATION, 0));
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
        builder.add(FACING, ROTATION);
    }

    @Nullable
    @Override
    public BlockState getStateForPlacement(BlockPlaceContext context) {
        Direction clickedFace = context.getClickedFace();
        float yaw = 0;
        if (context.getPlayer() != null) {
            yaw = context.getPlayer().getYRot();
        }
        int rotation = Math.floorMod((int) Math.round((-yaw) / 22.5f), 16);

        if (clickedFace == Direction.UP) {
            return this.defaultBlockState().setValue(FACING, Direction.UP).setValue(ROTATION, rotation);
        } else if (clickedFace == Direction.DOWN) {
            return null; // 不允许放在底面
        } else {
            return this.defaultBlockState().setValue(FACING, clickedFace.getOpposite()).setValue(ROTATION, 0);
        }
    }

    @Override
    public @NotNull VoxelShape getShape(BlockState state, @NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull CollisionContext context) {
        Direction facing = state.getValue(FACING);
        return switch (facing) {
            case UP -> SHAPE_UP;
            case DOWN -> SHAPE_DOWN;
            case NORTH -> SHAPE_NORTH;
            case SOUTH -> SHAPE_SOUTH;
            case WEST -> SHAPE_WEST;
            case EAST -> SHAPE_EAST;
        };
    }

    @Override
    public @NotNull VoxelShape getCollisionShape(@NotNull BlockState state, @NotNull BlockGetter level, @NotNull BlockPos pos, @NotNull CollisionContext context) {
        return getShape(state, level, pos, context);
    }

    @Override
    public BlockEntity newBlockEntity(@NotNull BlockPos pos, @NotNull BlockState state) {
        return new TrophyBlockEntity(pos, state);
    }
}
