'use strict';

var oboe = require('oboe');


// Model that can get stories from different types
// of reddit subreddits
exports.Reddit = function(type, subreddit) {
  var self = this;
  self.subreddit = subreddit;
  self.type = type;
  self.limit = 12;
  self.count = 0;
  self.after = "";
  self.baseUrl = 'http://www.reddit.com/r/';
  self.subUrl = subreddit ? self.baseUrl + subreddit + '/' : self.baseUrl;
  self.url = self.subUrl + type + '.json';
  var translateItem = function(item) {
    return {
      thumbnail: item.data.thumbnail,
      title: item.data.title,
      url: item.data.url,
      createdAt: item.data.created_utc,
      comments: item.data.num_comments,
      score: item.data.score,
      sourceLink: 'http://www.reddit.com' + item.data.permalink,
      story: '',
      item: item,
      source: {
        model: "Reddit",
        type: self.type,
        subreddit: self.subreddit
      },
    };
  };
  var translate = function(data) {
    return data.data.children.map(function(item) {
      return translateItem(item);
    });
  };
  var done = function(cb) {
    return function(data) {
      console.log('Successfully retrieved reddit ' + self.subreddit + ' ' + self.type);
      self.count += self.limit;
      self.after = data.data.after;
      console.log("SUCCESS: " + self.count + " AFTER: " + self.after);
      cb(translate(data));
    };
  };
  var fail = function(errCb) {
    return function(err) {
      console.log('********ERROR GETTING REDDIT ' + self.subreddit + ' ' + self.type);
      console.dir(err);
      if (err && err.thrown && err.thrown.stack) {
        console.log(err.thrown.stack);
      }
      errCb(err);
    };
  };
  var node = function(cb) {
    return function(item) {
      cb(translateItem(item));
    };
  };
  var getUrl = function() {
    var url = self.url + "?count=" + self.count + "&limit=" + self.limit;
    if (self.after) {
      url += "&after=" + self.after;
    }
    return url;
  }
  self.get = function getReddit(cb, errCb) {
    oboe(getUrl())
      .done(done(cb))
      .fail(fail(errCb));
  };
  self.getNodes = function getRedditNodes(cb, doneCb, errCb) {
    console.log("CALLING GET: " + self.count + " AFTER: " + self.after);
    oboe(getUrl())
      .node('data.children.*', node(cb))
      .done(done(doneCb))
      .fail(fail(errCb));
  };
};


// The retriever turns on the streams talking to the APIs to retreive stories.
exports.Retreiver = function(config) {
  var self = this;
  self.config = config;
  self.sources = self.config.sources;
  self.get = function(sourceCb, doneCb) {
    self.sources.forEach(function(source) {
      var responses = [];
      source.get(function success() {
        console.log("Received source callback SUCCESS data: ", arguments);
        responses.push({source: source, data: arguments, success: true});
        sourceCb.apply(this, arguments);
        if (responses.length === self.sources.length) {
          doneCb(responses);
        }
      }, function fail() {
        console.log("Received source callback ERROR data: ", arguments);
        responses.push({source: source, data: arguments, success: false});
        if (responses.length === self.sources.length) {
          doneCb(responses);
        }
      });
    });
  };
  self.getNodes = function(cb, doneCb) {
    self.sources.forEach(function(source) {
      var responses = [];
      source.getNodes(cb, function success() {
        console.log("Received source callback SUCCESS data: ", arguments);
        responses.push({source: source, data: arguments, success: true});
        if (responses.length === self.sources.length) {
          doneCb(responses);
        }
      }, function fail() {
        console.log("Received source callback ERROR data: ", arguments);
        responses.push({source: source, data: arguments, success: false});
        if (responses.length === self.sources.length) {
          doneCb(responses);
        }
      });
    });
  };
};















































