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

// Heart item
world.afterEvents.itemUse.subscribe(data => {
    let player = data.source as Player;
    let item = data.itemStack;

    const hours = new Date().getUTCHours();
    const minutes = new Date().getUTCMinutes();
    const date = `${hours > 12 ? hours - 12 : hours}:${minutes} ${hours >= 12 && hours !== 24 ? `PM` : `AM`}`;

    if (item.typeId == `lifesteal:heart`) {
        if (getScore(player, `ls-death`) >= 200) {
            if (getScore(player, `ls-nomsg`) == 0) {
                sendPlayerMessage(player, `§cYou are at the max amount of hearts!`);
                player.playSound(`mob.chicken.plop`, { pitch: 1.2 });
            }
            setScore(player, `ls-nomsg`, 7);
            return;
        }
        if (getScore(player, `ls-death`) >= +lifesteal.get(`maxhearts`)) {
            if (getScore(player, `ls-nomsg`) == 0) {
                sendPlayerMessage(player, `§cYou are at the max amount of hearts!`);
                player.playSound(`mob.chicken.plop`, { pitch: 1.2 });
            }
            setScore(player, `ls-nomsg`, 7);
            return;
        }

        if (player.runCommand(`clear @s lifesteal:heart 0 1`).successCount == 0) return;
        addScore(player, `ls-death`, 1);
        player.playSound(`heart`);
        lifestealLogs.push(`§6${date} > ${player.name} gained a heart by using a heart item`);
    }
});