module.exports = (lib) => {
  lib.trigger.add({
    name: 'trigger',
    call: /^!trigger/i,
    level: 100,
    active: 1,
    func: (e) => {
      var args = e.message.split(' ');
      args.shift();
      if(!lib.cfg.trigger[e.network+e.channel]) // generate config for channel
        lib.cfg.trigger[e.network+e.channel] = lib.genStdTrigger();
      else // check existing config
        lib.cfg.trigger[e.network+e.channel] = extend(lib.genStdTrigger(), lib.cfg.trigger[e.network+e.channel]);
      
      if(args.length > 0) {
        switch(args[0]) {
          case "reset":
            lib.cfg.trigger[e.network+e.channel] = lib.genStdTrigger();
            lib.config.write('trigger', e.network+e.channel, lib.cfg.trigger[e.network+e.channel], 'json', cbwc => {
              e.reply("trigger resetted");
            });
          break;
          default:
            for(let i = 0; i < args.length; i++) {
              let trigger = args[i].toLowerCase();
              if(triggerexists(trigger)) {
                lib.cfg.trigger[e.network+e.channel][trigger] = !lib.cfg.trigger[e.network+e.channel][trigger];
                lib.config.write('trigger', e.network+e.channel, lib.cfg.trigger[e.network+e.channel], 'json', cbwc => {
                  e.reply(!cbwc?"trigger "+ trigger +" in channel "+e.channel.getName() + lib.bot.format.get(lib.cfg.trigger[e.network+e.channel][trigger]?'green':'red')+" "+(lib.cfg.trigger[e.network+e.channel][trigger]?'activated':'deactivated'):cbwc);
                });
              }
              else
                e.reply("trigger "+trigger+" not found D:");
            }
          break;
        }
      }
      else {
        var output = "";
        e.reply("Trigger in "+e.channel.getName()+" (green: activated, red: deactivated):");
        for(var key in lib.cfg.trigger[e.network+e.channel])
          output += lib.bot.format.get(lib.cfg.trigger[e.network+e.channel][key]?'green':'red')+" "+key;
        e.reply(output);
      }
    },
    desc: 'muh'
  });
  
  var extend = (obj, src) => {
    Object.keys(src).forEach(key => obj[key] = src[key]);
    return obj;
  };
  var triggerexists = (search) => {
    var out = false;
    lib.trigger.trigger.forEach(entry => {
      if(entry.name === search) {
        out = true;
        return;
      }
    });
    return out;
  };
};