'use strict'

// const sentiment = require('../helpers/sentiment')
const config = require('../config')
const twit = require('twit')
const T = new twit(config.twitter)
const db = require('../helpers/db')

const unified = require('unified')
const sentiment = require('retext-sentiment')
const english = require('retext-english')
// const inspect = require('unist-util-inspect')

const hashtagStream2 = T.stream('statuses/filter', {
  track: config.queryString
})

const sentimentBot = () => {
  hashtagStream2.on('tweet', tweet => {
    console.log(`Sentiment Bot Running`)
    
    const processor = unified()
      .use(english)
      .use(sentiment)

    const tree = processor.parse(tweet.text)

    processor.run(tree, tweet.text)

    console.log('====================')
    console.log(tree.data.polarity)
    console.log(tree.data.valence)
    console.log('====================')

    // console.log(inspect(tree))

    // // Setup the http call
    // const httpCall = sentiment.init()

    // // Don't do anything if it's the bot tweet
    // if (tweet.user.screen_name == '_100DaysOfCode') return

    // httpCall.send('txt=' + tweet.text).end(result => {
    //   console.log('====================')
    //   console.log(result.status)
    //   console.log('====================')
    //   console.log(result.headers)
    //   console.log('====================')
    //   console.log(result.body)
    //   console.log('====================')
    //   // try
    //   try {
    //     let sentim = result.body.result.sentiment
    //     let confidence = parseFloat(result.body.result.confidence)
    //   } catch (err) {
    //     // derp out
    //     console.log(err)
    //     return
    //   }

    //   // if sentiment is Negative and the confidence is above 75%
    //   if (sentim == 'Negative' && confidence >= 75) {
    //     // get a random quote
    //     let phrase = sentiment.randomQuote()
    //     let screen_name = tweet.user.screen_name

    //     // Check key isn't in db already, key being the screen_name
    //     db.get(screen_name, (err, value) => {
    //       if (typeof value !== 'undefined') {
    //         console.log('ALREADY IN DB USER ', screen_name)
    //       } else {
    //         // Put a user name and that they have been encouraged
    //         db.put(screen_name, 'encourage', err => {
    //           // some kind of I/O error
    //           if (err) return console.log('Ooops!', err)

    //           console.log('LOGGED USER: ', screen_name)

    //           // tweet a random encouragement phrase
    //           tweetNow('@' + screen_name + ' ' + phrase)
    //         })
    //       }
    //     })
    //   }
    // })
  })
}

function tweetNow(text) {
  let tweet = { status: text }

  T.post('statuses/update', tweet, (err, data, response) => {
    if (err) {
      console.log('ERROR: ', err)
    }
    console.log('SUCCESS: Replied to Follower')
  })
}

module.exports = sentimentBot

@@ -761,24 +761,25 @@
 ============
+%7D
 ')%0A    conso
@@ -777,32 +777,44 @@
     console.log(
+%60POLARITY=$%7B
 tree.data.polari
@@ -815,16 +815,18 @@
 polarity
+%7D%60
 )%0A    co
@@ -835,16 +835,27 @@
 ole.log(
+%60VALENCE=$%7B
 tree.dat
@@ -859,24 +859,26 @@
 data.valence
+%7D%60
 )%0A    consol
@@ -916,117 +916,182 @@
     
-// console.log(inspect(tree))%0A%0A    // // Setup the http call%0A    // const httpCall = sentiment.init()%0A%0A    //
+try %7B%0A      let polarity = tree.data.polarity%0A      let valence = tree.data.valence%0A    %7D catch (err) %7B%0A      // derp out%0A      console.log(err)%0A      return      %0A    %7D%0A%0A   
  // 
@@ -1133,19 +1133,16 @@
 weet%0A   
- //
  if (twe

assign polarity and valence