// Generated by CoffeeScript 1.7.1
(function() {
  var HipChatClient, exports, https, querystring, _;

  https = require('https');

  querystring = require('querystring');

  _ = require('underscore');

  HipChatClient = (function() {
    HipChatClient.prototype.host = 'api.hipchat.com';

    function HipChatClient(apikey, agent) {
      this.apikey = apikey;
      this.agent = agent;
      this.rateLimits = {
        limit: 0,
        remaining: 0,
        reset: 0
      };
    }

    HipChatClient.prototype.createRoom = function(params, callback) {
      var data, options, _ref;
      data = {
        name: params.name,
        owner_user_id: params.owner_user_id,
        privacy: (_ref = params.privacy) != null ? _ref : 'public',
        topic: params.topic,
        guest_access: params.guest_access ? 1 : 0
      };
      options = this._prepareOptions({
        method: 'post',
        path: '/v1/rooms/create',
        query: data
      });
      return this._sendRequest(options, callback);
    };

    HipChatClient.prototype.listRooms = function(callback) {
      var options;
      options = this._prepareOptions({
        method: 'get',
        path: '/v1/rooms/list'
      });
      return this._sendRequest(options, callback);
    };

    HipChatClient.prototype.showRoom = function(room, callback) {
      var options;
      options = this._prepareOptions({
        method: 'get',
        path: '/v1/rooms/show',
        query: {
          room_id: room
        }
      });
      return this._sendRequest(options, callback);
    };

    HipChatClient.prototype.deleteRoom = function(room, callback) {
      var options;
      options = this._prepareOptions({
        method: 'post',
        path: '/v1/rooms/delete',
        query: {
          room_id: room
        }
      });
      return this._sendRequest(options, callback);
    };

    HipChatClient.prototype.getHistory = function(params, callback) {
      var data, options, _ref, _ref1;
      data = {
        room_id: params.room_id || params.room,
        date: (_ref = params.date) != null ? _ref : 'recent',
        timezone: (_ref1 = params.timezone) != null ? _ref1 : 'UTC'
      };
      options = this._prepareOptions({
        method: 'get',
        path: '/v1/rooms/history',
        query: data
      });
      return this._sendRequest(options, callback);
    };

    HipChatClient.prototype.postMessage = function(params, callback) {
      var data, options, _ref, _ref1, _ref2;
      data = {
        room_id: params.room_id || params.room,
        from: (_ref = params.from) != null ? _ref : 'node-hipchat',
        message: params.message,
        notify: params.notify ? 1 : 0,
        color: (_ref1 = params.color) != null ? _ref1 : 'yellow',
        message_format: (_ref2 = params.message_format) != null ? _ref2 : 'html'
      };
      options = this._prepareOptions({
        method: 'post',
        path: '/v1/rooms/message',
        data: data
      });
      return this._sendRequest(options, callback);
    };

    HipChatClient.prototype.showUser = function(user_id, callback) {
      var options;
      options = this._prepareOptions({
        method: 'get',
        path: '/v1/users/show',
        query: {
          user_id: user_id
        }
      });
      return this._sendRequest(options, callback);
    };

    HipChatClient.prototype.listUsers = function(callback) {
      var options;
      options = this._prepareOptions({
        method: 'get',
        path: '/v1/users/list'
      });
      return this._sendRequest(options, callback);
    };

    HipChatClient.prototype.deleteUser = function(user_id, callback) {
      var options;
      options = this._prepareOptions({
        method: 'post',
        path: '/v1/users/delete',
        query: {
          user_id: user_id
        }
      });
      return this._sendRequest(options, callback);
    };

    HipChatClient.prototype.createUser = function(params, callback) {
      var data, options, _ref;
      data = {
        email: params.email,
        name: params.name,
        mention_name: params.name.replace(/\s+/g, ''),
        title: params.title,
        is_group_admin: params.is_group_admin ? 1 : 0,
        password: params.password,
        timezone: (_ref = params.timezone) != null ? _ref : 'UTC'
      };
      options = this._prepareOptions({
        method: 'post',
        path: '/v1/users/create',
        data: data
      });
      return this._sendRequest(options, callback);
    };

    HipChatClient.prototype.updateUser = function(params, callback) {
      var data, options, _ref;
      data = {
        user_id: params.user_id,
        email: params.email,
        name: params.name,
        mention_name: params.name.replace(/\s+/g, ''),
        title: params.title,
        is_group_admin: params.is_group_admin ? 1 : 0,
        password: params.password,
        timezone: (_ref = params.timezone) != null ? _ref : 'UTC'
      };
      options = this._prepareOptions({
        method: 'post',
        path: '/v1/users/update',
        data: data
      });
      return this._sendRequest(options, callback);
    };

    HipChatClient.prototype.getRateLimits = function() {
      return this.rateLimits;
    };

    HipChatClient.prototype._prepareOptions = function(op) {
      op.host = this.host;
      if (op.query == null) {
        op.query = {};
      }
      op.query['auth_token'] = this.apikey;
      op.query = querystring.stringify(op.query);
      op.path += '?' + op.query;
      if (op.method === 'post' && (op.data != null)) {
        op.data = querystring.stringify(op.data);
        if (op.headers == null) {
          op.headers = {};
        }
        op.headers['Content-Type'] = 'application/x-www-form-urlencoded';
        op.headers['Content-Length'] = op.data.length;
      }
      if (this.agent) {
        op.agent = this.agent;
      }
      return op;
    };

    HipChatClient.prototype._sendRequest = function(options, callback) {
      var req;
      req = https.request(options);
      req.on('response', (function(_this) {
        return function(res) {
          var buffer;
          buffer = '';
          res.on('data', function(chunk) {
            return buffer += chunk;
          });
          return res.on('end', function() {
            var headers, value;
            headers = res.headers;
            _this.rateLimits = {
              limit: headers['x-ratelimit-limit'],
              remaining: headers['x-ratelimit-remaining'],
              reset: headers['x-ratelimit-reset']
            };
            if (callback != null) {
              if (res.statusCode === 200) {
                value = options.json === false ? buffer : JSON.parse(buffer);
                return callback(value, null);
              } else {
                return callback(null, buffer);
              }
            }
          });
        };
      })(this));
      if (options.data != null) {
        req.write('' + options.data);
      }
      return req.end();
    };

    return HipChatClient;

  })();

  exports = module.exports = HipChatClient;

}).call(this);
