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

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.api.capability.IPlayerDungeonData;
import top.r3944realms.eroticdungeongame.content.entity.SeatEntity;
import top.r3944realms.eroticdungeongame.core.device.ISeatType;
import top.r3944realms.lib39.core.sync.NBTEntitySyncData;

import java.util.Objects;

//todo: 使用方块坐标代替UUID（保存一致性）
public sealed abstract class AbstractPlayerDungeonData extends NBTEntitySyncData implements IPlayerDungeonData permits PlayerDungeonData  {
    protected AbstractPlayerDungeonData(ResourceLocation id) {
        super(id);
    }

    public void setDungeonData(@NotNull ISeatType ISeatType, SeatEntity seat) {
        setAnimationResourceLocation(Objects.requireNonNull(ISeatType.getAnimation(), "Seat Type has no animation.").getDefaultRL());
        setDungeonData_(seat, ISeatType);
    }

    public void setDungeonData(@NotNull ISeatType ISeatType, int varNumber, SeatEntity seat) {
        setAnimationResourceLocation(Objects.requireNonNull(ISeatType.getAnimation(), "Seat Type has no animation.").getVarietyRL(varNumber));
        setDungeonData_(seat, ISeatType);
    }

    public void clearDungeonData(@NotNull Player player) {
        setDeviceMainBlockPos(null);
        setAnimationResourceLocation(null);
        setEyeHeight(-1.0f);
        setPlayerBoundingBox(null);
        AABB aabb = player.getDimensions(player.getPose()).makeBoundingBox(player.position());
        player.setBoundingBox(aabb);
    }

    private void setDungeonData_(@NotNull SeatEntity seat, @NotNull ISeatType type) {
        setEyeHeight(type.getEyeHeight());
        setPlayerBoundingBox(type.getPlayerBB());
        setDeviceMainBlockPos(seat.getLinkedBlockPos());
    }
}
