/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.content.block.multiply.horizontal;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluids;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.content.block.multiply.AbstractThreePartSeatBlock;
import top.r3944realms.eroticdungeongame.content.block.part.AdvanceSeatPart;

public abstract class HorizontalTripleSeatBlock extends AbstractThreePartSeatBlock {
    public HorizontalTripleSeatBlock(Properties properties) {
        super(properties);
    }

    @Override
    protected BlockPos getMiddlePartPos(@NotNull BlockPos primaryPos, Direction facing) {
        // 中间部件在主部件的朝向方向
        return primaryPos.relative(facing);
    }

    @Override
    protected BlockPos getTopPartPos(@NotNull BlockPos primaryPos, Direction facing) {
        // 顶部部件在中间部件的朝向方向（即主部件的朝向方向两次）
        return primaryPos.relative(facing, 2);
    }

    @Override
    public BlockPos[] getOtherPartPositions(BlockPos currentPos, @NotNull BlockState currentState) {
        Direction facing = currentState.getValue(FACING);
        AdvanceSeatPart currentPart = currentState.getValue(PART);

        BlockPos[] positions = new BlockPos[2];

        switch (currentPart) {
            case FOOT -> {
                // 主部件：需要中间和顶部
                positions[0] = currentPos.relative(facing);                 // 中间部件
                positions[1] = currentPos.relative(facing, 2);      // 顶部部件
            }
            case MIDDLE -> {
                // 中间部件：需要主部件和顶部
                positions[0] = currentPos.relative(facing.getOpposite());   // 主部件
                positions[1] = currentPos.relative(facing);                 // 顶部部件
            }
            case HEAD -> {
                // 顶部部件：需要主部件和中间
                positions[0] = currentPos.relative(facing.getOpposite(), 2);    // 主部件
                positions[1] = currentPos.relative(facing.getOpposite());               // 中间部件
            }
        }

        return positions;
    }

    @Override
    protected void placeOtherParts(@NotNull Level level, BlockPos primaryPos, @NotNull BlockState primaryState,
                                   LivingEntity placer, ItemStack stack) {
        Direction facing = primaryState.getValue(FACING);

        // 放置中间部件
        BlockPos middlePos = getMiddlePartPos(primaryPos, facing);
        if (level.getBlockState(middlePos).canBeReplaced()) {
            BlockState middleState = primaryState.setValue(PART, AdvanceSeatPart.MIDDLE).setValue(WATERLOGGED, level.getFluidState(middlePos).getType() == Fluids.WATER);
            level.setBlock(middlePos, middleState, Block.UPDATE_ALL);
        }

        // 放置顶部部件
        BlockPos topPos = getTopPartPos(primaryPos, facing);
        if (level.getBlockState(topPos).canBeReplaced()) {
            BlockState topState = primaryState.setValue(PART, AdvanceSeatPart.HEAD).setValue(WATERLOGGED, level.getFluidState(topPos).getType() == Fluids.WATER);
            level.setBlock(topPos, topState, Block.UPDATE_ALL);
        }

        // 更新方块
        level.blockUpdated(primaryPos, Blocks.AIR);
        primaryState.updateNeighbourShapes(level, primaryPos, Block.UPDATE_ALL);
    }

    @Override
    public BlockPos getPrimaryPartPos(@NotNull BlockState blockState, BlockPos blockPos) {
        AdvanceSeatPart part = blockState.getValue(PART);
        Direction facing = blockState.getValue(FACING);

        return switch (part) {
            case FOOT -> blockPos;                                     // 已经是主部件
            case MIDDLE -> blockPos.relative(facing.getOpposite());     // 中间部件转主部件
            case HEAD -> blockPos.relative(facing.getOpposite(), 2);    // 顶部部件转主部件
        };
    }

    /**
     * 获取放置状态
     */
    @Override
    public BlockState getStateForPlacement(@NotNull BlockPlaceContext context) {
        Direction direction = context.getHorizontalDirection();
        BlockPos pos = context.getClickedPos();
        Level level = context.getLevel();

        // 检查中间和顶部位置是否可以放置
        BlockPos middlePos = pos.relative(direction);
        BlockPos topPos = pos.relative(direction, 2);

        if (level.getBlockState(middlePos).canBeReplaced(context) &&
                level.getBlockState(topPos).canBeReplaced(context) &&
                level.getWorldBorder().isWithinBounds(middlePos) &&
                level.getWorldBorder().isWithinBounds(topPos)) {

            return this.defaultBlockState()
                    .setValue(FACING, direction)
                    .setValue(PART, getPrimaryPart());
        }
        return null;
    }
}
