var Q = require('q');

var Twit = require('twit');

var models = require('../models');
var utils = require('../helpers/utils');
var config = require('../config');
var match = require('../helpers/match')
var tweetBuilder = require('./tweetBuilder');

var T = new Twit(config.twitter);

function removeHashTag(text) {
  return text.replace(/#\w+/i, '');
}

function removeUser(text) {
  return text.replace(/@\w/i, '')
}

function parseFieldsFromTweet(tweet, fields) {
  var parsed = {};
  var mapFields = {
    id: 'id',
    name: 'user.name',
    screen_name: 'user.screen_name',
    text: 'text',
    hashtags: 'entities.hashtags'
  }

  fields.forEach(function(k) {
    if (k in mapFields) {
      parsed[k] = utils.getField(tweet, mapFields[k], 'none '+k);
    }
  })
  
  return parsed;
}

function getLocation(id) {
  console.log(getLocation);
  return Q.promise(function (resolve, reject) {
    T.get('statuses/show/:id', {id: id}, function (err, data, response) {
      console.log(err, id);
      console.log(data);
    });
  });
}

function findScheduleAndPost(msg, screen_name) {

  match.parseQueryFromTweet(msg)
    .then(models.schedule.getOne)
    .then(function(data) { 
      if(!data.schedule) {
        data.schedule = 'No schedule found';
      };
      var posts = tweetBuilder.createPosts(screen_name, data);
      posts.forEach(function(post) {
        postTweet(post);
      })
    });
}



function handleNewTweet(tweet) {

  
  var tweetFields = parseFieldsFromTweet(tweet,
                     ['id', 'name', 'screen_name', 'text', 'hashtags']);
  var msg = tweetFields.text;
  if (msg.indexOf('boterino123') < 0 && msg.indexOf(config.hashtag) < 0) {
    return;
  }
  console.log('------------------on tweet --------------------------');
  var msg = removeHashTag(msg);
  msg = removeUser(msg);

  var msgClean = utils.cleanSpaces(msg);
  console.log(tweetFields);
  console.log('msgClean', msgClean);
  if ( msgClean === 'help' || !msgClean.length ) {
    var post = tweetBuilder.createHelpMsg(tweetFields.screen_name);
    postTweet(post);
    return;
  }

  findScheduleAndPost(msg, tweetFields.screen_name);
    
}

function listenStream(hashtag) {
  T.stream('statuses/filter', { track: hashtag});
  T.stream('user').on('tweet',handleNewTweet);;
  
}

function postTweet(message) {
  T.post('statuses/update', {status: message},
    function(err, data, response) {
      if (err) return console.log(err);
      console.log('posted', message);
    }
  )
}

// var mongoose = require('mongoose');
// var config = require('../config');

// mongoose.connect(config.mongoConnection);

// findScheduleAndPost('larcomar Terminator ', 'ggwp')

module.exports.listenStream = listenStream;
@@ -1405,16 +1405,231 @@
  %7D);%0A%7D%0A%0A
+function follow(id, cb) %7B%0A  T.post('friendships/create', %7Buser_id: id, follow: true%7D,%0A         function(err, data, response) %7B%0A            if (err) return cb('follow ' + err);%0A            cb(null);%0A          %7D%0A  )%0A%7D
 %0A%0Afuncti
@@ -2919,8 +2919,81 @@
 nStream;
+%0A%0Afollow(615385295803056100,function(err) %7B%0A  console.log('ggwp', err)%0A%7D)

test follow