package top.r3944realms.eroticdungeongame.content.util;

import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import top.r3944realms.eroticdungeongame.EroticDungeon;
import top.r3944realms.eroticdungeongame.content.block.blockentity.SeatBlockEntity;
import top.r3944realms.eroticdungeongame.content.device.SeatType;
import top.r3944realms.eroticdungeongame.content.entity.SeatEntity;

import java.util.*;

public class FurnitureHelper {
    private FurnitureHelper() {
    }

    public static List<Block> RESTRAINT_FURNACES = new ArrayList<>();
    public static @Nullable String getEntityDeviceId(@NotNull Entity entity) {
        // "entity.eroticdungeongame.wall_cuff -> 8 + modid -> wall_cuff"
        return entity.getEncodeId() == null ? null : entity.getEncodeId().substring(8 + EroticDungeon.MOD_ID.length());
    }

    public static Optional<Entity> getEntityById(@NotNull Level level, int entityId) {
        return Optional.ofNullable(level.getEntity(entityId));
    }

    public static @Nullable SeatBlockEntity getSeatBlockEntity(@NotNull Level level, BlockPos blockPos) {
        BlockEntity blockEntity = level.getBlockEntity(blockPos);
        if (blockEntity instanceof SeatBlockEntity seatBlockEntity) {
            return seatBlockEntity;
        }
        return null;
    }

    public static boolean isRestraintDevice(Block block) {
        return RESTRAINT_FURNACES.contains(block);
    }

    public static void setBlockStates(Level level, BooleanProperty property, boolean value, BlockPos @NotNull ... blockPositions) {
        for (BlockPos blockPos : blockPositions) {
            BlockState blockState = level.getBlockState(blockPos);
            if (blockState.hasProperty(property)) {
                level.setBlock(blockPos, blockState.setValue(property, value), Block.UPDATE_ALL);
            }
        }
    }

    public static SeatType getSeatType(@NotNull Block block) {
        String registryName = block.getName().toString();
        return Arrays.stream(SeatType.values())
                .filter(seatType -> seatType.name().equals(registryName))
                .findFirst()
                .orElse(null);
    }

    public static boolean isPlayerBound(UUID playerUuid) {
        return DeviceManager.getInstance().entrySet().stream()
                .anyMatch(entry -> entry.getValue().getBoundPlayerUuid().equals(playerUuid));
    }
    public static void releasePlayerFromDevice(Player player) {
        SeatBlockEntity deviceBlockEntity;

        // 检查玩家是否骑乘在实体上
        if (player.isPassenger()) {
            Entity vehicle = player.getVehicle();

            // 检查骑乘的实体是否是设备实体
            if (vehicle instanceof SeatEntity seatEntity) {

                // 检查设备是否释放
                if (seatEntity.isActive()) {
                    // 标记设备为已释放状态
                    seatEntity.setRemoving(true);

                    // 移除设备实体
                    seatEntity.remove(Entity.RemovalReason.DISCARDED);

                    // 获取对应的方块实体并处理释放逻辑
                    if (seatEntity.getLinkedBlockPos() == null ||
                            (deviceBlockEntity = getSeatBlockEntity(player.level(), seatEntity.getLinkedBlockPos())) == null) {
                        return;
                    }

                    // 通知方块实体玩家已释放
                    deviceBlockEntity.releasePlayer(player.level(), deviceBlockEntity.getBlockPos());

                    // 从设备管理器中移除会话
                    DeviceManager.getSeatSessions().remove(deviceBlockEntity.getBoundPlayerUUID());

                    // 检查玩家是否仍被其他设备绑定
                    if (isPlayerBound(player.getUUID())) {
                        return;
                    }

                    // 重置玩家的姿势状态
                    DeviceManager.resetSeatSession(player);
                }
            }
        }
    }



}
