//
// Autogenerated by Thrift Compiler (0.9.2)
//
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
//
var thrift = require('../thrift');
var Thrift = thrift.Thrift;
var Q = thrift.Q;


var ttypes = require('./bridge_types');
//HELPER FUNCTIONS AND STRUCTURES

TSBridgeService_invoke_args = function(args) {
  this.method = null;
  this.file = null;
  this.position = null;
  this.params = null;
  if (args) {
    if (args.method !== undefined) {
      this.method = args.method;
    }
    if (args.file !== undefined) {
      this.file = args.file;
    }
    if (args.position !== undefined) {
      this.position = args.position;
    }
    if (args.params !== undefined) {
      this.params = args.params;
    }
  }
};
TSBridgeService_invoke_args.prototype = {};
TSBridgeService_invoke_args.prototype.read = function(input) {
  input.readStructBegin();
  while (true)
  {
    var ret = input.readFieldBegin();
    var fname = ret.fname;
    var ftype = ret.ftype;
    var fid = ret.fid;
    if (ftype == Thrift.Type.STOP) {
      break;
    }
    switch (fid)
    {
      case 1:
      if (ftype == Thrift.Type.STRING) {
        this.method = input.readString();
      } else {
        input.skip(ftype);
      }
      break;
      case 2:
      if (ftype == Thrift.Type.STRING) {
        this.file = input.readString();
      } else {
        input.skip(ftype);
      }
      break;
      case 3:
      if (ftype == Thrift.Type.I32) {
        this.position = input.readI32();
      } else {
        input.skip(ftype);
      }
      break;
      case 4:
      if (ftype == Thrift.Type.STRING) {
        this.params = input.readString();
      } else {
        input.skip(ftype);
      }
      break;
      default:
        input.skip(ftype);
    }
    input.readFieldEnd();
  }
  input.readStructEnd();
  return;
};

TSBridgeService_invoke_args.prototype.write = function(output) {
  output.writeStructBegin('TSBridgeService_invoke_args');
  if (this.method !== null && this.method !== undefined) {
    output.writeFieldBegin('method', Thrift.Type.STRING, 1);
    output.writeString(this.method);
    output.writeFieldEnd();
  }
  if (this.file !== null && this.file !== undefined) {
    output.writeFieldBegin('file', Thrift.Type.STRING, 2);
    output.writeString(this.file);
    output.writeFieldEnd();
  }
  if (this.position !== null && this.position !== undefined) {
    output.writeFieldBegin('position', Thrift.Type.I32, 3);
    output.writeI32(this.position);
    output.writeFieldEnd();
  }
  if (this.params !== null && this.params !== undefined) {
    output.writeFieldBegin('params', Thrift.Type.STRING, 4);
    output.writeString(this.params);
    output.writeFieldEnd();
  }
  output.writeFieldStop();
  output.writeStructEnd();
  return;
};

TSBridgeService_invoke_result = function(args) {
  this.success = null;
  if (args) {
    if (args.success !== undefined) {
      this.success = args.success;
    }
  }
};
TSBridgeService_invoke_result.prototype = {};
TSBridgeService_invoke_result.prototype.read = function(input) {
  input.readStructBegin();
  while (true)
  {
    var ret = input.readFieldBegin();
    var fname = ret.fname;
    var ftype = ret.ftype;
    var fid = ret.fid;
    if (ftype == Thrift.Type.STOP) {
      break;
    }
    switch (fid)
    {
      case 0:
      if (ftype == Thrift.Type.STRING) {
        this.success = input.readString();
      } else {
        input.skip(ftype);
      }
      break;
      case 0:
        input.skip(ftype);
        break;
      default:
        input.skip(ftype);
    }
    input.readFieldEnd();
  }
  input.readStructEnd();
  return;
};

TSBridgeService_invoke_result.prototype.write = function(output) {
  output.writeStructBegin('TSBridgeService_invoke_result');
  if (this.success !== null && this.success !== undefined) {
    output.writeFieldBegin('success', Thrift.Type.STRING, 0);
    output.writeString(this.success);
    output.writeFieldEnd();
  }
  output.writeFieldStop();
  output.writeStructEnd();
  return;
};

