/*
 *  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.vertical;

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.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 VerticalTripleSeatBlock extends AbstractThreePartSeatBlock {
    public VerticalTripleSeatBlock(Properties properties) {
        super(properties);
    }

    @Override
    protected BlockPos getMiddlePartPos(@NotNull BlockPos primaryPos, Direction facing) {
        return primaryPos.above();
    }

    @Override
    protected BlockPos getTopPartPos(@NotNull BlockPos primaryPos, Direction facing) {
        return primaryPos.above(2);
    }

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

        BlockPos[] positions = new BlockPos[2];

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

        return positions;
    }

    @Override
    protected void placeOtherParts(@NotNull Level level, BlockPos primaryPos, BlockState primaryState, LivingEntity placer, ItemStack stack) {
        // 放置中间部件
        BlockPos middlePos = getMiddlePartPos(primaryPos, null);
        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, null);
        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);

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