const client = require('./api/twitter-api');
const logger = require('./utils/logger');
const countSyllables = require('./utils/lang/countSyllables');

const stream = client.stream('user');
let screen_name = null;


// Verify Authentication
client.get('account/verify_credentials',{})
    .then((res) => {
        screen_name = res.screen_name;
        logger.info(`Successfully Authenticated as "${screen_name}"`);
        main();
    }, err => {
        logger.error('Authentication failed: Invalid credentials.', {error: err});
        process.exit(1);
    });


function main() {
    // Stream Tweets
    stream.on('data', res => {
        let reply_to_name = res.in_reply_to_screen_name;
        let reply_to_status = res.in_reply_to_status_id_str;

        if (reply_to_name === screen_name &&  reply_to_status == null) {
            reply(reply_to_status, res.text);        
        }        
    });
}

function reply(status_id, text) {
    let syllables = getSyllables(text);
    let message = `Syllable Count: ${syllables.count}\nPhonemes: ${syllables.phonemes}`;

    client.post('statuses/update', {status: message, in_reply_to_status_id: status_id})
    .then(() => {
        logger.info('Replied to a syllable request');
    }, err => {
        logger.error('Error replying to a syllable request', {error: err});
    });
}

function getSyllables(text) {
    let message = text.replace(/@S\+/img, '');
    message = message.replace(/\s+/img, ' ');
    return countSyllables(message);
}




@@ -557,17 +557,16 @@
    %7D);%0A%0A
-%0A
 function
@@ -743,14 +743,35 @@
 s_id
-_str;%0A
+;%0A        console.log(res);
 %0A   
@@ -811,17 +811,16 @@
 _name &&
- 
  reply_t
@@ -859,29 +859,24 @@
 reply(re
-ply_to_status
+s.id_str
 , res.te
@@ -1089,103 +1089,159 @@
     
-client.post('statuses/update', %7Bstatus: message, in_reply_to_status_id: status_id%7D)%0A
+let params = %7B%0A        in_reply_to_status_id: status_id, %0A        status: message%0A    %7D;%0A%0A    client.post('statuses/update', params)%0A  
     
+  
 .then((
+res
 ) =%3E
@@ -1243,32 +1243,36 @@
 s) =%3E %7B%0A        
+  
+  
 logger.info('Rep
@@ -1272,9 +1272,22 @@
 fo('
-R
+Successfully r
 epli
@@ -1311,19 +1311,42 @@
 request'
-);%0A
+, %7Btweet: res.text%7D);%0A    
     %7D, e
@@ -1353,32 +1353,36 @@
 rr =%3E %7B%0A        
+  
+  
 logger.error('Er
@@ -1429,24 +1429,28 @@
 ror: err%7D);%0A
+    
     %7D);%0A%7D%0A%0Af
@@ -1478,27 +1478,27 @@
 text) %7B%0A    
-let
+var
  message = t
@@ -1515,10 +1515,10 @@
 e(/@
-S
 %5C
+S
 +/im
@@ -1604,16 +1604,23 @@
 (message
+.trim()
 );%0A%7D%0A%0A%0A%0A

Fix syllable reply errors