import { world, EquipmentSlot } from "@minecraft/server";
import * as utils from "../utils"

export async function onTick()
{
    try {
        for (const player of world.getPlayers()) {
            const equippable = player.getComponent('equippable');
            if(equippable){
                const chest = equippable.getEquipment(EquipmentSlot.Chest);
                if(chest){
                    if(chest.typeId == "pu_sg:wooden_jetpack"){
                        if (player.isJumping) {
                            utils.runPlayerCommand(player, "effect @s levitation 1 3 true");
                        
                            // Get player's view direction
                            const viewDir = player.getViewDirection();
                        
                            const backwardOffsetX = -viewDir.x * 0.1;
                            const backwardOffsetZ = -viewDir.z * 0.1;
                        
                            // Apply the particle with the offset
                            utils.runPlayerCommand(player, `particle pu_sg:wood_jetpack_1 ~${backwardOffsetX} ~ ~${backwardOffsetZ}`);
                            
                        } else {
                            if (!player.isOnGround) {
                                utils.runPlayerCommand(player, "effect @s slow_falling 1 3 true");

                                // Get player's view direction
                                const viewDir = player.getViewDirection();
                            
                                const backwardOffsetX = -viewDir.x * 0.1;
                                const backwardOffsetZ = -viewDir.z * 0.1;
                            
                                // Apply the particle with the offset
                                utils.runPlayerCommand(player, `particle pu_sg:wood_jetpack_2 ~${backwardOffsetX} ~ ~${backwardOffsetZ}`);
                            }
                        }                                 
                    }
                }
            }
        }
    } catch (e) {
        // Error handling
        console.warn("Error inside the main interval loop");
        console.warn(e);
    }
};