/*
    @author Hertz
    @version 1.0
*/
var PlayerUtil = Java.loadClass('net.threetag.palladium.util.PlayerUtil');

global.playSoundToAll = function(entity, range, sound, category, volume, pitch) {
    // PlayerUtil['playSoundToAll(net.minecraft.class_1937,double,double,double,double,net.minecraft.class_3414,net.minecraft.class_3419,float,float)'](entity.getLevel(), entity.x, entity.y, entity.z, range, sound, category, volume, pitch);
    PlayerUtil['playSoundToAll(net.minecraft.world.level.Level,double,double,double,double,net.minecraft.resources.ResourceLocation,net.minecraft.sounds.SoundSource,float,float)'](entity.getLevel(), entity.x, entity.y, entity.z, range, sound, category, volume, pitch);
}

function resolveAllegedBooleanFromObject(thing) {
    if (thing.toString() == 'true') { return true; }
    if (thing.toString() == 'false') { return false; }
    return null
}

var soundTimer = 0;
var soundInterval = 6;

var otherSoundTimer = 0;

StartupEvents.registry('palladium:abilities', (event) => {
    
    event.create('arrzenhanced:speed_sounds')
    .icon(palladium.createItemIcon('minecraft:note_block'))
    .addProperty('loop', 'boolean', true, 'Loop t/f')
    .firstTick((entity, entry, holder, enabled) => global.doSpeedSounds_firstTick_conflictAvoidingSuffix(entity, entry, holder, enabled))
    .tick((entity, entry, holder, enabled) => global.doSpeedSounds_conflictAvoidingSuffix(entity, entry, holder, enabled));

    event.create('arrzenhanced:looped_conditional_audio')
    .icon(palladium.createItemIcon('minecraft:note_block'))
    .addProperty('sound', 'string', 'minecraft:ambient.cave', 'Sound to play')
    .addProperty('sound_interval', 'integer', 5, 'Interval to play sounds at')
    .tick((entity, entry, holder, enabled) => global.doConditionalSound_conflictAvoidingSuffix(entity, entry, holder, enabled))
    .lastTick((entity, entry, holder, enabled) => global.doConditionalSound_lastTick_conflictAvoidingSuffix(entity, entry, holder, enabled));
});

global.doSpeedSounds_firstTick_conflictAvoidingSuffix = (entity, entry, holder, enabled) => {
    if (enabled) {
        var lightningColor = palladium.getProperty(entity, 'arrzenhanced.speedColor')
        var soundMappings = {
            0: "generic",
            1: "orange",
            2: "white",
            3: "blue",
            4: "white",
            5: "red",
            6: "generic", // green
            7: "generic", // purple
            8: "generic", // pink
            9: "white", // flash
            10: "white", // luna
            11: "red", // hertz
            12: "generic", // arrzee
        }
        var resourceLocation = soundMappings[lightningColor] == undefined ? soundMappings[0] : soundMappings[lightningColor];
        var finalLoc = "arrzenhanced:speedforce." + resourceLocation + ".sprint"

        // entity.tell('ur color is')
        // entity.tell(resourceLocation)
        global.playSoundToAll(entity, 12, finalLoc, "players", 1, 1)
    }
}

global.doSpeedSounds_conflictAvoidingSuffix = (entity, entry, holder, enabled) => {
    var looper = resolveAllegedBooleanFromObject(entry.getPropertyByName('loop'));
    if (looper) {
        soundTimer++;
    }
    if (enabled) {
        var lightningColor = palladium.getProperty(entity, 'arrzenhanced.speedColor')
        var soundMappings = {
            0: "generic",
            1: "orange",
            2: "white",
            3: "blue",
            4: "white",
            5: "red",
            6: "generic", // green
            7: "generic", // purple
            8: "generic", // pink
            9: "white", // flash
            10: "white", // luna
            11: "white", // hertz
        }
        var resourceLocation = soundMappings[lightningColor] == undefined ? soundMappings[0] : soundMappings[lightningColor];
        var finalLoc = "arrzenhanced:speedforce." + resourceLocation + ".sprint"

        if (soundTimer >= soundInterval && looper) {
            global.playSoundToAll(entity, 12, finalLoc, "players", 1, 1)
            soundTimer = 0;
        }
    }
}

global.doConditionalSound_conflictAvoidingSuffix = (entity, entry, holder, enabled) => {
    otherSoundTimer++;
    if (enabled) {
        var sound = entry.getPropertyByName('sound')
        var sound_interval = entry.getPropertyByName('sound_interval')

        if (otherSoundTimer >= sound_interval) {
            global.playSoundToAll(entity, 20, sound, "players", 1, 1)
            otherSoundTimer = 0;
        }
    }
}
global.doConditionalSound_lastTick_conflictAvoidingSuffix = (entity, entry, holder, enabled) => {
    return
    if (enabled) {
        var sound = entry.getPropertyByName('sound')
        entity.server.runCommandSilent(`stopsound @a * ${sound}`)
    }
}

StartupEvents.registry('sound_event', event => {
    event.create('arrzenhanced:speedforce.generic.sprint')
    event.create('arrzenhanced:speedforce.orange.sprint')
    event.create('arrzenhanced:speedforce.white.sprint')
    event.create('arrzenhanced:speedforce.blue.sprint')
    event.create('arrzenhanced:speedforce.red.sprint')
    event.create('arrzenhanced:speedforce.aerokinesis_loop')
    event.create('arrzenhanced:shieldthrowloop')
    event.create('arrzenhanced:speedburst_shield_swing')
})