import { world, Player, system } from "@minecraft/server";
import { setScore, envSteal, getScore, lifesteal, lifestealLogs, addScore, randomN, random } from "../../lifesteal";
import { sendAllMessage, sendPlayerMessage } from "../../config";

/**
 * Detect death system
 * (This is the core lifesteal mechanic, die and lose a heart, kill and gain one)
 */
world.afterEvents.entityDie.subscribe(data => {
    let player = data.deadEntity as Player;
    let target = player.target as Player;

    if (lifesteal.get(`useplayerjson`) !== `true`) {
        setScore(player, `ls-hearts`, getScore(player, `ls-death`));
    }

    for (let tag of player.getTags()) {
        if (tag.startsWith(`lsh-`)) {
            player.removeTag(tag);
            setScore(player, `lsh-give`, -1);
        }
    }

    const hours = new Date().getUTCHours();
    const minutes = new Date().getUTCMinutes();
    const date = `${hours > 12 ? hours - 12 : hours}:${minutes} ${hours >= 12 && hours !== 24 ? `PM` : `AM`}`;
    const heartgain: string = lifesteal.get(`heartgain`);
    const heartlose = lifesteal.get(`heartlose`);
    const mode: string = lifesteal.get(`lifestealmode`);
    let heartsToAdd = +heartgain;
    if (mode.includes(`random`)) heartsToAdd = randomN(+mode.split(`-`)[1], +mode.split(`-`)[2]);

    if ((getScore(player, `ls-death`) - 1) === 0) sendAllMessage(`§c${player.name} has lost all their hearts.`);

    if (!player.hasTag(`heart-notify-off`) && !player.hasTag(`ls-message-debounce`)) {
        if (Math.sign(+heartlose) === -1) {
            const absHeartLose = Math.abs(+heartlose);
            sendPlayerMessage(player, `§8[§a!§8] §aYou gained ${absHeartLose === 1 ? `a` : `${absHeartLose}`} ${absHeartLose === 1 ? `heart` : `hearts`}.`);
        } else {
            sendPlayerMessage(player, `§8[§c!§8] §cYou lost ${+heartlose === 1 ? `a` : `${heartlose}`} ${+heartlose === 1 ? `heart` : `hearts`}.`);
        }
    }

    if (envSteal == true) {
        let hearts = getScore(player, `ls-death`);
        let check = hearts < heartlose;

        addScore(player, `ls-death`, -(+heartlose));
        lifestealLogs.push(`§6${date} > ${player.name} lost ${heartlose} heart(s)`);

        if (!target) return;
        if (target.typeId != `minecraft:player` || player.typeId != `minecraft:player` || player === target) return;

        if (check == true) {
            addScore(player, `ls-death`, hearts);
            lifestealLogs.push(`§6${date} > ${target.name} gained ${hearts} heart(s) from killing ${player.name}`);
            return;
        }

        addScore(target, `ls-death`, heartsToAdd);
        lifestealLogs.push(`§6${date} > ${target.name} gained ${heartsToAdd} heart(s) from killing ${player.name}`);
    }
    if (envSteal == false) {
        if (!target) return;
        if (target.typeId != `minecraft:player` || player.typeId != `minecraft:player`) return;

        let hearts = getScore(player, `ls-death`);
        let check = hearts < heartlose;

        addScore(target, `ls-death`, +heartlose);
        lifestealLogs.push(`§6${date} > ${player.name} lost ${heartlose} heart(s)`);

        if (check == true) {
            addScore(player, `ls-death`, hearts);
            lifestealLogs.push(`§6${date} > ${target.name} gained ${hearts} heart(s) from killing ${player.name}`);
            return;
        }

        addScore(player, `ls-death`, -(heartsToAdd));
        lifestealLogs.push(`§6${date} > ${target.name} gained ${heartsToAdd} heart(s) from killing ${player.name}`);
    }

    system.runTimeout(() => {
        setScore(player, `lsh-give`, 0);
    }, 1);
}, { entityTypes: [`minecraft:player`] });