TSBridgeServiceClient = exports.Client = function(output, pClass) {
    this.output = output;
    this.pClass = pClass;
    this._seqid = 0;
    this._reqs = {};
};
TSBridgeServiceClient.prototype = {};
TSBridgeServiceClient.prototype.seqid = function() { return this._seqid; }
TSBridgeServiceClient.prototype.new_seqid = function() { return this._seqid += 1; }
TSBridgeServiceClient.prototype.invoke = function(method, file, position, params, callback) {
  this._seqid = this.new_seqid();
  if (callback === undefined) {
    var _defer = Q.defer();
    this._reqs[this.seqid()] = function(error, result) {
      if (error) {
        _defer.reject(error);
      } else {
        _defer.resolve(result);
      }
    };
    this.send_invoke(method, file, position, params);
    return _defer.promise;
  } else {
    this._reqs[this.seqid()] = callback;
    this.send_invoke(method, file, position, params);
  }
};

TSBridgeServiceClient.prototype.send_invoke = function(method, file, position, params) {
  var output = new this.pClass(this.output);
  output.writeMessageBegin('invoke', Thrift.MessageType.CALL, this.seqid());
  var args = new TSBridgeService_invoke_args();
  args.method = method;
  args.file = file;
  args.position = position;
  args.params = params;
  args.write(output);
  output.writeMessageEnd();
  return this.output.flush();
};

TSBridgeServiceClient.prototype.recv_invoke = function(input,mtype,rseqid) {
  var callback = this._reqs[rseqid] || function() {};
  delete this._reqs[rseqid];
  if (mtype == Thrift.MessageType.EXCEPTION) {
    var x = new Thrift.TApplicationException();
    x.read(input);
    input.readMessageEnd();
    return callback(x);
  }
  var result = new TSBridgeService_invoke_result();
  result.read(input);
  input.readMessageEnd();

  if (null !== result.success) {
    return callback(null, result.success);
  }
  return callback('invoke failed: unknown result');
};
TSBridgeServiceProcessor = exports.Processor = function(handler) {
  this._handler = handler
}
TSBridgeServiceProcessor.prototype.process = function(input, output) {
  var r = input.readMessageBegin();
  if (this['process_' + r.fname]) {
    return this['process_' + r.fname].call(this, r.rseqid, input, output);
  } else {
    input.skip(Thrift.Type.STRUCT);
    input.readMessageEnd();
    var x = new Thrift.TApplicationException(Thrift.TApplicationExceptionType.UNKNOWN_METHOD, 'Unknown function ' + r.fname);
    output.writeMessageBegin(r.fname, Thrift.MessageType.EXCEPTION, r.rseqid);
    x.write(output);
    output.writeMessageEnd();
    output.flush();
  }
}

TSBridgeServiceProcessor.prototype.process_invoke = function(seqid, input, output) {
  var args = new TSBridgeService_invoke_args();
  args.read(input);
  input.readMessageEnd();
  if (this._handler.invoke.length === 4) {
    Q.fcall(this._handler.invoke, args.method, args.file, args.position, args.params)
      .then(function(result) {
        var result = new TSBridgeService_invoke_result({success: result});
        output.writeMessageBegin("invoke", Thrift.MessageType.REPLY, seqid);
        result.write(output);
        output.writeMessageEnd();
        output.flush();
      }, function (err) {
        var result = new TSBridgeService_invoke_result(err);
        output.writeMessageBegin("invoke", Thrift.MessageType.REPLY, seqid);
        result.write(output);
        output.writeMessageEnd();
        output.flush();
      });
  } else {
    this._handler.invoke(args.method, args.file, args.position, args.params,  function (err, result) {
      var result = new TSBridgeService_invoke_result((err != null ? err : {success: result}));
      output.writeMessageBegin("invoke", Thrift.MessageType.REPLY, seqid);
      result.write(output);
      output.writeMessageEnd();
      output.flush();
    });
  }
}

