var RtmClient = require('@slack/client').RtmClient;
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
var CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
var doge = require('dogefy');

module.exports = function(key){
  if(!key) throw new Error('You need to specify a Slack token');

  var rtm = new RtmClient(key);

  rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, function() {
    var team = rtm.dataStore.getTeamById(rtm.activeTeamId);
    var user = rtm.dataStore.getUserById(rtm.activeUserId);
    console.log('Connected to ' + team.name + ' as ' + user.name);
  });

  rtm.on(RTM_EVENTS.MESSAGE, function(message){
    var channel = rtm.dataStore.getChannelGroupOrDMById(message.channel);

    if(message.type === 'message' && message.text && (channel.is_im || isDirect(rtm.activeUserId, message.text))){
      var cleanedMessage = message.text.replace(getTag(rtm.activeUserId), '').replace(':', '').substring(0, 2000).trim();

      if(cleanedMessage){
        console.log('Dogefied: ' + cleanedMessage);
        rtm.sendMessage('``` \n' + doge(cleanedMessage) + '\n ```', message.channel);
      }
    }
  });

  rtm.start();
};

function getTag(id){
  return '<@' + id + '>';
};

function isDirect(userId, messageText){
  return messageText && messageText.indexOf(userId) > -1;
};

@@ -753,16 +753,22 @@
 .text &&
+%0A     
  (channe
@@ -819,16 +819,21 @@
 e.text))
+%0A    
 )%7B%0A     
@@ -866,16 +866,25 @@
 age.text
+%0A        
 .replace
@@ -913,16 +913,25 @@
 Id), '')
+%0A        
 .replace
@@ -939,16 +939,25 @@
 ':', '')
+%0A        
 .substri
@@ -967,16 +967,25 @@
 0, 2000)
+%0A        
 .trim();

reformat long line