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

// ! Tick
system.runInterval(() => {
    // * Players
    world.getAllPlayers().forEach(player => {
        // * Test if Prismatica blade is in hand + Half Health
        if (
            player.runCommand('/testfor @s[hasitem={item=darkw_dotv:weapon_prismatica, location=slot.weapon.mainhand}]').successCount &&
            player.getComponent('health').currentValue <= player.getComponent('health').effectiveMax / 2
        ) {
            player.addEffect('speed', 10, { amplifier: 3, showParticles: false })
        }
        // * Triox Charm
        if (player.runCommand('/testfor @s[hasitem={item=darkw_dotv:charm_triox, location=slot.weapon.offhand}]').successCount && player.isSneaking && player.getItemCooldown('darkw_dotv:charm_triox') == 0) player.addEffect('resistance', 10, { showParticles: false })
        // * Test if Prismatica blade is in hand + Half Health
        if ((player.getComponent('health').currentValue <= player.getComponent('health').effectiveMax / 3) && player.hasTag('darkw_dotv:m4rkBonus')) {
            player.addEffect('resistance', 10, { amplifier: 1 })
            player.addEffect('speed', 10, { amplifier: 2 })
            player.addEffect('strength', 10, { amplifier: 5 })
        }
        // * Chaos Armor Setbonus
        if (player.hasTag('darkw_dotv:chaos_powerhungry')) player.getComponent('player.saturation').setCurrentValue(0)
        if (player.hasTag('darkw_dotv:chaos_powerhungry') && (player.getComponent('player.hunger').currentValue < 7)) {
            player.addEffect('speed', 5, { amplifier: 3, showParticles: false })
        }
        // * YINYANG
        if (player.runCommand('/testfor @s[hasitem={item=darkw_dotv:weapon_yinyang, location=slot.weapon.mainhand}]').successCount) {
            let mh = player.getComponent('equippable')
            let eq = mh.getEquipment('Mainhand')
            if (!eq.getDynamicProperty('darkw_dotv:limboselect')) {
                eq.setDynamicProperty('darkw_dotv:limboselect', 'limbo')
                mh.setEquipment(EquipmentSlot.Mainhand, eq)
                return
            }
            let souls = world.scoreboard.getObjective('darkw_dotv:limbosouls')
            if (!souls.getScore(player)) souls.setScore(player, 0)
            switch (eq.getDynamicProperty('darkw_dotv:limboselect')) {
                case ('limbo'): {
                    let title = `-| §d§kDEVIL§r |-| §fLIMBO §r|-| §k§uANGEL§r |-\n§rUse item to Swap Modes.\n            -=| ${souls.getScore(player)} |=-`
                    player.runCommand(`title @s actionbar ${title}`)
                    break
                }
                case ('angel'): {
                    let title = `-| §d§kDEVIL§r |-| §k§fLIMBO§r |-| §uANGEL§r |-\n§rUse item to Swap Modes.\n            -=| ${souls.getScore(player)} |=-`
                    player.runCommand(`title @s actionbar ${title}`)
                    break
                }
                case ('devil'): {
                    let title = `-| §dDEVIL§r |-| §k§fLIMBO§r |-| §k§uANGEL§r |-\n§rUse item to Swap Modes.\n            -=| ${souls.getScore(player)} |=-`
                    player.runCommand(`title @s actionbar ${title}`)
                    break
                }
            }
        }
    })
})