import { ApplicationCommandOptionType, REST, RESTPostAPIChatInputApplicationCommandsJSONBody, Routes } from 'discord.js';
import { CClient, Modul, SRecord } from '../utils/types';
import { log, prefix } from '../utils/utils';

const komandy: RESTPostAPIChatInputApplicationCommandsJSONBody[] = [
	{
		name: `${prefix}anketa`,
		description: "udelam fajnonovou anketu haby sy moch myt zbitecny anzori ostatnich",
		dm_permission: false,
		options: [
			{
				name: "moznosti",
				description: "mezi cim maj ty morove vibirat (odeluj carkama nebo pipama)",
				type: ApplicationCommandOptionType.String,
				required: true
			},
			{
				name: "cas",
				description: "jak dlouho budem cekat na ti bridili nez se rozhodnou",
				type: ApplicationCommandOptionType.Integer
			},
			{
				name: "jmena",
				description: "jestli chces strapnovat ostatni jejich vibjerem tak tuto zapni",
				type: ApplicationCommandOptionType.Boolean
			},
			{
				name: "neprubezny",
				description: "jestli chces haby bili visledky vydet hned nebo az potom co bude pozde sy to rozmyslet",
				type: ApplicationCommandOptionType.Boolean
			}
		]
	},

	{
		name: `${prefix}vyhledej`,
		description: "vihledavani tak dobri ze je dyckscord muze jenom zavydet",
		dm_permission: false,
		options: [
			{
				name: "kontent",
				description: "nejaki pysmenka kteri ve spravje jsou v tutom poradi",
				type: ApplicationCommandOptionType.String,
				required: false
			},
			{
				name: "regex",
				description: "regulerni exprese ktera se aplykuje na obsach spravy (hestly nevys co to je pouzij radsi 'kontent')",
				type: ApplicationCommandOptionType.String,
				required: false
			},
			{
				name: "uzivatel",
				description: "kerej kokot spavu posral",
				type: ApplicationCommandOptionType.User,
				required: false
			},
			{
				name: "od",
				description: "vihledat zpravy mladsy nez tuten datum\npotporuju vsechni formati jako new Date() v jávaskryptech",
				type: ApplicationCommandOptionType.String,
				required: false
			},
			{
				name: "do",
				description: "vihledat zpravy starsi nez tuten datum\npotporuju vsechni formati jako new Date() v jávaskryptech",
				type: ApplicationCommandOptionType.String,
				required: false
			},
			{
				name: "obrasek",
				description: "estli sprava ma nejakej obrasek (a jakoze fakt vobrasek, ne otkas na gif)",
				type: ApplicationCommandOptionType.Boolean,
				required: false
			},
			{
				name: "link",
				description: "estli sprava ma link",
				type: ApplicationCommandOptionType.Boolean,
				required: false
			},
			{
				name: "seradit",
				description: "jak visletki poskladat",
				type: ApplicationCommandOptionType.String,
				choices: [{
					name: "ot polen co nejlip horej",
					value: "ASC"
				},
				{
					name: "ot tech nejvic fres",
					value: "DESC"
				}
				],
				required: false
			},
			{
				name: "ma_soubor",
				description: "estli je u spravi nejakej souor",
				type: ApplicationCommandOptionType.Boolean,
				required: false
			},
			{
				name: "nazev_souboru",
				description: "vihledat spravi se souborem s takovim nazvem",
				type: ApplicationCommandOptionType.String,
				required: false
			},
		]
	}
];

const exp: Modul = {
	on_clientReady: async () => {
		const client = module.exports.client as CClient;
		client.slashCommandy = {};

		// Construct and prepare an instance of the REST module
		const rest = new REST({ version: '10' }).setToken(process.env.token!);

		// and deploy your commands!
		try {
			// The put method is used to fully refresh all commands in the guild with the current set
			const data = (await rest.put(
				Routes.applicationCommands(client.user!.id),
				{ body: komandy },
			)) as SRecord<string>[] | undefined;

			data?.forEach(command => {
				client.slashCommandy[command.name] = command.id;
			});
		} catch (error) {
			// And of course, make sure you catch and log any errors!
			log("Chyba při refreshování aplikačních (/) komandů", error);
		}

	}
};

module.exports = exp;
