import { world, Player, system } from '@minecraft/server';
import { ActionFormData, FormCancelationReason, ModalFormData } from '@minecraft/server-ui';
import { Command } from '../../handler';
import { sendPlayerMessage } from '../../../config';
import { getScore, ccLd } from '../../../lifesteal';

new Command({
    name: `cookieclicker`,
    aliases: [`cc`],
    ghostsOnly: true,
    code({ player, args }) {
        let balance: number;
        if (!balance) balance = 0;
        try {
            balance = getScore(player, `ls-cc-ld`);
        } catch { }
        sendPlayerMessage(player, `§3Close chat to open Cookie Clicker`);
        function don(player: Player) {
            const form = new ModalFormData()
                .title(`Cookie clicker`)
                .toggle(`Close\n§7Close game when you click submit`)
                .dropdown(`Click submit without any toggles on to get a cookie\nCookies:`, [`${balance}`]);
            form.show(player).then(result => {
                if (result.canceled && result.cancelationReason == `UserBusy`) don(player);
                if (!result.formValues[0]) {
                    balance += 1;
                    let score;

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

                    ccLd.set(`${player.name}`, score + 1);
                    don(player);
                    return;
                }
                if (result.formValues[0]) return;
                don(player);
            });
        }
        don(player);
    }
});