const permissions = require("../../utils/Permissions.json");

module.exports = async (client, member, memberCounter, guildData, event) => {

  const channel = member.guild.channels.cache.get(memberCounter.channel);

  try {

    if (!channel) {

      client.logger.log(`SayaÃ§ kanalÄ± bulunamadÄ±, sunucudaki sayaÃ§ sÄ±fÄ±rlanÄ±yor... â€¢ ${member.guild.name} (${member.guild.id})`);
      guildData.memberCounter = undefined;
      await guildData.save();

      return member.guild.channels.cache.get(memberCounter.setupChannel)?.send({
        embeds: [
          {
            color: client.settings.embedColors.red,
            title: '**Â»** SayaÃ§ KanalÄ± BulunamadÄ±ÄŸÄ± Ä°Ã§in SayaÃ§ SÄ±fÄ±rlandÄ±!',
            description: `**â€¢** Tekrar ayarlamak iÃ§in \`/sayaÃ§ Ayarla\` komutunu kullanabilirsiniz.`
          }
        ]
      });

    }

    let clientPerms = [];
    ["ViewChannel", "SendMessages", "EmbedLinks"].forEach((perm) => {
      if (!channel.permissionsFor(member.guild.members.me).has(perm)) {
        clientPerms.push(permissions[perm]);
      }
    });

    if (clientPerms.length > 0) {

      client.logger.log(`SayaÃ§ kanalÄ±nda bir/birkaÃ§ yetkim bulunmadÄ±ÄŸÄ± iÃ§in sayaÃ§ sistemi sÄ±fÄ±rlanÄ±yor... â€¢ ${member.guild.name} (${member.guild.id})`);
      guildData.memberCounter = undefined;
      await guildData.save();

      return member.guild.channels.cache.get(memberCounter.setupChannel)?.send({
        embeds: [{
          color: client.settings.embedColors.red,
          author: {
            name: `SayaÃ§ Sistemini Ã‡alÄ±ÅŸtÄ±rabilmem Ä°Ã§in Gereken Ä°zinlere Sahip DeÄŸilim!`,
            icon_url: member.guild.iconURL(),
          },
          description: `**Â»** ${channel} kanalÄ±nda yeterli yetkiye sahip olmadÄ±ÄŸÄ±m iÃ§in sayaÃ§ sistemini sÄ±fÄ±rladÄ±m.`,
          fields: [
            {
              name: '**Â»** Ä°htiyacÄ±m Olan Ä°zinler;',
              value: "**â€¢** " + clientPerms.map((p) => `${p}`).join("\n**â€¢** "),
            },
          ]
        }]
      });

    }

    let joinEmbed = {
      color: event === "guildMemberAdd" ? client.settings.embedColors.green : client.settings.embedColors.red,
      author: {
        name: `${member.user.tag} ${event === "guildMemberAdd" ? "KatÄ±ldÄ±" : "AyrÄ±ldÄ±"}!`,
        icon_url: member.user.displayAvatarURL(),
      },
      description: `ğŸ“¥ â€¢ **${memberCounter.target}** Ã¼ye olmamÄ±za **${memberCounter.target - member.guild.memberCount}** Ã¼ye kaldÄ±.`,
    };

    if (member.user.bot) joinEmbed.author.name += ` (Bot ğŸ¤–)`;

    channel.send({
      embeds: [joinEmbed]
    });

    //SayaÃ§ hedefine ulaÅŸÄ±lÄ±rsa
    if (memberCounter.target <= member.guild.memberCount) {

      channel.send({
        embeds: [{
          color: client.settings.embedColors.default,
          title: `**Â»** Tebrikler ${member.guild.name}!`,
          description:
            `**â€¢** BaÅŸarÄ±yla **${memberCounter.target}** kullanÄ±cÄ±ya ulaÅŸtÄ±k!\n` +
            `**â€¢** SayaÃ§ hedefini otomatik olarak ikiye katladÄ±m!`,
        }]
      });

      guildData.memberCounter.target = guildData.memberCounter.target * 2;
      await guildData.save();

    }

  } catch (err) { client.logger.error(err); };
};