/*
 *  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.LevelReader;
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.part.AdvanceSeatPart;

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

    /**
     * 根据当前部分获取其他部分位置
     */
    public BlockPos[] getOtherPartPositions(BlockPos currentPos, @NotNull AdvanceSeatPart currentPart, Direction facing) {
        BlockPos[] positions = new BlockPos[2];

        switch (currentPart) {
            case HEAD -> { // Left
                positions[0] = currentPos.relative(facing.getCounterClockWise());             // 中部
                positions[1] = currentPos.relative(facing.getCounterClockWise(), 2);  // 右边
            }
            case MIDDLE -> {
                positions[0] = currentPos.relative(facing.getClockWise());        // 左边
                positions[1] = currentPos.relative(facing.getCounterClockWise()); // 右边
            }
            case FOOT -> { // Right
                positions[0] = currentPos.relative(facing.getClockWise());          // 中部
                positions[1] = currentPos.relative(facing.getClockWise(),2); // 左边
            }
        }

        return positions;
    }

    @Override
    protected BlockPos getMiddlePartPos(@NotNull BlockPos primaryPos, Direction facing) {
        // 中间部件就是主部位
        return primaryPos;
    }

    @Override
    protected final BlockPos getTopPartPos(@NotNull BlockPos primaryPos, Direction facing) {
        // 左部件
        return getLeftPartPos(primaryPos, facing);
    }

    protected BlockPos getLeftPartPos(@NotNull BlockPos primaryPos, Direction facing) {
        return primaryPos.relative(facing.getClockWise());
    }

    protected BlockPos getRightPartPos(@NotNull BlockPos primaryPos, Direction facing) {
        return primaryPos.relative(facing.getCounterClockWise());
    }

    @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.relative(facing.getClockWise());           // 右部件转主部件
            case MIDDLE -> blockPos;                                         // 主部件
            case HEAD -> blockPos.relative(facing.getCounterClockWise());    // 左部件转主部件
        };
    }

    @Override
    public BlockPos[] getOtherPartPositions(BlockPos currentPos, @NotNull BlockState currentState) {
       throw new UnsupportedOperationException("Use getOtherPartPositions with Direction Prams instead.");
    }

    protected void handleOtherPartsRemoval(Level level, BlockPos blockPos, BlockState blockState) {
        Direction facing = blockState.getValue(FACING);
        AdvanceSeatPart part = blockState.getValue(PART);
        BlockPos[] otherPartPositions = getOtherPartPositions(blockPos, part, facing);

        for (BlockPos otherPos : otherPartPositions) {
            removeIfSameBlock(level, otherPos);
        }
    }

    @Override
    public boolean canSurvive(@NotNull BlockState state, @NotNull LevelReader level, @NotNull BlockPos pos) {
        Direction facing = state.getValue(FACING);
        // 检查所有其他部件位置是否可以放置
        BlockPos[] otherPositions = getOtherPartPositions(pos, getPrimaryPart(), facing);

        for (BlockPos otherPos : otherPositions) {
            if (!level.getBlockState(otherPos).canBeReplaced()) {
                return false;
            }
        }

        return true;
    }

    @Override
    protected void placeOtherParts(@NotNull Level level, BlockPos primaryPos, @NotNull BlockState primaryState, LivingEntity placer, ItemStack stack) {
        Direction facing = primaryState.getValue(FACING);
        // 放置右部部件
        BlockPos right = getRightPartPos(primaryPos, facing);
        if (level.getBlockState(right).canBeReplaced()) {
            BlockState middleState = primaryState.setValue(PART, AdvanceSeatPart.FOOT).setValue(WATERLOGGED, level.getFluidState(right).getType() == Fluids.WATER);
            level.setBlock(right, middleState, Block.UPDATE_ALL);
        }

        // 放置左部部件
        BlockPos left = getLeftPartPos(primaryPos, facing);
        if (level.getBlockState(left).canBeReplaced()) {
            BlockState topState = primaryState.setValue(PART, AdvanceSeatPart.HEAD).setValue(WATERLOGGED, level.getFluidState(left).getType() == Fluids.WATER);
            level.setBlock(left, topState, Block.UPDATE_ALL);
        }
        // 更新方块
        level.blockUpdated(primaryPos, Blocks.AIR);
        primaryState.updateNeighbourShapes(level, primaryPos, Block.UPDATE_ALL);
    }

    public BlockPos[] getPlaceOtherPartPositions(@NotNull BlockPos blockPos, Direction direction) {
        return getOtherPartPositions(blockPos, getPrimaryPart(), direction);
    }

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

        // 检查其它位置是否可以放置
        BlockPos[] otherPartPositions = getPlaceOtherPartPositions(pos, direction);

        for (BlockPos otherPos : otherPartPositions) {
            if (!level.getBlockState(otherPos).canBeReplaced() || !level.getWorldBorder().isWithinBounds(otherPos)) {
                return null;
            }
        }
        return this.defaultBlockState()
                .setValue(FACING, direction)
                .setValue(PART, getPrimaryPart());
    }

    @Override
    public AdvanceSeatPart getPrimaryPart() {
        return AdvanceSeatPart.MIDDLE;
    }
}
