// Komandy, co maj víc než "pár" řádek, a který si nezaslouží vlastní soubor

import { ChannelType, Message, MessageReaction } from "discord.js";
import { emouty } from "../utils/emotes";
import { Modul, SRecord } from "../utils/types";
import { formatter, ping, messageReply, sendDM, sendWithoutReply } from "../utils/utils";

const exp: Modul = {
  more_komandy: {

    status: (mes, koho) => {
      if (!ping.test(koho)) return "tak si kokot ti kokote";

      const uzivatel = mes.mentions.members!.first()!;
      const embed = {
        title: `Informace o statusech pro ${uzivatel.displayName}:`,
        color: 431075,
        description: ""
      };
      const presence = uzivatel.presence?.clientStatus;
      if (!(presence && Object.keys(presence).length)) {
        embed.description = "*Všude je offline*";
      } else {
        const zarizeni: SRecord<string> = { desktop: "Počítač", mobile: "Mobil", web: "Web" };
        const statusy = { online: "🟢", idle: "🟡", dnd: "🔴" };
        const uStatusy: string[] = [];
        const klice = Object.keys(presence) as ("web" | "mobile" | "desktop")[];
        klice.forEach(status => {
          uStatusy.push(`${zarizeni[status]}: ${statusy[presence[status]!]}`);
        });
        embed.description = uStatusy.join("\n");
      }
      return { embeds: [embed] };
    },

    zareaguj: {
      als: ["react"],
      arg: "emout/emouty",
      run: async (mes, arg) => {
        const emouty = arg.split(/\s/);

        let naCo: Message;
        if (mes.reference) {
          naCo = await mes.channel.messages.fetch(mes.reference.messageId!);
        } else {
          const msgs = [...mes.channel.messages.cache.values()];
          if (msgs.length < 2) {
            naCo = (await mes.channel.messages.fetch({ limit: 2 })).at(1)!;
          }
          else naCo = msgs[msgs.length - 2];
        }

        const reakce: Promise<void | MessageReaction>[] = [];
        if (mes.channel.type != ChannelType.DM) mes.delete();
        emouty.forEach(emout => {
          reakce.push(naCo.react(emout)
            .catch(() => { }));
        });

        if (!(await Promise.all(reakce)).every(r => !!r != false)) return "retarde";
      }
    },

    odpocitej: mes => {
      const randomshit = (mes: Message, argument: string[]) => {
        mes.edit(argument[0]);
        argument.splice(0, 1);
        if (argument.length) setTimeout(() => randomshit(mes, argument), 1000);
      };
      messageReply(mes, ":stop_button:").then(mes => randomshit(mes, [":five:", ":four:", ":three:", ":two:", ":one:", ":ok:"]));
    },

    pocasi: () => {
      const embed = {
        title: "Počasí",
        image: { url: "attachment://pocasi.png" }
      };
      return { embeds: [embed], files: ["https://util.deadfish.cz/morepocasi/v/49.4348358/12.8147250/pocasi.png"] };
    },

    pockej: {
      als: ["cekej"],
      arg: "počet sekund",
      run: async (mes, arg) => {
        if (mes.author.id == mes.client.user?.id) return emouty.sjeta;
        let cas: number;
        if (arg.length) {
          cas = Number(arg);
          if (isNaN(cas)) return "cos to tam napsal ty kkt";
        } else {
          cas = Math.floor(Math.random() * 10740 + 60);
        }

        try {
          await sendWithoutReply(await mes.author.createDM(), `test ${emouty.d3k}`);
        } catch {
          return "ja ti ale napsat nemuzu ti kundo";
        }

        setTimeout(async () => {
          for (let i = 0; i < 7; i++) {
            await sendDM(mes.author, "uz?");
          }
        }, cas * 1000);

        const datum = new Date();
        datum.setSeconds(datum.getSeconds() + cas);
        return `doufam ze v ${formatter(datum)} to uz bude`;
      }
    }
  }
};

module.exports = exp;
