import { Interaction } from 'discord.js';

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


export async function np(interaction: Interaction) {
	await interaction.reply(nowPlayingResponse(interaction));
}

function formatQueue(interaction: Interaction) {
	let formattedText = "";
	const subscription = subscriptions.get(interaction.guildId);

	if(subscription.queue.length === 0) {
		return "Nothing is currently in the queue.";
	} else {
		formattedText = `Now playing: [**${subscription.nowPlaying.title}**](${subscription.nowPlaying.url}), added by ${subscription.nowPlaying.addedBy}`
	}

	formattedText = formattedText.concat(`\nUp next:`);

	subscription.queue.forEach((track) => {
		formattedText = formattedText.concat(`\n- **${track.title}**, added by ${track.addedBy}`)
	});

	return formattedText;
}

function nowPlayingResponse(interaction: Interaction) {
	return formatCallbackData(formatQueue(interaction), "In the queue");
}

function nowPlayingMessage(track: Track) {
	return formatCallbackData(`Now playing: [**${track.title}**](${track.url}), added by ${track.addedBy}`);
}

export async function nowPlayingCallback(subscription: MusicSubscription) {
	const channel = await getAllowedTextChannel(subscription);
	await channel.send(nowPlayingMessage(subscription.nowPlaying));
}

export const npCommand = {
	name: "np",
	description: "Shows the currently-playing song along with the next five songs in the queue"
};
