/**
 * @fileoverview Mock server for REST resource in JSON format.
 */

goog.provide('ydn.client.JsonMockClient');
goog.require('ydn.client.MockClient');



/**
 * <pre>
 *   client = new ydn.client.JsonMockClient(1);
 *   client.setResources([{
 *     method: 'GET',
 *     url: 'http://localhost/test',
 *     resp: {
 *       body: 'OK'
 *       status: 200
 *     }
 *   }]);
 * </pre>
 * Mock server for REST resource in JSON format.
 * @param {number=} opt_delay server respond time, default to 0 for
 * synchronous respond.
 * @constructor
 * @struct
 * @extends {ydn.client.MockClient}
 */
ydn.client.JsonMockClient = function(opt_delay) {
  ydn.client.JsonMockClient.base(this, 'constructor', opt_delay);
  /**
   * @type {Array<ydn.client.JsonMockClient.ReqObj>}
   */
  this.resources = [];
};
goog.inherits(ydn.client.JsonMockClient, ydn.client.MockClient);


/**
 * @typedef {{
 *   body: Object,
 *   status: number,
 *   statusText: (string|undefined)
 * }}
 */
ydn.client.JsonMockClient.RespObj;


/**
 * @typedef {{
 *   method: string,
 *   url: string,
 *   resp: ydn.client.JsonMockClient.RespObj
 * }}
 */
ydn.client.JsonMockClient.ReqObj;


/**
 * Set resources to respoind the request.
 * @param {Array<ydn.client.JsonMockClient.ReqObj>} res
 */
ydn.client.JsonMockClient.prototype.setResources = function(res) {
  this.resources = res;
};


/**
 * Set resource stored in JSON file in server.
 * @param {string} url
 */
ydn.client.JsonMockClient.prototype.setResourcesByUrl = function(url) {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, false);
  var me = this;
  xhr.onload = function() {
    me.resources = JSON.parse(xhr.responseText);
  };
  xhr.send(null);
};


/**
 * Add a resource
 * @param {string} mth 'GET', 'POST'
 * @param {string} url
 * @param {ydn.client.JsonMockClient.RespObj} resp
 */
ydn.client.JsonMockClient.prototype.addResource = function(mth, url, resp) {
  this.resources.push({
    method: mth,
    url: url,
    resp: resp
  });
};


/**
 * Set a resource
 * @param {string} mth 'GET', 'POST'
 * @param {string} url
 * @param {ydn.client.JsonMockClient.RespObj} resp
 * @return {number} 1 if added, 0 if replace the resource.
 */
ydn.client.JsonMockClient.prototype.setResource = function(mth, url, resp) {
  var idx = this.findResource_({
    method: mth,
    url: url
  });
  if (idx >= 0) {
    this.resources[idx].resp = resp;
    return 0;
  }
  this.resources.push({
    method: mth,
    url: url,
    resp: resp
  });
  return 1;
};


/**
 * Find resource index.
 * @param req
 * @return {number}
 * @private
 */
ydn.client.JsonMockClient.prototype.findResource_ = function(req) {
  for (var i = 0; i < this.resources.length; i++) {
    var res = this.resources[i];
    if ((!res.method || res.method == req.method) &&
        (!res.url || res.url == req.path) &&
        (!res.body || res.body == req.body)) {
      return i;
    }
  }
  return -1;
};


/**
 * @inheritDoc
 */
ydn.client.JsonMockClient.prototype.request = function(req) {
  var r = goog.base(this, 'request', req);
  var resp = r.response;
  resp.status = 404;
  resp.statusText = req.method + ' ' + req.path + ' Not Found';
  var idx = this.findResource_(req);
  if (idx >= 0) {
    var res = this.resources[idx];
    resp.status = res.resp.status;
    resp.statusText = res.resp.statusText || '';
    resp.body = res.resp.body;
  }
  return r;
};

@@ -268,19 +268,20 @@
 %0A *     
-url
+path
 : 'http:
@@ -384,16 +384,112 @@
  %3C/pre%3E%0A
+ * Set debug log for tracking bug.%0A * %3Cpre%3E%0A *   ydn.debug.log('ydn.client', 'fine');%0A * %3C/pre%3E%0A
  * Mock 
@@ -1149,19 +1149,20 @@
 g,%0A *   
-url
+path
 : string
@@ -1526,19 +1526,20 @@
 string%7D 
-url
+path
 %0A */%0Aydn
@@ -1600,19 +1600,20 @@
 unction(
-url
+path
 ) %7B%0A  va
@@ -1660,19 +1660,20 @@
 ('GET', 
-url
+path
 , false)
@@ -1866,35 +1866,36 @@
 @param %7Bstring%7D 
-url
+path
 %0A * @param %7Bydn.
@@ -1990,35 +1990,36 @@
 = function(mth, 
-url
+path
 , resp) %7B%0A  this
@@ -2053,32 +2053,34 @@
 d: mth,%0A    
-url: url
+path: path
 ,%0A    resp: 
@@ -2173,19 +2173,20 @@
 string%7D 
-url
+path
 %0A * @par
@@ -2356,19 +2356,20 @@
 on(mth, 
-url
+path
 , resp) 
@@ -2424,24 +2424,26 @@
 th,%0A    
-url: url
+path: path
 %0A  %7D);%0A 
@@ -2563,16 +2563,18 @@
     
-url: url
+path: path
 ,%0A  
@@ -2911,19 +2911,20 @@
 res.
-url
+path
  %7C%7C res.
 url 
@@ -2923,11 +2923,12 @@
 res.
-url
+path
  == 

make consistent api