import { Client, Message } from "discord.js";
import { replyTTS } from "../chatbot/addons/tts";
import axios from 'axios';
import { musicCommandMappings } from "./music";

console.log("Initializing Classic Bot Hook...");

const prefix = "!";

const musicChecks = (message: Message, client: Client): boolean => {
    const channel = message.member?.voice.channel;
    if (!channel) {
        message.reply(":no_entry_sign:... are you in a voice channel?");
        return false;
    }
    return true;
}
const commandMappings = new Map<string, (message: Message, client: Client) => void>([
    ["ping", (message: Message, client: Client) => {
        message.reply("Pong!");
    }],
    ["chat", (message: Message, client: Client) => {
        message.reply("You can't use this command anymore! Instead, just mention me in a message and I will respond!");
    }],
    ["help", (message: Message, client: Client) => {
        message.reply(`Commands:
        !ping - Pong!
        !help - Show this message!
        !about - Version Information
        !roll [number] - Roll a number between 1 and [number]
        !poke, !hug, !bonk, !wave - Random GIFs
        !waifu - Random Waifu
-- Music Commands --
        !play [song] - Searches the song on YouTube (or plays the YT/Soundcloud URL)
        !stop - Stops the music and leaves the voice channel
        !skip - Skips the current song
        !queue - Shows the current queue and song
        **or use JMusicBot using ";music play"** (only works in #bot)
-- Trivia! --
!trivia - Starts a new trivia game
!trivia leaderboard - Shows the trivia leaderboard
Respond to a trivia question with "answer" for forfeit the question and find out the answer.
-- Chat Bot (SmoothieGPT) --
        @mention: I will respond to any message that mentions me!

        If you are in a VC, I will join and speak my response! (TTS)
        `);

    }],
    ["about", (message: Message, client: Client) => {
        // get os version and node version
        const os = require('os');
        const osType = os.type();
        const osRelease = os.release();
        const nodeVersion = process.version;
        message.reply(`SinewareBot 2 aka. SmoothieGPT - running on Node ${nodeVersion} on ${osType} ${osRelease}.`);
    }],

    ["roll", (message: Message, client: Client) => {
        let args = message.content.split(" ");
        if(args.length < 2) {
            message.reply("You need to specify a number to roll!");
            return;
        }
        let max = parseInt(args[1]);
        if(isNaN(max)) {
            message.reply("You need to specify a number to roll!");
            return;
        }
        let roll = Math.floor(Math.random() * max) + 1;
        message.reply(`You rolled a ${roll}!`);
    }],
    ["say", async (message: Message, client: Client) => {
        await replyTTS(message, message.content.split(" ").slice(1).join(" "));
    }],
    ["poke", async (message: Message, client: Client) => { message.channel.send((await axios.get("https://api.waifu.pics/sfw/poke")).data.url); }],
    ["hug", async (message: Message, client: Client) => { message.channel.send((await axios.get("https://api.waifu.pics/sfw/hug")).data.url); }],
    ["bonk", async (message: Message, client: Client) => { message.channel.send((await axios.get("https://api.waifu.pics/sfw/bonk")).data.url); }],
    ["wave", async (message: Message, client: Client) => { message.channel.send((await axios.get("https://api.waifu.pics/sfw/wave")).data.url); }],
    ["waifu", async (message: Message, client: Client) => { /*message.channel.send((await axios.get("https://api.waifu.pics/sfw/waifu")).data.url);*/ message.channel.send("(Command Disabled)") }],
    
    ...musicCommandMappings
]);
    
export async function classicBotHook(message: Message, client: Client): Promise<boolean> {
    if(message.content.startsWith("!")) {
        let args = message.content.split(" ");
        let command = args[0].substring(1);
        if(commandMappings.has(command)) {
            try {
                commandMappings.get(command)!(message, client);
            } catch(e: any) {
                message.reply(`:fire: ${e.message}`);
                return true;
            }
            return true;
        }
        message.reply("Unknown command!");
    }
    return false;
}