import { world, Player, system } from '@minecraft/server';
import { Command } from '../../handler';
import { sendPlayerMessage } from '../../../config';
import { sleep, random, rpsLd } from '../../../lifesteal';

type countInfo = {
    player: Player,
    previous: string;
    previous2?: string;
};

let count: countInfo[] = [];

new Command({
    name: `rps`,
    aliases: [`rockpaperscissors`],
    ghostsOnly: true,
    code: ({ player, args }) => {
        system.run(async () => {
            if (player.hasTag(`ls-ghostcommand-cd`)) return sendPlayerMessage(player, `§8[§cX§8] §cYou are on cooldown for rps!`);
            player.addTag(`ls-ghostcommand-cd`);
            system.runTimeout(() => {
                player.removeTag(`ls-ghostcommand-cd`);
            }, 80);

            let acceptable = [
                `rock`,
                `paper`,
                `scissors`
            ];
            if (!acceptable.includes(args[0])) return sendPlayerMessage(player, `§8[§cX§8] §cYou can only do rock paper or scissors!`);
            let chosen1 = args[0];

            let find = count.find(x => x.player == player);
            if (!find) {
                count.push({
                    player: player,
                    previous: chosen1
                });
            }
            else {
                let splice = count.splice(count.findIndex(x => x.player == player));
                count.push({
                    player: player,
                    previous: chosen1,
                    previous2: splice[0].previous
                });
            }


            let chosen2 = random(1, false, `rps`);

            if (find) {
                if (find.previous == `rock`) {
                    if (find.previous2 == `rock`) {
                        chosen2 = random(1, false, `pppprr`);
                    }
                    if (find.previous2 == `paper`) {
                        chosen2 = random(1, false, `ppprss`);

                    }
                    if (find.previous2 == `scissors`) {
                        chosen2 = random(1, false, `ppprrs`);
                    }
                }
                if (find.previous == `paper`) {
                    if (find.previous2 == `rock`) {
                        chosen2 = random(1, false, `sssppr`);
                    }
                    if (find.previous2 == `paper`) {
                        chosen2 = random(1, false, `sssspp`);

                    }
                    if (find.previous2 == `scissors`) {
                        chosen2 = random(1, false, `sssrrp`);
                    }
                }
                if (find.previous == `scissors`) {
                    if (find.previous2 == `rock`) {
                        chosen2 = random(1, false, `rrrpps`);
                    }
                    if (find.previous2 == `paper`) {
                        chosen2 = random(1, false, `rrrpss`);

                    }
                    if (find.previous2 == `scissors`) {
                        chosen2 = random(1, false, `rrrrss`);
                    }
                }
            }

            if (chosen2 == `r`) chosen2 = `rock`;
            if (chosen2 == `p`) chosen2 = `paper`;
            if (chosen2 == `s`) chosen2 = `scissors`;


            sendPlayerMessage(player, `3`);
            player.playSound(`random.orb`);
            await sleep(10);
            sendPlayerMessage(player, `2`);
            player.playSound(`random.orb`, { pitch: 1.1 });
            await sleep(10);
            sendPlayerMessage(player, `1`);
            player.playSound(`random.orb`, { pitch: 1.2 });
            await sleep(10);
            player.playSound(`mob.villager.yes`);
            player.playSound(`mob.villager.no`);
            sendPlayerMessage(player, `<${player.name}> ${chosen1}`);
            sendPlayerMessage(player, `<AI> ${chosen2}`);
            await sleep(10);
            if (chosen1 == chosen2) return sendPlayerMessage(player, `§bTie!`);
            if (chosen1 == `rock`) {
                if (chosen2 == `paper`) {
                    player.playSound(`mob.villager.no`);
                    sendPlayerMessage(player, `§cYou lost!`);
                    return;
                }
                player.playSound(`mob.villager.yes`);

                let score;
                if (!rpsLd.get(`${player.name}`)) score = 0;
                else score = rpsLd.get(`${player.name}`);

                rpsLd.set(`${player.name}`, score + 1);
                sendPlayerMessage(player, `§aYou won!`);
            }
            if (chosen1 == `paper`) {
                if (chosen2 == `scissors`) {
                    player.playSound(`mob.villager.no`);
                    sendPlayerMessage(player, `§cYou lost!`);
                    return;
                }
                player.playSound(`mob.villager.yes`);

                let score;
                if (!rpsLd.get(`${player.name}`)) score = 0;
                else score = rpsLd.get(`${player.name}`);

                rpsLd.set(`${player.name}`, score + 1);
                sendPlayerMessage(player, `§aYou won!`);
            }
            if (chosen1 == `scissors`) {
                if (chosen2 == `rock`) {
                    player.playSound(`mob.villager.no`);
                    sendPlayerMessage(player, `§cYou lost!`);
                    return;
                }
                player.playSound(`mob.villager.yes`);

                let score;
                if (!rpsLd.get(`${player.name}`)) score = 0;
                else score = rpsLd.get(`${player.name}`);

                rpsLd.set(`${player.name}`, score + 1);
                sendPlayerMessage(player, `§aYou won!`);
            }
        });
    }
});