import { world, Player, system } from '@minecraft/server';
import { Command } from '../../handler';
import { sendPlayerMessage } from '../../../config';
import { getPlayer, getScore, random, randomN, setScore } from '../../../lifesteal';

new Command({
    name: `jumpscare`,
    ghostsOnly: true,
    code({ player, args }) {
        const cooldown = player.getDynamicProperty(`jumpscare-cooldown`) as number | undefined;

        if (cooldown ? cooldown > Date.now() : false) {
            player.sendMessage(`§8[§cX§8] §cYou are on cooldown! §6${timeStamp((cooldown as number) - Date.now())}§c left.`);

            player.playSound(`random.break`, { volume: 0.5, pitch: 8 });
            return;
        }

        const target = getPlayer(`${args.join(` `)}`);
        if (!target) {
            player.sendMessage(`§8[§cX§8] §cFailed to find player.`);
            player.playSound(`random.break`, { volume: 0.5, pitch: 8 });
            return;
        }

        sendPlayerMessage(player, `§cJumpscared §6${target.name}`);
        target.onScreenDisplay.hideAllExcept();
        player.addEffect(`darkness`, 60);
        target.playSound(`mob.warden.nearby_closest`, { volume: 0.2 });
        system.runTimeout(() => {
            target.playSound(`mob.enderdragon.growl`, { volume: 0.5 });
            target.playSound(`mob.enderdragon.death`, { volume: 0.5 });
            target.playSound(`mob.enderdragon.growl`, { pitch: 1.3, volume: 1 });
            target.playSound(`mob.enderdragon.death`, { pitch: 1.3, volume: 0.5 });
            target.playSound(`mob.warden.emerge`, { pitch: 0.5, volume: 0.3 });
            target.playSound(`mob.warden.emerge`, { pitch: 0.5, volume: 0.4 });
            target.playSound(`mob.warden.emerge`, { pitch: 0.5, volume: 0.5 });

            const randomnumber = randomN(1, 3);
            if (randomnumber === 1)
                target.onScreenDisplay.setTitle(`§c§lboo`);
            if (randomnumber === 2)
                target.onScreenDisplay.setTitle(`§c§lrahh!!`);
            if (randomnumber === 3)
                target.onScreenDisplay.setTitle(`§c§lbig roar`);
            target.onScreenDisplay.updateSubtitle(`§c§ljumpscared by ${player.name}`);
            target.onScreenDisplay.resetHudElements();
        }, 40);

        player.setDynamicProperty(`jumpscare-cooldown`, Date.now() + 60000);
    }
});

/**
 * stole this from papi (hi papi!!)
 * @name timeStamp
 * @param {number} time The milliseconds number which needs to be formatted.
 * @example timeStamp(20000);
 * @remarks Formats the milliseconds to a week, day, hour, minute, second format.
 * @returns {string} Returns the formatted timestamp string.
 */
const timeStamp = (time: number): string => {
    let seconds: number = Math.floor(time / 1000);
    let minutes: number = Math.floor(seconds / 60);
    let hours: number = Math.floor(minutes / 60);
    let days: number = Math.floor(hours / 24);
    const weeks: number = Math.floor(days / 7);

    seconds %= 60;
    minutes %= 60;
    hours %= 24;
    days %= 7;

    const duration: string[] = [];
    if (weeks) duration.push(`${weeks} week${weeks > 1 ? `s` : ``}`);
    if (days) duration.push(`${days} day${days > 1 ? `s` : ``}`);
    if (hours) duration.push(`${hours} hour${hours > 1 ? `s` : ``}`);
    if (minutes) duration.push(`${minutes} minute${minutes > 1 ? `s` : ``}`);
    if (seconds) duration.push(`${seconds} second${seconds > 1 ? `s` : ``}`);

    if (duration.length) return duration.join(`, `);
    else return `0 seconds`;
};