'use strict';

var _ = require('lodash');
var moment = require('moment');
var async = require('async');
var Twitter = require('twitter');
var cache = require('memory-cache');

/* jshint camelcase: false */
var config = {
  consumer_key: 'AuCTJJkwLGemXxxi2IPag',
  consumer_secret: '4IIpSfjPbvUNEN3scPogNVuC5Wbseabn2T97kRR1tms',
  access_token_key: '2296757942-WIkUaGQyHxHsSe6W4KsWnXStZo9Up6cw5LLqsQq',
  access_token_secret: 'esTngppPSimuksCGoeb3UUjkDtYawzgGy3adwiO6eP1tX'
};

var twitter = new Twitter(config);

function parseUser(data) {
  return _.extend({
    id: _.uniqueId('user'),
    /* jshint camelcase: false */
    image: data.profile_image_url
  }, _.pick(data, 'id_str', 'name', 'screen_name'));
}

function parseTweet(data) {
  return _.extend({
    id: _.uniqueId('tweet'),
    user: parseUser(data.user),
    /* jshint camelcase: false */
    createdAt: +(new Date(data.created_at)),
    formatedDate: moment(data.created_at).format('MMM Do YYYY @ h:mm a')
  }, _.pick(data, 'id_str', 'text'));
}

var CACHE_TIME = 1e3 * 60 * 15; // 15 min cache time

exports.getTweets = function(cb) {
  var CACHE_KEY = 'TWEETS';
  var tweets = cache.get(CACHE_KEY);

  if (tweets) {
    return process.nextTick(function() {
      cb(null, tweets);
    });
  }

  /* jshint camelcase: false */
  var opts = {
    screen_name: 'nychtml5',
    include_rts: true,
    exclude_replies: false
  };

  // https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
  /* jshint camelcase: true */
  twitter.get('/statuses/user_timeline.json', opts, function (data) {
    if (!Array.isArray(data)) return cb(new Error('No twitter data available'));

    tweets = data.map(parseTweet);

    cache.put(CACHE_KEY, tweets, CACHE_TIME);
    cb(null, tweets);
  });
};

exports.getHashTags = function(cb) {
  var CACHE_KEY = 'HASH_TAGS';

  var tweets = cache.get(CACHE_KEY);

  if (tweets) {
    return process.nextTick(function() {
      cb(null, tweets);
    });
  }

  /* jshint camelcase: false */
  var opts = {
    screen_name: 'nychtml5',
    include_rts: true,
    exclude_replies: false
  };

  // https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
  /* jshint camelcase: true */
  twitter.get('/statuses/user_timeline.json', opts, function (data) {
    if (!Array.isArray(data)) return cb(new Error('No twitter data available'));

    tweets = data.map(parseTweet);

    cache.put(CACHE_KEY, tweets, CACHE_TIME);
    cb(null, tweets);
  });
};

exports.get = function(cb) {
  function getTweets(done) {
    exports.getTweets(function(err, t) {
      if (err) t = [];
      done(null, t);
    });
  }

  function getHashTags(done) {
    exports.getHashTags(function(err, t) {
      if (err) t = [];
      done(null, t);
    });
  }

  async.parallel([getTweets], function(err, results) {
    if (err) return cb(err);
    var tweets = _.flatten(results);

    tweets.sort(function(a, b) {
      /* jshint camelcase: false */
      return a.createdAt > b.createdAt;
    });

    cb(null, tweets);
  });
};

@@ -1765,23 +1765,23 @@
 orts.get
-HashTag
+Mention
 s = func
@@ -1814,16 +1814,15 @@
  = '
-HASH_TAG
+MENTION
 S';%0A
@@ -2005,87 +2005,8 @@
 = %7B%0A
-    screen_name: 'nychtml5',%0A    include_rts: true,%0A    exclude_replies: false%0A
   %7D;
@@ -2124,36 +2124,40 @@
 .get('/statuses/
-user
+mentions
 _timeline.json',
@@ -2547,23 +2547,23 @@
 tion get
-HashTag
+Mention
 s(done) 
@@ -2583,15 +2583,15 @@
 .get
-HashTag
+Mention
 s(fu
@@ -2676,16 +2676,14 @@
 ync.
-parallel
+series
 (%5Bge
@@ -2689,16 +2689,29 @@
 etTweets
+, getMentions
 %5D, funct
@@ -2895,9 +2895,9 @@
 dAt 
-%3E
+%3C
  b.c
@@ -2911,24 +2911,79 @@
 t;%0A    %7D);%0A%0A
+    tweets = _.uniq(tweets, true, _.property('text'));%0A
     cb(null,

add mentions