const { ButtonBuilder } = require('discord.js');
const words = require('../../utils/Words.json');

module.exports = {
  name: "kelime-yarÄ±ÅŸmasÄ±",
  description: "Rastgele verilen kelimeyi ilk yazan kazanÄ±r.",
  usage: "kelime-yarÄ±ÅŸmasÄ± <@KullanÄ±cÄ±#1234>",
  aliases: ['yazÄ±-yarÄ±ÅŸÄ±', 'ilk-yazan-kazanÄ±r', 'kelimeyarÄ±ÅŸmasÄ±', 'yazan-kazanÄ±r', 'kelimeyarÄ±ÅŸÄ±', 'kelime-yarÄ±ÅŸÄ±', 'ky', "yazankazanÄ±r", "yazan-kazanÄ±r"],
  category: "Games",
  memberPermissions: [],
  botPermissions: ["SendMessages", "EmbedLinks"],
  nsfw: false,
  cooldown: false,
  ownerOnly: false,

  async execute(client, message, args, data) {

    let opponent = message.mentions.users.first();

    if (!opponent) return message.channel.send({
      embeds: [
        {
          color: client.settings.embedColors.red,
          title: '**Â»** Oynamak Ä°stediÄŸin KullanÄ±cÄ±yÄ± Etiketlemelisin!',
          description: `**â€¢** Ã–rnek kullanÄ±m: \`${data.prefix}kelime-yarÄ±ÅŸmasÄ± <@Ãœye>\``
        }
      ]
    });

    if (opponent.id == client.user.id) return message.channel.send({
      embeds: [
        {
          color: client.settings.embedColors.red,
          title: '**Â»** Benimle OynayamazsÄ±n!',
          description: `**â€¢** Ã‡Ã¼nkÃ¼ henÃ¼z benim seviyemde deÄŸilsin ğŸ˜�`
        }
      ]
    });

    if (opponent.bot) return message.channel.send({
      embeds: [
        {
          color: client.settings.embedColors.red,
          title: '**Â»** Botlarla OynayamazsÄ±n!',
          description: `**â€¢** Oynayabileceklerini sandÄ±n mÄ± ki zaten?`
        }
      ]
    });

    if (opponent.id === message.author.id) return message.channel.send({
      embeds: [
        {
          color: client.settings.embedColors.red,
          title: '**Â»** Kendinle OynayamazsÄ±n!',
          description: `**â€¢** Sen kendinle oynamaya Ã§alÄ±ÅŸacak kadar yalnÄ±z mÄ±ydÄ±n?`
        }
      ]
    });

    if (client.gamesPlaying.has(message.channel.id)) return message.channel.send({
      embeds: [
        {
          color: client.settings.embedColors.red,
          title: '**Â»** Bu Kanalda Zaten Devam Eden Bir Oyun Var!',
          description: `**â€¢** FarklÄ± bir kanalda dene veya sÄ±ranÄ± bekle.`
        }
      ]
    });

    client.gamesPlaying.set(message.channel.id, this.name);

    const { buttonConfirmation } = require("../../modules/Functions");
    const buttonConfirmationResult = await buttonConfirmation(
      message,
      [
        {
          color: client.settings.embedColors.default,
          title: `**Â»** ${opponent.username}, kelime yarÄ±ÅŸmasÄ± isteÄŸini kabul ediyor musun?`,
          description: `**â€¢** ButonlarÄ± kullanarak cevaplayabilirsin.`
        }
      ],
      opponent.id
    );
    if (!buttonConfirmationResult.status) {

      client.gamesPlaying.delete(message.channel.id);

      return buttonConfirmationResult.reply.edit({
        embeds: [
          {
            color: client.settings.embedColors.red,
            title: '**Â»** Meydan Okuma Kabul Edilmedi!',
            description: `**â€¢** ÃœzgÃ¼nÃ¼m, istersen [destek sunucumuza](https://discord.gg/VppTU9h) katÄ±lÄ±p orada birilerini bulabilirsin.`
          }
        ],
        components: []
      }).catch(() => { });

    }

    await buttonConfirmationResult.reply.edit({
      embeds: [
        {
          color: client.settings.embedColors.default,
          title: '**Â»** HazÄ±rlanÄ±n!',
          description: `**â€¢** 3 saniye sonra kelime geliyor!`
        }
      ],
      components: []
    });

    try {

      const word = words[Math.floor(Math.random() * words.length)];

      await setTimeout(() => {
        message.channel.send({
          embeds: [
            {
              color: client.settings.embedColors.default,
              title: `**Â»** Hemen \`${word.toLocaleLowerCase('tr-TR')}\` kelimesini yaz!`,
            }
          ]
        });
      }, 3000);

      const filter = res => [opponent.id, message.author.id].includes(res.author.id) && res.content.toLocaleLowerCase('tr-TR') === word;
      const winner = await message.channel.awaitMessages({ filter, max: 1, time: 10000 });

      client.gamesPlaying.delete(message.channel.id);
      if (!winner.size)
        return message.channel.send({
          embeds: [
            {
              color: client.settings.embedColors.red,
              title: '**Â»** Kimse KazanamadÄ±!',
              description: `**â€¢** Dostluk kazanmÄ±ÅŸ olabilir. Belki o da kaybetmiÅŸtir.`
            }
          ]
        });

      return message.channel.send({
        embeds: [
          {
            color: client.settings.embedColors.green,
            title: `**Â»** Tebrikler ${winner.first().author.username}! KazandÄ±n! ğŸ�†`,
            //description: `**â€¢** FarklÄ± bir kanalda dene veya sÄ±ranÄ± bekle.`
          }
        ]
      });

    } catch (err) {
      client.logger.error(err);
      client.gamesPlaying.delete(message.channel.id);
    }

  }
};