/* api.js   
 * ssd api helper code
 *--------------------------------------------------------------------------*/

var api = {

  get_url : function(loc) {
    var url = "";
    if(loc == "base") {
      url = "http://tps.crumpleitup.com/";
    }
    else if(loc == "list") {
      url = "http://tps.crumpleitup.com/list" + "/" + 4757;
    }
    return url;
  },

  /* get_tweets for widgets. */
  get_tweets_w : function(startid, offset, limit, cb) {
    return api.get_tweets(startid, offset, limit,
      null, /* location */
      null, /* date */
      null, /* author */
      null, /* kw */
      null, /* startDR */
      null, /* endDR */
      cb,
      null  /* jsonp */);
  },

  get_tweets : function(startid, offset, limit, location, date, author, 
            keyword, startDR, endDR, cb, jsonp) 
  {
    // XXX: If you change args to this function be sure to also change
    //      get_tweets_w.

    var url = "http://tps.crumpleitup.com/api/searches" + "/" + 4757 
      + "/tweets/" + startid + "/" + offset + "/" + limit;
    if (jsonp) {
      url += '?callback=?';
    }
    var qargs = {};

    //Apply the active filter to the argument to be sent to the server
    if (location) {
      qargs.loc = location;
    }
    if (date) {
      qargs.date = date;
    }
    if (author) {
      qargs.author = author;
    }
    if (keyword) {
      qargs.keyword = keyword;
    }

    if (startDR) {
      qargs.startDR = startDR;
    }

    if (endDR) {
      qargs.endDR = endDR;
    }

    //AJAX request to server
    $.getJSON(url, qargs, cb);
  },

  keywords : function(cb, location, date, author, keyword, startDR, endDR) {
    var url = "http://tps.crumpleitup.com/api/searches/4757/keywords";

    var qargs = {};

    //Apply the active filter to the argument to be sent to the server
    if (location) {
      qargs.loc = location;
    }
    if (date) {
      qargs.date = date;
    }
    if (author) {
      qargs.author = author;
    }
    if (keyword) {
      qargs.keyword = keyword;
    }

    if (startDR) {
      qargs.startDR = startDR;
    }

    if (endDR) {
      qargs.endDR = endDR;
    }

    //AJAX request to server
    $.getJSON(url, qargs, cb);
  },

  search_metrics : function(cb, loc) {
    var url = "http://tps.crumpleitup.com/searches/metrics" + "/" + 4757;

    if (loc) {
      url += ("?loc=" + loc);
    }

    //AJAX request to server
    $.getJSON(url, cb);
  },

  tfgraph : function(cb, start, end, location, date, author, keyword, startDR, endDR) {
    var url = "http://tps.crumpleitup.com/api/searches/4757/tfgraph";

    var qargs = {};

    //Apply the active filter to the argument to be sent to the server
    if (location) {
      qargs.loc = location;
    }
//    if (date) {
//      qargs.date = date;
//    }
    if (author) {
      qargs.author = author;
    }
    if (keyword) {
      qargs.keyword = keyword;
    }

//    if (startDR) {
//      qargs.startDR = startDR;
//    }

//    if (endDR) {
//      qargs.endDR = endDR;
//    }

    //AJAX request to server
    $.getJSON(url, qargs, cb);
  },

  geodata : function(cb, maxdate, filter_date, filter_author, filter_keyword, startDR, endDR) {
    var url = "http://tps.crumpleitup.com/api/searches/" + "/" + 4757 
      + "/geodata";
  
    var qargs = {};

    //Apply the active filter to the argument to be sent to the server
    if (maxdate) {
      qargs.maxdate = maxdate;
    }
    if (filter_date) {
      qargs.date = filter_date;
    }
    if (filter_author) {
      qargs.author = filter_author;
    }
    if (filter_keyword) {
      qargs.keyword = filter_keyword;
    }
    if (startDR) {
      qargs.startDR = startDR;
    }
    if (endDR) {
      qargs.endDR = endDR;
    }

    //AJAX request to server
    $.getJSON(url, qargs, cb);
  },

  send_tweet : function(txt, cb) {
    var url = "http://tps.crumpleitup.com/api/twitter/send_tweet";
    $.post(url, {txt: txt}, cb);
  },
  
  send_DM : function(txt, id, cb) {
    var url = "http://tps.crumpleitup.com/api/twitter/send_DM";
    $.post(url, {txt: txt, id: id}, cb, "json");
  },

  send_response_tweet : function(txt, id, cb) {
    var url = "http://tps.crumpleitup.com/api/twitter/send_tweet";
    $.post(url, {txt: txt, id: id}, cb, "json");
  },

  //Request to open a new Ticket on the server on the specified Tweet. Also
  //send a normal Reply on the tweet. The server must additionally respond with
  //possible issues like "Is there already a ticket open on this tweet?" 
  //"is there already a ticket open with this author?" etc, and it must be 
  //reflected to the client (Although they should get warnings before hand)
  open_ticket_with_tweet : function(txt, id, cb) {
    var url = "http://tps.crumpleitup.com/api/twitter/open_ticket_with_tweet" + "/" + id;
    $.post(url, {txt: txt, id: id}, cb, "json");
  },

  //Create a new ticket with the given parameters.
  //status = True
  // XXX BROKEN!  Must use POST!
  create_ticket : function(assigned_to, priority, start_tweet_id, screen_name, cb) {
    var url = "http://tps.crumpleitup.com/ticket/create" + "?assigned_to="
        + encodeURIComponent(assigned_to) + "&priority=" + priority
        + "&start_tweet_id=" + start_tweet_id + "&screen_name="
        + encodeURIComponent(screen_name);
    $.getJSON(url, cb);
  },

  //Make a request to see if this user has an Opened ticket. 
  is_ticket_opened : function(screen_name, cb) {
    var url = "http://tps.crumpleitup.com/ticket/convo" + "/" + screen_name;
    $.getJSON(url, cb);
  },

  is_following : function(name, cb) {
    var url = "http://tps.crumpleitup.com/api/twitter/is_following" + "/" + name;
    $.getJSON(url, cb);
  },

  follow : function(name, cb) {
    var url = "http://tps.crumpleitup.com/api/twitter/follow" + "/" + name;
    $.getJSON(url, cb);
  },

  unfollow : function(name, cb) {
    var url = "http://tps.crumpleitup.com/api/twitter/unfollow" + "/" + name;
    $.getJSON(url, cb);
  },

  favorite : function(id, cb) {
    var url = "http://tps.crumpleitup.com/api/twitter/favorite" + "/" + id;
    $.getJSON(url, cb);
  },

  unfavorite : function(id, cb) {
    var url = "http://tps.crumpleitup.com/api/twitter/unfavorite" + "/" + id;
    $.getJSON(url, cb);
  },

  favorites : function(cb) {
    var url = "http://tps.crumpleitup.com/api/twitter/favorites";
    $.getJSON(url, cb);
  },

  /* Supports the simpler status request used by the widgets. */
  search_status_w : function(newest, cb) {
    return api.search_status(newest, 
      null,  /* location */
      null,  /* date */
      null,  /* author */
      null,  /* kw */
      null,  /* startDR */
      null,  /* endDR */
      cb,
      null);
  },

  search_status : function(newest, location, date, author, keyword, 
    startDR, endDR, cb, jsonp) 
  {
    // XXX: If you change args to this function be sure to also change
    //      search_status_w.

    var url = "http://tps.crumpleitup.com/searches/status" + "/" + 4757 
      + "/" + newest;
    if (jsonp) {
      url += '?callback=?';
    }
    var qargs={};

    //Apply the active filter to the argument to be sent to the server
    if (location) {
      qargs.loc = location;
    }
    if (date) {
      qargs.date = date;
    }
    if (author) {
      qargs.author = author;
    }
    if (keyword) {
      qargs.keyword = keyword;
    }
    if (startDR) {
      qargs.startDR = startDR;
    }
    if (endDR) {
      qargs.endDR = endDR;
    }

    //AJAX request to server
    $.getJSON(url, qargs, cb);
  },

  poll_status : function(cb) {
    var url = "http://tps.crumpleitup.com/api/poll/status";
    $.getJSON(url, cb);
  },

  shorten_link : function(longurl, callback) {
    var url = "http://tps.crumpleitup.com/api/url/shorten"; 
    $.getJSON(url, { longUrl: longurl }, callback);
  },
  
  ttgraph : function(cb, loc, date, auth, keyword, startDR, endDR) {
    var url = "http://tps.crumpleitup.com/api/searches/4757/ttgraph";

    var qargs = {};

    //Apply the active filter to the argument to be sent to the server
    if (loc) {
        qargs.loc = loc;
    }
    if (date) {
        qargs.date = date;
    }
    if (auth) { //DELETE to not have Author filter itself
      qargs.author = auth;
    }
    if (keyword) {
      qargs.keyword = keyword;
    }
    if (startDR) {
      qargs.startDR = startDR;
    }
    if (endDR) {
      qargs.endDR = endDR;
    }

    //AJAX request to server
    $.getJSON(url, qargs, cb);
  },

  tlgraph : function(cb, location, date, author, keyword, startDR, endDR) {
    var url = "http://tps.crumpleitup.com/api/searches/4757/tlgraph";

    var qargs = {};

    //Apply the active filter to the argument to be sent to the server
    if (location) {
      qargs.loc = location;
    }
    if (date) {
      qargs.date = date;
    }
    if (author) {
      qargs.author = author;
    }
    if (keyword) {
      qargs.keyword = keyword;
    }

    if (startDR) {
      qargs.startDR = startDR;
    }

    if (endDR) {
      qargs.endDR = endDR;
    }

    //AJAX request to server
    $.getJSON(url, qargs, cb);
  },


  geo_lookup : function(cb, loc) {
    var url = "http://tps.crumpleitup.com/api/searches/" +"geo_lookup";
    var first = true;

    //Apply the active filter to the argument to be sent to the server
    if (loc) {
      url += ("?loc=" + loc);
      first = false;
    }

    
    $.getJSON(url, cb);
  },


  /* For widgets */
  time_frequency : function(cb) {
    var url = "http://tps.crumpleitup.com/api/searches/4757/tfgraph";
    $.getJSON(url, cb);
  }


};
