// Tady bude muzika, vole

import { AudioPlayerStatus, VoiceConnection } from "@discordjs/voice";
import { search, soundcloud, stream, validate, video_basic_info } from "play-dl";
import { emouty } from "../utils/emotes";
import { Modul, SRecord } from "../utils/types";
import { configureTimeAnouncment, getCurrentPlayer, joinVoice, play } from "../utils/utils";

const kjus: SRecord<{ name: string; url: string; }[]> = {};

async function zahrat(conn: VoiceConnection, url: string, seek?: number) {
  const src = await stream(url, { seek });
  play(conn, { name: src.stream, volume: 1, type: src.type });
}

const exp: Modul = {
  more_komandy: {
    zahraj: {
      DMUnsafe: true,
      run: async (mes, txt) => {
        const kanel = mes.member?.voice.channel;
        if (!kanel) return "nejsi ve vojsu ty kkt";

        const ajtem = { name: "", url: "" };
        const druh = await validate(txt);
        if (druh && druh != "search") {
          if (druh != "yt_video" && druh != "so_track") return "tuto neumim zahrat";
          ajtem.url = txt;
          if (druh == "yt_video") {
            video_basic_info(txt).then(v => {
              ajtem.name = v.video_details.title!;
              mes.channel.send(`zahraju \`${ajtem.name}\``);
            });
          } else {
            soundcloud(txt).then(s => {
              ajtem.name = s.name;
              mes.channel.send(`zahraju \`${s.name}\``);
            });
          }
        } else {
          const msg = mes.channel.send("hledam");
          const hledani = await search(txt, { limit: 1 });
          ajtem.url = hledani[0].url;
          ajtem.name = hledani[0].title!;
          msg.then(m => void m.edit(`zahraju \`${ajtem.name}\``));
        }

        const kju = (kjus[mes.guildId!] ??= []);
        kju.push(ajtem);

        if (kju.length != 1) return;

        configureTimeAnouncment(mes.guildId!, false);

        const { conn } = await joinVoice(kanel);
        const player = getCurrentPlayer(mes.guildId!)!;

        zahrat(conn, ajtem.url);

        player.on(AudioPlayerStatus.Idle, () => {
          kju.splice(0, 1);
          if (kju.length == 0) {
            delete kjus[mes.guildId!];
            configureTimeAnouncment(mes.guildId!, true);
            return;
          }
          zahrat(conn, kju[0].url);
        });
      }
    },

    queue: {
      als: ["q", "kju"],
      DMUnsafe: true,
      run: mes => {
        const kju = kjus[mes.guildId!];
        if (!kju) return "nehraje nic";

        return `tuto je kurentni repertoar:\n${kju.reduce((acc, cur, i) => {
          return `${acc}\n${i ? `${i}. ${cur.name}` : `-- *${cur.name}* --`}`;
        }, "")}`;
      }
    },

    skip: {
      DMUnsafe: true,
      run: mes => {
        const kju = kjus[mes.guildId!];
        if (!kju) return "nic nehraje";

        const player = getCurrentPlayer(mes.guildId!);
        player.stop();
        mes.react(emouty.d3k);
      }
    },

    remove: {
      DMUnsafe: true,
      als: ["odebrat"],
      run: (mes, arg) => {
        const numero = Number(arg);
        if (isNaN(numero)) return "cokundo?";

        const kju = kjus[mes.guildId!];
        if (!kju) return "nic nehraje";

        if (kju.length <= numero) return "tolik toho nehrae";

        kju.splice(numero, 1);
        mes.react(emouty.d3k);
      }
    },

    seek: {
      DMUnsafe: true,
      run: (mes, arg) => {
        const numero = Number(arg);
        if (isNaN(numero)) return "huh?";

        const kju = kjus[mes.guildId!];
        if (!kju) return "nehraje nic";

        const conn = getCurrentPlayer(mes.guildId!)!.playable[0];
        zahrat(conn, kju[0].url, numero);
        mes.react(emouty.d3k);
      }
    }
  }
};

module.exports = exp;
