var allowedModes = [
  'q','a','o','h','v'
];

module.exports = (lib) => {
  lib.trigger.add({
    name: 'umode',
    call: /^\!umode .*/i,
    level: 100,
    active: 0,
    func: (e) => {
      var tmp = e.message.trim().split(' ');
      tmp.shift();
      var user = tmp[tmp.length - 1];
      if(!(user in e.channel.names))
        user = e.user.getNick();
      else
        tmp.pop();
      var modes;
      if(user.toLowerCase() != lib.bot.me.nick.toLowerCase()) {
        var modes = processModes(tmp.join(''));
        if(modes.length) {
          if(checkModes(modes, allowedModes)) {
            modesToApply = lib.groupUserModes(modes);
            modesToApply.forEach(modeStr => lib.bot.mode(e.channel, modeStr + (' ' + user).repeat(modeStr.length - 1), e.network));
          }
          else
            e.reply('Error! Allowed modes are: ' + allowedModes.join(','));
        }
        else
          e.reply('No modes specified!');
      }
      else
        e.reply('f0ck you hard');
    },
    desc: 'change Usermodes'
  });
};

var processModes = modes => {
  var prefix = '+', modeArr = [];
  modes.split('').forEach(token => {
    if(token === '-' || token === '+') {prefix = token; return;}
    modeArr.push(prefix + token);
  });
  return modeArr;
};

var checkModes = (modes, allowedModes) => {
  if([... new Set(modes)].some(mode => {
    return (allowedModes.indexOf(mode[1]) != -1)?false:true;
  })) return false;
  return true;
};