import {
    system,
    world
} from "@minecraft/server";
const petsId = [    `feral_lynx`,    `nebula_dragon`,    `flame_hawk`,    `frost_fox`,    `music_golem`,    `storm_griffin`]
system.runInterval(()=>{
    world.getDimension('overworld').getEntities().forEach(entity=>{
        petsId.forEach(pet=>{
            const volumeSetting = world.getDynamicProperty('tp_pw:sound')
            if(volumeSetting===true&&(entity.typeId===`tp_pw:`+pet||entity.typeId===`tp_pw:`+pet+`_untamed`)) {
                try {
                    const delaySB = world.scoreboard.getObjective(`tp_pw:entity_idle_delay`)
                    entity.runCommand(`scoreboard players remove @s[scores={tp_pw:entity_idle_delay=1..}] tp_pw:entity_idle_delay 1`)
                    if(delaySB.getScore(entity)===0) {
                        if(!entity.isValid())return
                        let pitch = Math.random() * (1.4 - 0.6) + 0.6;
                        let delay = (Math.random() * (30 - 15) + 15)*20;
                        const markVariant = entity.getComponent('minecraft:mark_variant')
                        if (markVariant.value===1) {
                            entity.runCommand(`playsound tp_pw:entity.snoring @a ~~~`)
                            delay = 5*20;
                        } else {
                            entity.runCommand(`playsound tp_pw:entity.${pet}.idle @a ~~~ 0.5 ${pitch}`)
                        }
                        delaySB.setScore(entity, delay)
                    }
                } catch (error) {
                    
                }
            }
        })
    })
})
function idleInterval(entity, pet) {
    if(!entity.isValid()||entity.getDynamicProperty(`tp_pw:instanced`)===undefined) return
    let delay = Math.random() * (12 - 5) + 5;
    let pitch = Math.random() * (1.4 - 0.6) + 0.6;
    try {
        entity.runCommand(`playsound tp_pw:entity.${pet}.idle @a ~~~ 0.5 ${pitch}`)
    } catch (error) {
        
    }
    system.runTimeout(()=>{
        idleInterval(entity, pet)
    },delay * 20);
}
function hurtSound(entity, pet) {
    const volumeSetting = world.getDynamicProperty('tp_pw:sound')
    if(!entity.isValid()||!volumeSetting) return
    let pitch = Math.random() * (1.4 - 0.6) + 0.6;
    try {
        entity.runCommand(`playsound tp_pw:entity.${pet}.hurt @a ~~~ 1 ${pitch}`)
    } catch (error) {
    }
}
function deathSound(entity, pet, loc) {
    const volumeSetting = world.getDynamicProperty('tp_pw:sound')
    if(!entity.isValid()||!volumeSetting) return
    let pitch = Math.random() * (1.4 - 0.6) + 0.6;
    try {
        world.getDimension('overworld').runCommand(`playsound tp_pw:entity.${pet}.hurt @a ${loc.x} ${loc.y} ${loc.z} 1 ${pitch}`)
    } catch (error) {
    }
}
world.afterEvents.entitySpawn.subscribe(event=>{
    const entity = event.entity
    petsId.forEach(pet=>{
        if(entity.typeId===`tp_pw:`+pet || entity.typeId===`tp_pw:`+pet+`_untamed`) {
            idleInterval(entity, pet)
        }
    })
})
world.afterEvents.entityHurt.subscribe(event=>{
    const entity = event.hurtEntity
    petsId.forEach(pet=>{
        if(entity.typeId===`tp_pw:`+pet || entity.typeId===`tp_pw:`+pet+`_untamed`) {
            hurtSound(entity, pet)
        }
    })
})
world.afterEvents.entityDie.subscribe(event=>{
    const entity = event.deadEntity
    petsId.forEach(pet=>{
        if(entity.typeId===`tp_pw:`+pet || entity.typeId===`tp_pw:`+pet+`_untamed`) {
            const loc = {
                x:entity.location.x,
                y:entity.location.y,
                z:entity.location.z
            }
            deathSound(entity, pet, loc)
        }
    })
})