import {system, world} from "@minecraft/server"
import {getHeight} from "cubc/ra_su/hitboxes"

export function getScore(entity, scoreboard) {
    try {
        return world.scoreboard.getObjective(scoreboard).getScore(entity);
    } catch {
    }
    console.warn(`scoreboard ${scoreboard} not found on ${entity.nameTag}`);
    return 0;
}

function noSpecialAbility(itemName) {
    return !itemName.includes("cubc_ra_su:");
}


world.afterEvents.entityHurt.subscribe((event) => {
    let damager = event.damageSource.damagingEntity;
    let hurtEntity = event.hurtEntity;
    if (damager === undefined)
        return;

    let damager_id = damager.typeId.replace('minecraft:', '');
    if (damager_id === "player") {
        const equipment = damager.getComponent('equippable');
        const item = equipment.getEquipment('Mainhand');
        if (item) {
            customHit(damager, hurtEntity, item.typeId);
        }
    }
});

world.afterEvents.itemUse.subscribe(eventData => {
    const item = eventData.itemStack
    const player = eventData.source
    const type_id = item.typeId;

    if (type_id.includes('cubc_ra_su:mini_cannon')) {
        player.runCommand(`execute if entity @s[scores={cubc_ra_su_item_cooldown=0}] run function cubc/ra_su/weapon/mini_cannon/use`);
        player.runCommand(`scoreboard players set @s[scores={cubc_ra_su_item_cooldown=0}] cubc_ra_su_item_cooldown 15`);
    }
    if (type_id.includes('cubc_ra_su:slingshot')) {
        player.runCommand(`execute if entity @s[scores={cubc_ra_su_item_cooldown=0}] run function cubc/ra_su/weapon/slingshot/use`);
        player.runCommand(`scoreboard players set @s[scores={cubc_ra_su_item_cooldown=0}] cubc_ra_su_item_cooldown 15`);
    }
    if (type_id.includes('cubc_ra_su:spear')) {
        player.runCommand(`execute if entity @s[scores={cubc_ra_su_item_cooldown=0}] run function cubc/ra_su/weapon/spear/use`);
        player.runCommand(`scoreboard players set @s[scores={cubc_ra_su_item_cooldown=0}] cubc_ra_su_item_cooldown 15`);
    }
})

world.afterEvents.projectileHitEntity.subscribe((event) => {
    let damager = event.source;
    let hurtEntity = event.getEntityHit().entity;
    if (damager === undefined)
        return;

    let damager_id = damager.typeId.replace('minecraft:', '');
    if (damager_id === "player" && event.projectile.typeId.includes('shuriken')) {
        customHit(damager, hurtEntity, event.projectile.typeId);
    }
});


function isTool(typeId) {
    const toolTypes = ["_pickaxe", "lunarite_axe", "meteorite_axe", "solarite_axe", "_hoe", "_shovel"];
    return toolTypes.some(tool => typeId.includes(tool)) && !typeId.includes("battle_pickaxe");
}

function customHit(player, hurtEntity, typeId) {
    if (isTool(typeId) || noSpecialAbility(typeId)) {
        return;
    }
    player.runCommand("tag @e remove cubc_ra_su_damager");
    player.runCommand("tag @s add cubc_ra_su_damager");

    hurtEntity.runCommand("tag @e remove cubc_ra_su_hurt");
    hurtEntity.runCommand("tag @s add cubc_ra_su_hurt");
    hurtEntity.runCommand(`execute positioned ~~${getHeight(hurtEntity) / 2}~ run function cubc/ra_su/particle/water_splash_1`);


    if (typeId.includes('copper')) {
        hurtEntity.runCommand('summon lightning_bolt');
        hurtEntity.runCommand('function cubc/ra_su/particle/explosion2');
    }

    if (typeId.includes('long_sword')) {
        hurtEntity.runCommand('execute as @p[rm=0.01] at @s anchored eyes positioned ^^^3 run damage @e[r=3, family=mob, rm=0.01] 5 magic');
        hurtEntity.runCommand('execute as @p[rm=0.01] at @s anchored eyes positioned ^^^4 run damage @e[r=2, family=mob, rm=0.01] 5 magic');
    }

    if (typeId.includes('scythe')) {
        player.runCommand('particle cubc_ra_su:scythe_sway');
        player.runCommand('damage @e[r=3, family=mob, rm=0.01, tag=!cubc_ra_su_hurt] 5 magic');
        player.runCommand('damage @e[r=3, type=player, rm=0.01, tag=!cubc_ra_su_hurt] 5 magic');
    }

    if (typeId.includes('pirate_sword')) {
        hurtEntity.runCommand('effect @s fatal_poison 5 2 true');
        hurtEntity.runCommand('effect @e[r=3, family=mob] fatal_poison 5 2 true');
        hurtEntity.runCommand('scriptevent cubc_ra_su:repeat repeat:5, delay:7 "/particle cubc_ra_su:poison_splash_1_3');
    }

    if (typeId.includes('ice')) {
        hurtEntity.runCommand('effect @s slowness 5 255 true');
        hurtEntity.runCommand('effect @e[r=3, family=mob] slowness 5 255 true');
        hurtEntity.runCommand('function cubc/ra_su/particle/ice_explosion_medium_1');
    }

    if (typeId.includes('redstone')) {
        hurtEntity.runCommand('effect @s wither 5 1 true');
    }

    if (typeId.includes('battle_pickaxe')) {
        hurtEntity.runCommand(`effect @e[r=2, family=mob] wither 4 1 true`);
        hurtEntity.runCommand(`scriptevent cubc_ra_su:repeat repeat:8, delay:10 "/particle cubc_ra_su:skeleton_head_player`);
    }

    if (typeId.includes('cleaver')) {
        hurtEntity.runCommand('effect @s slowness 5 5 true');
        hurtEntity.runCommand('effect @e[r=3, family=mob] slowness 5 255 true');
        hurtEntity.runCommand('function cubc/ra_su/particle/ice_explosion_medium_1');
    }

    if (typeId.includes('dagger')) {
        player.runCommand('effect @s speed 5 2');
    }

    if (typeId.includes('battle_axe')) {
        hurtEntity.runCommand('scriptevent cubc_ra_su:delay 11 "/damage @s 3 magic');
    }

    if (typeId.includes('kama')) {
        player.runCommand("execute as @s anchored eyes positioned ^^-0.5^-1 run summon cubc_ra_su:launcher ~~~ ~~ cubc_ra_su:player_launch");
    }

    if (typeId.includes('spiked_hammer')) {
        hurtEntity.runCommand("summon cubc_ra_su:instant_knockback");
    }

    if (typeId.includes('_club')) {
        hurtEntity.runCommand("effect @s[family=mob] slowness 255 5 true");
        hurtEntity.runCommand("effect @s slowness 2 5 true");
    }

    if (typeId.includes('hook')) {
        if (Math.random() < 0.3) {
            hurtEntity.runCommand('effect @p[rm=0.01, r=10] regeneration 1 1 true');
        }
    }

    if (typeId.includes('halberd')) {
        hurtEntity.runCommand('execute as @e[family=mob, r=3] run scriptevent cubc_ra_su:knock_up');
        hurtEntity.runCommand('execute as @a[tag=!cubc_ra_su_damager, r=3] run scriptevent cubc_ra_su:knock_up');
    }

    if (typeId.includes('hammer')) {
        player.runCommand('execute at @e[tag=cubc_ra_su_hurt] run summon cubc_ra_su:hurt_launcher');
    }

    if (typeId.includes('great_axe')) {
        player.runCommand('damage @e[tag=cubc_ra_su_hurt] 5 magic');
    }
}