/* ========================================================================
=== Server Script ===

Thank you so much, @vomiter, for the script that applies light.
The script really helped me:
https://discord.com/channels/303440391124942858/1362074755598913656/1362178494607528136

Important Notes:
-----------------
This script also requires the mod https://www.curseforge.com/minecraft/mc-mods/dynamiclights-reforged
for the light effect to function.

coded by @andrezinh0

======================================================================== */

// CONFIGURATION
// Damage values: 1 damage equals half a heart, so 1 full heart equals 2 damage
let lowDamage = 4;
let mediumDamage = 8;
let highDamage = 12;

// Light settings
// Duration of the light in ticks
let flashTime = 0;
// Valid light levels range from 0 to 15
let lightLevel = 4;
let highlightLevel = 7;

// Enable or disable debug logging
let log = false;

let $SwordBlockingHandler = Java.loadClass('fuzs.swordblockingmechanics.handler.SwordBlockingHandler')
let $EventResult = Java.loadClass("fuzs.puzzleslib.api.event.v1.core.EventResult"); 

EntityEvents.hurt(event => {
    let player = event.player;
    if (!player) return;

    let damageSource = event.source
    let damageAmount = event.damage

    let result = $SwordBlockingHandler.onLivingAttack(player, damageSource, damageAmount);

    if (result.equals($EventResult.INTERRUPT)) {

    if (log) {
        console.log("parry")
        console.log(damageAmount);
    }

    let lookAngle = player.lookAngle
    
    let lookDistance = 1.5;
    let lookX = player.x + lookAngle.x() * lookDistance;
    let lookY = player.y + player.getEyeHeight() + lookAngle.y() * lookDistance;
    let lookZ = player.z + lookAngle.z() * lookDistance;

    let isVeryLowDamage = damageAmount < lowDamage;
    let isLowDamage = damageAmount >= lowDamage && damageAmount < mediumDamage;
    let isMediumDamage = damageAmount >= mediumDamage && damageAmount < highDamage;
    let isHighDamage = damageAmount >= highDamage;

    let light = player.block.createEntity("item");
    
        // https://github.com/iron431/irons-spells-n-spellbooks/blob/1.20.1/src/main/java/io/redspace/ironsspellbooks/particle/ShockwaveParticleOptions.java#L25
        // 'irons_spellbooks:shockwave Vector3f color, float scale, boolean glowing, trailParticle'

        if (isVeryLowDamage) {
            player.level.spawnParticles('minecraft:cloud', true, lookX, lookY, lookZ, 0.1, 0.1, 0.1, 1, 0.1)
        }

        if (isLowDamage) {
            player.level.spawnParticles('minecraft:cloud', true, lookX, lookY, lookZ, 0.1, 0.1, 0.1, 1, 0.1)
            player.level.spawnParticles('irons_spellbooks:spark 0.3 0.3 0.3', true, lookX, lookY, lookZ, 0.0, 0.0, 0.0, 4, 0.4)
        }

        if (isMediumDamage) {
            player.level.spawnParticles('irons_spellbooks:spark 1 0.8 0.6', true, lookX, lookY, lookZ, 0.0, 0.0, 0.0, 8, 0.2)
            player.level.spawnParticles('irons_spellbooks:spark 1 0.5 0.4', true, lookX, lookY, lookZ, 0.0, 0.0, 0.0, 8, 0.3)

            event.server.scheduleInTicks(flashTime, _ => {
                light.kill();
            });
        }

        if (isHighDamage) {
            player.level.spawnParticles('irons_spellbooks:spark 1 0.8 0.6', true, lookX, lookY, lookZ, 0.0, 0.0, 0.0, 14, 0.3)
            player.level.spawnParticles('irons_spellbooks:spark 1 0.5 0.4', true, lookX, lookY, lookZ, 0.0, 0.0, 0.0, 14, 0.4)

            event.server.scheduleInTicks(flashTime, _ => {
                light.kill();
            });
        }
    }
});
