/*
 *  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.AbstractTwoPartSeatBlock;
import top.r3944realms.eroticdungeongame.content.block.part.SeatPart;

/**
 * 水平方向的双格座椅方块
 * 两个部件在水平方向相邻
 */
public abstract class HorizontalDoubleSeatBlock extends AbstractTwoPartSeatBlock {

    public HorizontalDoubleSeatBlock(Properties properties) {
        super(properties);
    }

    @Override
    protected BlockPos getSecondaryPartPos(BlockPos primaryPos, Direction facing, SeatPart currentPart) {
        if (isPrimary(currentPart)) {
            // 主部件的从部件在朝向方向
            return primaryPos.relative(facing);
        } else {
            // 从部件的主部件在朝向的反方向
            return primaryPos.relative(facing.getOpposite());
        }
    }

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

        if (level.getBlockState(secondaryPos).canBeReplaced()) {
            BlockState secondaryState = setSecondaryState(primaryState).setValue(WATERLOGGED, level.getFluidState(secondaryPos).getType() == Fluids.WATER);
            level.setBlock(secondaryPos, secondaryState, Block.UPDATE_ALL);
            level.blockUpdated(primaryPos, Blocks.AIR);
            primaryState.updateNeighbourShapes(level, primaryPos, Block.UPDATE_ALL);
        }
    }

    @Override
    public BlockPos getPrimaryPartPos(BlockState blockState, BlockPos blockPos) {
        if (!isPrimary(blockState)) {
            // 如果点击的是从部件，找到主部件位置
            Direction facing = blockState.getValue(FACING);
            return blockPos.relative(facing.getOpposite());
        }
        return blockPos;
    }

    @Override
    protected void handleSecondaryPartRemoval(Level level, BlockPos blockPos, @NotNull BlockState blockState) {
        Direction facing = blockState.getValue(FACING);

        BlockPos otherPartPos;
        if (isPrimary(blockState)) {
            // 移除主部件时移除从部件
            otherPartPos = blockPos.relative(facing);
        } else {
            // 移除从部件时移除主部件
            otherPartPos = blockPos.relative(facing.getOpposite());
        }

        BlockState otherState = level.getBlockState(otherPartPos);
        if (otherState.getBlock() == this) {
            level.removeBlock(otherPartPos, false);
        }
    }

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

        BlockPos secondaryPos = pos.relative(direction);
        Level level = context.getLevel();

        // 检查从部件位置是否可以放置
        if (level.getBlockState(secondaryPos).canBeReplaced(context) &&
                level.getWorldBorder().isWithinBounds(secondaryPos)) {
            return setPrimaryStatePart(
                    this.defaultBlockState()
                        .setValue(FACING, direction)
                );
        }
        return null;
    }
}