const { SlashCommandBuilder } = require('@discordjs/builders');
const noblox = require("noblox.js");
const { cookies, commands, gamePlaces } = require("../config/config.js");
const { getAssetInfo, buyModel, getCurrentUser, validateMapAsset, getValidationMessage } = require("../common.js");

/**
 * @param {import('discord.js').ChatInputCommandInteraction} interaction
 */
async function execute(interaction) {
    const game = interaction.options.getString("game", true);
    const cookie = cookies[game];
    if (cookie === undefined) {
        await interaction.editReply("🚫  Invalid game specified!");
        return;
    }

    const id = interaction.options.getInteger("asset_id", true);
    noblox.setCookie(cookie, false);

    let alreadyOwned;
    try {
        // Check if the bot already owns this asset
        alreadyOwned = await noblox.getOwnership(await getCurrentUser(), id, "Asset");
    } catch (error) {
        if (error.message !== "400 The specified Asset does not exist!") {
            throw error;
        }
        await interaction.editReply(`🚫  This asset does not exist (id: \`${id}\`).`);
        return;
    }
    
    // Validate that this is a model
    const assetInfo = await getAssetInfo(id);
    if (assetInfo.status === 404) {
        await interaction.editReply(`🚫  This [model](<https://create.roblox.com/store/asset/${id}/>) (id: \`${id}\`) is off sale. Please configure it to be on sale (Configure -> Distribute on Creator Store). Alternatively, the provided ID may not actually be a model.`);
        return;
    }
    if (assetInfo.status < 200 || assetInfo.status >= 300) {
        await interaction.editReply(`🚫  Something unexpected went wrong trying to retrive info about the model (id: \`${id}\`).`);
        return;
    }
    if (assetInfo.forSale === false) {
        await interaction.editReply(`🚫  This [model](<https://create.roblox.com/store/asset/${id}/>) (id: \`${id}\`) is off sale. Please configure it to be on sale (Configure -> Distribute on Creator Store).`);
        return;
    }
    if (!assetInfo.isModel) {
        await interaction.editReply(`🚫  This asset (id: \`${id}\`) is not a model. Your map must be a model.`);
        return;
    }
    if (!assetInfo.isFree) {
        await interaction.editReply(`🚫  This [model](<https://create.roblox.com/store/asset/${id}/>) (id: \`${id}\`) is not free. Please change the price to be free.`);
        return;
    }

    // Kick off the buy request
    let buyPromise;
    if (!alreadyOwned) {
        buyPromise = buyModel(id);
    }
    
    // Validate and send the validation result
    const validation = await validateMapAsset(id, game);
    const msg = getValidationMessage(validation, game, false);
    await interaction.editReply(msg);
    
    if (alreadyOwned) {
        // Unicode is for the information emoji [i]
        await interaction.followUp(`\u2139\uFE0F  The ${game} maptest bot already has this model, so no action was taken.`);
        return;
    }

    // Make sure the buy request is done
    const success = await buyPromise;
    if (!success) {
        await interaction.followUp(`🚫  Something went wrong when trying to buy the [model](<https://create.roblox.com/store/asset/${id}/>) (id: \`${id}\`).`);
        return;
    }

    await interaction.followUp(
`
Now that your [map (id: ${id})](<https://create.roblox.com/store/asset/${id}/>) has been taken by the bot you can load it into the [${game} maptest place](<${gamePlaces[game]}>). 
To load your map, join the game and do \`!map ${id}\`. If your map successfully loaded, do \`!rtv\` and then choose your map.
Otherwise, you can expand the chat to view the full error message by clicking and dragging on the edge of the chat.
`
    );
}

module.exports = {
	data: new SlashCommandBuilder()
		.setName('take')
		.setDescription('Takes an asset ID')
        .addStringOption(option =>
            option.setName("game")
            .setDescription("Select the maptest game")
            .setRequired(true)
            .addChoices(...commands))
        .addIntegerOption(option =>
            option.setName("asset_id")
            .setDescription("The asset ID of the model")
            .setRequired(true))
        ,
	execute
};