import { Direction, EntityComponentTypes, ItemStack, MolangVariableMap, Player, world } from "@minecraft/server";
import { SuppEntities } from "../reg/suppEntities";
import { isBlockSolid, isLivingEntity, shrinkIfInSurvival, splitRes } from "../util/miscUtils";
import { wrapDegrees } from "../util/mthUtils";
import { dirToYaw, getHorizontalViewDir, offsetVec } from "../util/dirUtils";
import { getPlacementPosition } from "../util/placementUtils";
import { isBottomSlab } from "../util/blockProperties";
import { McEntities } from "../reg/mcEntities";
import { configs } from "../configs";
function getOrCreateSeat(block) {
    const opt = {
        location: block.center(),
        maxDistance: 0.5,
        type: "minecraft:pig",
        closest: 1
    };
    const entities = block.dimension.getEntities(opt);
    if (entities.length == 0) {
        return block.dimension.spawnEntity(SuppEntities.Seat, block.center());
    }
    else {
        return entities[0];
    }
}
export const CushionSeatComponent = {
    onPlayerInteract(event) {
        const player = event.player;
        const block = event.block;
        const seat = getOrCreateSeat(block);
        const passengers = seat.getComponent(EntityComponentTypes.Rideable);
        if (passengers.getRiders().length == 0) {
            passengers.addRider(player);
        }
    }
};
const itemIdData = "razz_sup:color";
export function setCushionColor(cushion, item) {
    cushion.triggerEvent(item.typeId);
    cushion.setDynamicProperty(itemIdData, item.typeId);
}
export const CushionItemComponent = {
    onUseOn(event) {
        const block = getPlacementPosition(event.block, event.blockFace);
        if (block) {
            const below = block.below();
            const isSlab = isBottomSlab(below);
            if (isSlab || isBlockSolid(block.below(), Direction.Up)) {
                //check for entities
                const eOpt = {
                    location: block.location,
                    volume: { x: 0, y: 1, z: 0 },
                    closest: 100,
                };
                let entities = block.dimension.getEntities(eOpt);
                entities = entities.filter(e => (!isLivingEntity(e) && e.typeId != McEntities.Item));
                if (entities.length == 0) {
                    const player = event.source;
                    const item = event.itemStack;
                    const cushion = event.block.dimension.spawnEntity(SuppEntities.Cushion, offsetVec(event.block.bottomCenter(), Direction.Up, isSlab ? 0.5 : 1));
                    const dir = getHorizontalViewDir(player);
                    let number = dirToYaw(dir);
                    const yaw = wrapDegrees(360 + 180 - number);
                    //set color
                    setCushionColor(cushion, item);
                    cushion.setRotation({ x: 0, y: yaw });
                    cushion.dimension.playSound("dig.cloth", cushion.location);
                    if (configs.cushionLockRotation) {
                        cushion.triggerEvent("razz_sup:lock_rider_rotation");
                    }
                    if (player instanceof Player) {
                        shrinkIfInSurvival(player, item);
                    }
                }
            }
        }
    }
};
world.afterEvents.dataDrivenEntityTrigger.subscribe((event) => {
    let entity = event.entity;
    if (entity.typeId == SuppEntities.Cushion && event.eventId == "razz_sup:damaged_by_player") {
        // Get the variant component
        const id = entity.getDynamicProperty(itemIdData);
        const itemStack = new ItemStack(id, 1); // Drops the same item as the entity type
        entity.dimension.spawnItem(itemStack, offsetVec(entity.location, Direction.Up, 0.125));
        const [namespace, name] = splitRes(id);
        const map = new MolangVariableMap();
        map.setVector3("variable.emitter_radius", { x: 0.5, y: 1.5 / 16, z: 0.5 });
        map.setFloat("variable.emitter_intensity", 4);
        entity.dimension.spawnParticle("razz_sup:breaking_" + name, entity.location, map);
        entity.dimension.playSound("dig.cloth", entity.location);
        // Remove the entity
        entity.remove();
    }
});
