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

const timeofday = world.getTimeOfDay

world.beforeEvents.itemUseOn.subscribe((event) => {

    const player = event.source
    const block = event.block
    const equippable = player.getComponent("equippable");
    const item = equippable.getEquipment("Mainhand");
    if (!equippable) return
    
    if (!item) return

    if (player.lastTick == undefined) { player.lastTick = 0 }
    if (system.currentTick - player.lastTick > 80) {
        if (item.typeId === "arcedge_horr:b_rea_scythe") {
        player.runCommandAsync(`summon arcedge_horr:b_rea_min_friendly ${block.x} ${block.y + 1} ${block.z}`);
        player.lastTick = system.currentTick;
        }
    }
})

world.afterEvents.entityHitEntity.subscribe((event) => {
    const player = event.damagingEntity;


    const equippable = player.getComponent("equippable");
    if (!equippable) return
    const item = equippable.getEquipment("Mainhand");
    
    const attacker = event.damagingEntity;
    const victim = event.hitEntity;

    const VictimHealth = victim.getComponent("minecraft:health");
    const AttackerHealth = attacker.getComponent("minecraft:health");
    if (!VictimHealth) return;
    if (!AttackerHealth) return;

    if (!item) return

    switch (item.typeId) {
        // Reaper Scythe
        case "arcedge_horr:b_rea_scythe":
            victim.addEffect('wither', 200, {amplifier: 5})
            break
        
        // Frank Hammer
        case "arcedge_horr:frnk_weapon":
            const PlayerViewVec = attacker.getViewDirection();
            if (attacker.isSneaking) {
            attacker.runCommandAsync('particle minecraft:knockback_roar_particle ~ ~ ~')
            victim.applyKnockback(PlayerViewVec.x, PlayerViewVec.z, 1, 0.45)
            attacker.addEffect('hunger', 60, {amplifier: 4})
            }
            break

        // Werewolf Claws
        case "arcedge_horr:were_weapon":
            const NightCheck = isNight()
            if (NightCheck) {
                attacker.addEffect('speed', 100, {amplifier: 1})
                attacker.addEffect('strength', 100, {amplifier: 1})
            }
            break


        // Witch Staff
        case "arcedge_horr:wtc_stf":
            const WitchRan = SetWitchRan(1, 3)

            if (WitchRan === 1) {
                victim.addEffect('poison', 60, {amplifier: 1})
            }
            if (WitchRan === 2) {
                victim.addEffect('instant_damage', 60, {amplifier: 1})
            }
            if (WitchRan === 3) {
                victim.addEffect('weakness', 60, {amplifier: 1})
            }
            break

        // Frank Hammer
        case "arcedge_horr:zmb_mal":
            const PlayerViewVecMal = attacker.getViewDirection();
            if (attacker.isSneaking) {
            victim.addEffect('weakness', 60, {amplifier: 1})
            attacker.addEffect('hunger', 60, {amplifier: 4})
            }
            break
        
    }
})

world.afterEvents.entityDie.subscribe((event) => {
    const player = event.damageSource.damagingEntity
    if (!player) return

        if (player.typeId === 'minecraft:player') {
    const testDagger = player.runCommand('testfor @s[hasitem={item=arcedge_horr:vmp_dggr, quantity=1, location=slot.weapon.mainhand}]').successCount;
    if (testDagger === 1) {
        event.damageSource.damagingEntity.addEffect('instant_health', 1)
        };
    }
})

function SetWitchRan(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function isNight() {
    const time = world.getTimeOfDay();
    return time >= 13000 || time < 1000;
}

