import {
    world,
    system,
    ItemStack
} from "@minecraft/server";

function getSoundName(sound) {
    var soundName = sound.replace("mm:music_disc_", "")
    soundName = soundName.replaceAll("_", "")
    soundName = "record." + soundName
    return soundName
}

const records = [
    "mm:music_disc_keeper",
    "mm:music_disc_myths_and_magic",
    "mm:music_disc_devoid",
    "mm:music_disc_devoid_altered",
    "mm:music_disc_farewell",
    "mm:music_disc_what"
]

world.beforeEvents.playerBreakBlock.subscribe((event) => {
    const block = event.block;
    if (block.type.id === "minecraft:jukebox") {

        const record = block.getComponent("minecraft:record_player")
        if (!record) return;
        if (!record.getRecord()) return;
        system.run(() => {
            for (const soundId of records) {
                const stopSound = getSoundName(soundId)
                event.player.runCommand("/stopsound @a[r=64] " + stopSound)
            }
        })
    }
});

world.beforeEvents.playerInteractWithBlock.subscribe((e) => {
    const block = e.block;
    const isFirst = e.isFirstEvent;
    const item = e.itemStack;
    const player = e.player;
    const dim = block.dimension;
    if (isFirst == false) return;
    if (player.isSneaking) return;
    const record = block.getComponent("minecraft:record_player")
    if (!record) return;
    if (record.getRecord()) {
        const stopSound = getSoundName(record.getRecord().typeId)
        system.run(() => {
            player.runCommand("/stopsound @a[r=64] " + stopSound)
        })
    } else if (records.includes(item.typeId)) {
        system.run(() => {
            dim.playSound(getSoundName(item.typeId), block.center())
            //player.runCommand("playsound " + getSoundName(item.typeId) + " @a[r=32] ~~~ 1 1 0")
            const players = dim.getPlayers({ maxDistance: 32, location: block.location })
            for (const target of players) {

                target.onScreenDisplay.setActionBar([{ text: "§c" }, { translate: "mm.nowPlaying" }, { translate: item.typeId.replace("mm:", "playing.").replace("music_disc_", "") }])
                system.runTimeout(() => {
                    target.onScreenDisplay.setActionBar([{ text: "§e" }, { translate: "mm.nowPlaying" }, { translate: item.typeId.replace("mm:", "playing.").replace("music_disc_", "") }])
                }, 5)
                system.runTimeout(() => {
                    target.onScreenDisplay.setActionBar([{ text: "§a" }, { translate: "mm.nowPlaying" }, { translate: item.typeId.replace("mm:", "playing.").replace("music_disc_", "") }])
                }, 10)
                system.runTimeout(() => {

                    target.onScreenDisplay.setActionBar([{ text: "§b" }, { translate: "mm.nowPlaying" }, { translate: item.typeId.replace("mm:", "playing.").replace("music_disc_", "") }])
                }, 15)

                system.runTimeout(() => {

                    target.onScreenDisplay.setActionBar([{ text: "§d" }, { translate: "mm.nowPlaying" }, { translate: item.typeId.replace("mm:", "playing.").replace("music_disc_", "") }])
                }, 20)

                system.runTimeout(() => {
                    target.onScreenDisplay.setActionBar([{ text: "§c" }, { translate: "mm.nowPlaying" }, { translate: item.typeId.replace("mm:", "playing.").replace("music_disc_", "") }])
                    system.runTimeout(() => {
                        target.onScreenDisplay.setActionBar([{ text: "§e" }, { translate: "mm.nowPlaying" }, { translate: item.typeId.replace("mm:", "playing.").replace("music_disc_", "") }])
                    }, 5)
                    system.runTimeout(() => {
                        target.onScreenDisplay.setActionBar([{ text: "§a" }, { translate: "mm.nowPlaying" }, { translate: item.typeId.replace("mm:", "playing.").replace("music_disc_", "") }])
                    }, 10)
                    system.runTimeout(() => {

                        target.onScreenDisplay.setActionBar([{ text: "§b" }, { translate: "mm.nowPlaying" }, { translate: item.typeId.replace("mm:", "playing.").replace("music_disc_", "") }])
                    }, 15)

                    system.runTimeout(() => {

                        target.onScreenDisplay.setActionBar([{ text: "§d" }, { translate: "mm.nowPlaying" }, { translate: item.typeId.replace("mm:", "playing.").replace("music_disc_", "") }])
                    }, 20)
                }, 25)
            }
        })
    }
})


// you get the what disc by killing furnace

world.afterEvents.entityDie.subscribe((e) => {
    const entity = e.deadEntity;
    if (!entity) return;
    if (!entity.isValid) return;
    if (entity.typeId !== "minecraft:player") return;
    const damager = e.damageSource.damagingEntity;
    if (!damager) return;
    if (!damager.isValid) return;
    if (damager.typeId !== "minecraft:player") return;

    if (entity.name == "WardedCentaur65") {
        const item = new ItemStack("mm:music_disc_what", 1)
        entity.dimension.spawnItem(item, entity.location)
    }
})