import { selfGrantableRolesCmd } from "../types";
import { asSingleLine, trimLines } from "src/utils";
import { getApplyingEntries } from "../util/getApplyingEntries";

export const RoleHelpCmd = selfGrantableRolesCmd({
  trigger: ["role help", "role"],
  permission: null,

  async run({ message: msg, pluginData }) {
    const applyingEntries = getApplyingEntries(pluginData, msg);
    if (applyingEntries.length === 0) return;

    const allPrimaryAliases = [];
    for (const entry of applyingEntries) {
      for (const aliases of Object.values(entry.roles)) {
        if (aliases[0]) {
          allPrimaryAliases.push(aliases[0]);
        }
      }
    }

    const prefix = pluginData.guildConfig.prefix;
    const [firstRole, secondRole] = allPrimaryAliases;

    const help1 = asSingleLine(`
      To give yourself a role, type e.g. \`${prefix}role ${firstRole}\` where **${firstRole}** is the role you want.
      ${secondRole ? `You can also add multiple roles at once, e.g. \`${prefix}role ${firstRole} ${secondRole}\`` : ""}
    `);

    const help2 = asSingleLine(`
      To remove a role, type \`${prefix}role remove ${firstRole}\`,
      again replacing **${firstRole}** with the role you want to remove.
    `);

    const helpMessage = trimLines(`
      ${help1}

      ${help2}

      **Roles available to you:**
      ${allPrimaryAliases.join(", ")}
    `);

    const helpEmbed = {
      title: "How to get roles",
      description: helpMessage,
      color: parseInt("42bff4", 16),
    };

    msg.channel.createMessage({ embed: helpEmbed });
  },
});
