import { Interaction } from 'discord.js';

import { subscriptions } from "../index";
import { MusicSubscription } from '../subscription';
import { ensureVoiceConnection, formatCallbackData, waitingForResponse } from "../utils";

function alreadyLoopingResponse(interaction: Interaction) {
	const subscription = subscriptions.get(interaction.guildId);
	return formatCallbackData(`Looping is already enabled. 
			Currently playing: **${subscription.queue[0].title}**, added by ${subscription.queue[0].addedBy}`);
}

const nothingToLoopResponse = formatCallbackData(`The queue is empty.`);

function loopEnabledResponse(interaction: Interaction) {
	const subscription = subscriptions.get(interaction.guildId);
	return formatCallbackData(`Looping has been enabled. 
			Currently playing: **${subscription.queue[0].title}**, added by ${subscription.queue[0].addedBy}`);
}

export const loopCommand = {
	name: "loop",
	description: "Loops the currently playijng song. All other songs remain in the queue"
};

export async function loop(interaction: Interaction, subscription: MusicSubscription) {
	if (!interaction.guildId) return;
	await ensureVoiceConnection(interaction);
	await interaction.reply(waitingForResponse);

	if(subscription.queue.length === 0) {
		await interaction.editReply(nothingToLoopResponse);
	} else if(!subscription.looping){
		subscription.looping = true;
		await interaction.editReply(interaction);
	} else {
		await interaction.editReply(interaction);
	}
}