const { inspect } = require("util");
// edit csettings
// no new value check right now
exports.run = async (client, message, [action, category, key, value], level) => { // eslint-disable-line no-unused-vars
  // Retrieve current settings
  const settings = message.settings;
  const container = settings[category];
  if (action === "view") {
    const category = ["functionEnabled", "commandEnabled"];
    category.forEach(c =>{
      message.channel.send(client.embed().setDescription(`${c}:\n` + inspect(message.settings[c])));
    });
  }
  else if (action === "get") {
    if (!category && category !== "functionEnabled" && category !== "commandEnabled") return message.reply("please specify a category: functionEnabled, commandEnabled");
    if (!key) return message.reply("please specify a key to view");
    if (container[key] !== true && container[key] !== false) return message.reply("this key does not exist in the settings or the choosed category");
    message.channel.send(client.embed().setDescription(`The value of ${key} is currently ${container[key]}`));
  }
  else if (action === "edit") {
    if (!category && category !== "functionEnabled" && category !== "commandEnabled") return message.reply("please specify a category: functionEnabled, commandEnabled");
    if (!key) return message.reply("please specify a key to edit");
    if (container[key] === undefined) return message.reply("this key does not exist in the settings or the choosed category");
    if (value !== "true" && value !== "false") return message.reply("please specify a correct value: true, false");
    // create new value
    if (value === "true") container[key] = true;
    else container[key] = false;
    // return const to global and save
    settings[category] = container;
    client.settings.set(message.guild.id, settings);
    message.channel.send(client.embed().setDescription(`${key} successfully edited to ${value}`));
  }
  else if (action === "add") {
    if (!category && category !== "functionEnabled" && category !== "commandEnabled") return message.reply("please specify a category: functionEnabled, commandEnabled");
    if (!key) return message.reply("please specify a key to add");
    if (container[key]) return message.reply("this key already exists in the settings");
    if (value !== "true" && value !== "false") return message.reply("please specify a value");
    // value needs join
    if (value === "true") container[key] = true;
    else container[key] = false;
    settings[category] = container;
    client.settings.set(message.guild.id, settings);
    message.channel.send(client.embed().setDescription(`${key} successfully added with the value of ${value.join(" ")}`));
  }
  else if (action === "del") {
    if (!category && category !== "functionEnabled" && category !== "commandEnabled") return message.reply("please specify a category: functionEnabled, commandEnabled");
    if (!key) return message.reply("Please specify a key to delete.");
    if (!container[key]) return message.reply("This key does not exist in the settings");
    const response = await client.awaitReply(message, `Are you sure you want to permanently delete ${key}? This **CANNOT** be undone.\"yes\" \\ \"no\"`, 300, true, ["yes", "no"]);
    if (response === "yes") {
      // We delete the `key` here.
      delete container[key];
      settings[category] = container;
      client.settings.set(message.guild.id, settings);
      message.channel.send(client.embed().setDescription(`${key} was successfully deleted.`));
    }
    else message.reply("action cancelled.");
  }
  else message.reply("please specify a correct action: view, get, edit, add, del");
};

exports.conf = {
  admin: true,
  guildOnly: true,
  aliases: ["setting"],
  permLevel: 10
};

exports.help = {
  name: "set",
  category: "-1-Owner",
  description: "View or change roles and channels.",
  usage: "set <view/get/edit> <category> <key> <value>"
};
