import { world, BlockComponentTypes, system } from '@minecraft/server';
import { Vector } from './Vector';

world.afterEvents.itemUse.subscribe((xm) => {
    const player = xm.source;
    const item = xm.itemStack;

    if (item.typeId == "minecraft:apple") {
        // player.setRotation({ x: 90, y: 90});
        player.teleport({ x: player.location.x, y: player.location.y, z: player.location.z - 1000 }, {
            facingLocation: { x: 0, y: 0, z: 0},
         },);
    }
});

system.runInterval(() => {
    world.getPlayers().forEach((player) => {
        // const blockList = player.getBlockFromViewDirection();
        // const block = blockList ? blockList.block : null;

        // if (block) {
        //     try {
        //         const inventory = block.getComponent("inventory");
        //         if (inventory && inventory.container) {
        //             const blockxxx = inventory.container.getItem(0).typeId;
        //             player.onScreenDisplay.setActionBar(`${blockxxx}`);
        //         } else {
        //             player.onScreenDisplay.setActionBar("方块没有库存组件。");
        //         }
        //     } catch (error) {
        //         player.onScreenDisplay.setActionBar("获取方块库存组件时出错。");
        //     }
        // } else {
        //     player.onScreenDisplay.setActionBar("未找到方块或方块未加载。");
        // }
        // const rotation_x = player.getRotation().x;
        // const rotation_y = player.getRotation().y;
        // player.onScreenDisplay.setActionBar(`X:${rotation_x}\Y:${rotation_y}`);
        const item = player.getComponent("inventory");
        const item_id = item.container.getItem(player.selectedSlotIndex);
        if (item_id?.typeId == "minecraft:apple") {
            const blockList = player.getBlockFromViewDirection({ maxDistance: 10 });
            const block = blockList?.block || null;
    
            if (block) {
                const blockState = Object.entries(block.permutation.getAllStates());
                player.onScreenDisplay.setActionBar(JSON.stringify(blockState));
            }
    
            const entityList = player.getEntitiesFromViewDirection({ maxDistance: 10 });
            if (!entityList || entityList.length === 0) return null;
    
            const entity = entityList[0].entity;
            if (!entity) return null;
    
            const state = entity.getProperty("ph_wild:state");
            const in_water = entity.getProperty("ph_wild:in_water");
            const tame_has_rider = entity.getProperty("ph_wild:tame_has_rider");
            const has_target = entity.getProperty("ph_wild:has_target");
            const in_snow = entity.getProperty("ph_wild:in_snow");
            const is_scared = entity.getProperty("ph_wild:is_scared");
            const camouflage = entity.getProperty("ph_wild:camouflage");
            const camouflage_look = entity.getProperty("ph_wild:camouflage_look");
            const is_tempt = entity.getProperty("ph_wild:is_tempt");
            const is_hurt = entity.getProperty("ph_wild:is_hurt");
            const on_animation = entity.getProperty("ph_wild:on_animation");
            const water_shake = entity.getProperty("ph_wild:water_shake");
            const snow_shake = entity.getProperty("ph_wild:snow_shake");
            const pickup_item = entity.getProperty("ph_wild:pickup_item");
            const run_attack = entity.getProperty("ph_wild:run_attack");
            const attack_cd = entity.getProperty("ph_wild:attack_cd");
            const swim_attack = entity.getProperty("ph_wild:swim_attack");
    
            const is_tamed = entity.getProperty("ph_wild:is_tamed");
            const has_food = entity.getProperty("ph_wild:has_food");
            const on_hurt = entity.getProperty("ph_wild:on_hurt");
            const dash_attack = entity.getProperty("ph_wild:dash_attack");
            player.onScreenDisplay.setActionBar(`state:${state}\non_animation:${on_animation}\nin_water:${in_water}\nswim_attack:${swim_attack}\nwater_shake:${water_shake}\nis_tamed:${is_tamed}\ntame_has_rider:${tame_has_rider}\nhas_target:${has_target}\npickup_item:${pickup_item}\nhas_food:${has_food}\non_hurt:${on_hurt}\ndash_attack:${dash_attack}\nrun_attack:${run_attack}\n\n\n\n\n\n\n\n\n\n\n`);
            // for (const item of groups) {
            //     world.sendMessage(`${JSON.stringify(item)}`);
            // }
        }
    })
})