module.exports = (lib) => {
  lib.trigger.add({
    name: 'level',
    call: /^\!level(?: +\@.*)?/i,
    level: 0,
    active: 1,
    func: (e, userlevel) => {
      var args = e.message.split(' ');

      if(Number.isInteger(parseInt(args[2]))) {
        if(userlevel >= 100) {
          var name = args[1].replace('@','');
          var level = (args[2]>=100)?100:args[2];
			level = (level<0)?0:level;
          
          if(name.toLowerCase() != lib.bot.me.nick.toLowerCase()) {
            if( name in e.channel.names ) {
              lib.bot.whois(name.trim(), e.network, (err, data) => {
                if(!err) {
                  if(data.account !== null) {
                    var userstring = (data.nick === data.account)?data.nick:(data.nick+' ('+data.account+')');
                    if(data.account in lib.admins) {
                      if(level <= 0) {
                        lib.sql.query('delete from `f0ck`.`user` where `id` = ? limit 1', [lib.admins[data.account].id], () => {
                          lib.loadUser();
                          applyModes(lib, data.channels[e.channel], level, e.channel, data.nick);
                          e.reply('deleted user '+userstring);
                        });
                      }
                      else {
                        lib.sql.query('update `f0ck`.`user` set `level` = ? where `id` = ?', [level, lib.admins[data.account].id], () => {
                          lib.loadUser();
                          applyModes(lib, data.channels[e.channel], level, e.channel, data.nick);
                          e.reply('changed user '+userstring+' to level '+level);
                        });
                      }
                    }
                    else {
                      lib.sql.query('insert into `f0ck`.`user` (`account`,`level`,`network`) values (?,?,?)', [data.account, level, e.network], () => {
                        lib.loadUser();
                        applyModes(lib, data.channels[e.channel], level, e.channel, data.nick);
                        e.reply('added new user '+userstring+' with level '+level);
                      });
                    }
                  }
                  else
                    e.reply('user '+data.nick+' isn\'t registered');
                }
              });
            }
            else
              e.reply('user '+name+' not found');
          }
          else
            e.reply('f0ck you hard');
        }
        else
          e.reply('no permission, min level 100 required');
      }
      else {
        var user = (!(user = e.message.match(/^\!level(?: +@(.*))?/i)[1]))?e.user.getNick():user.trim();
        lib.getUserlevel(user, e.network, e.channel.getName(), cb => {
          e.reply("level from "+user+": "+cb.level+" (Channel: "+cb.channel+" DB: "+cb.db+")");
        });
      }
    },
    desc: 'get Userlevel'
  });
};

var applyModes = (lib, h, q, channel, nick) => {
  h = lib.modesToString(h);
  q = lib.levelToModes(q);
  lib.groupUserModes(lib.userModeDiff(h, q)).forEach(entry => lib.bot.mode(channel, entry + (' ' + nick).repeat(entry.length - 1)));
};