import { APIResponse, get, post } from '.';
import { GuildChannel, GuildInfo } from './guilds.types';

export async function getGuilds(): Promise<GuildInfo[]> {
  const response = await get('guilds');
  const guilds: APIResponse<GuildInfo[]> = await response?.json();
  return guilds.data || [];
}

export async function getTextChannels(guildId: number): Promise<GuildChannel[]> {
  const response = await get(`guilds/${guildId}/text`);
  const channels: APIResponse<GuildChannel[]> = await response?.json();
  return channels.data || [];
}

export async function sendMessage(guildId: number, channelId: number, message: string): Promise<void> {
  await post(`guilds/${guildId}/text/${channelId}/message`, { message });
}

export async function getVoiceChannels(guildId: number): Promise<GuildChannel[]> {
  const response = await get(`guilds/${guildId}/voice`);
  const channels: APIResponse<GuildChannel[]> = await response?.json();
  return channels.data || [];
}

export async function playTrack(guildId: number, channelId: number, track: string): Promise<void> {
  await post(`guilds/${guildId}/voice/${channelId}/play`, { track_url: track });
}

export async function stopTrack(guildId: number): Promise<void> {
  await post(`guilds/${guildId}/voice/stop`, {});
}

export async function pauseTrack(guildId: number): Promise<void> {
  await post(`guilds/${guildId}/voice/pause`, {});
}

export async function resumeTrack(guildId: number): Promise<void> {
  await post(`guilds/${guildId}/voice/resume`, {});
}

export async function setVolume(guildId: number, volume: number): Promise<void> {
  await post(`guilds/${guildId}/voice/volume`, { volume: `${volume}` });
}

export async function skipTrack(guildId: number): Promise<void> {
  await post(`guilds/${guildId}/voice/skip`, {});
}

export async function getVolume(guildId: number): Promise<number> {
  const response = await get(`guilds/${guildId}/voice/volume`);
  const volume: number = await response?.json();
  return volume || 0;
}
