/*
 * OpenHandle Toolbox, 0.2.3
 *
 * <p>Copyright (C) 2008 Tony Hammond</p>
 *
 * <p><a rel="license" href="http://creativecommons.org/licenses/GPL/2.0/">
 * <img alt="Creative Commons License" style="border-width:0"
 * src="http://i.creativecommons.org/l/GPL/2.0/88x62.png" /></a><br />
 * This work is licensed under a <a rel="license"
 * href="http://creativecommons.org/licenses/GPL/2.0/">Creative Commons GNU
 * General Public License License</a>.</p>
 *
 * @author <a href="mailto:tony.hammond@gmail.com">Tony Hammond</a>
 *
 */

var OpenHandle = OpenHandle || {
  Toolbox: null
};

OpenHandle.Toolbox = function() {

  // Get random handle from OpenHandle/CrossRef table
  var getRandomHandle = function() {
    var handles = [
      //
      // Handle Client Library
      '4263537/4066',
      '4263537/5009',
      '4263537/5030',
      '0.NA/0.NA',
      '0.NA/CNRI',
      //
      // Handle Technical Manual
      '4263537/5031',
      '4263537/5030',
      '4263537/5029',
      '4263537/4068',
      '4236567/5014',
      '0.TYPE/DESC',
      //
      // OpenHandle Handle Gallery
      '0.NA/0',
      '0.NA/10101',
      '0.SERV/10',
      // '0.TYPE/URL',
      '200/14',
      // '100.2/ADA300359',
      '100.10/newregistry',
      'loc.gmd/g3850.ct000509',
      '2000.01/A3D8BE7457C943FFB66ED4583059A8BA',
      '10101/nature',
      '10100/10.1038/nature06264',
      '10.1038/nature06264',
      // '10.1130/B25510.1',
      '10.2277/0521853575',
      '10.1392/ONIX_DOI_MR',
      '1721.1/29448',
      '10000.2/m.donoghue',
      //
      // CrossRef DOI Guidelines
      '10.1090/S0002-9939-00-05422-8',
      '10.1103/PhysRevLett.88.088302',
      '10.1046/j.1432-1327.2001.02263.x',
      '10.1101/gr.10.12.1841',
      // '10.1130/0091-7613(2001)',
      '10.1109/16.8842',
      '10.1023/A:1003629312096',
      '10.1162/003355300554872',
      '10.1038/26989',
      '10.1093/ageing/29.1.57',
      '10.1126/science.286.5445.1679e',
      '10.1098/rspa.2001.0787',
      '10.1086/301055',
      '10.1006/jmbi.1998.2354',
      '10.1063/1.125173',
      '10.1021/cm960127g'
    ];
    return handles[Math.floor(Math.random() * handles.length)];
  };

  var getTestHandleData = function(args) {
	
	var testHandle = {};
	
    // registered handle (10.1000/1) for testing
    testHandle.DEFAULT =
    {
      "comment" : "OpenHandle (JSON) - see http://code.google.com/p/openhandle/" ,
      "handle" : "hdl:10.1000/1" ,
      "handleStatus" : {
        "code" : "1" ,
        "message" : "SUCCESS"
      } ,
      "handleValues" : [
        {
          "index" : "100" ,
          "type" : "HS_ADMIN" ,
          "data" : {
            "adminRef" : "hdl:0.NA/10.1000?index=200" ,
            "adminPermission" : "111111110111"
          } ,
          "permission" : "1110" ,
          "ttl" : "+86400" ,
          "timestamp" : "Thu Apr 13 16:08:57 BST 2000" ,
          "reference" : []
        } ,
        {
          "index" : "1" ,
          "type" : "URL" ,
          "data" : "http://www.doi.org/index.html" ,
          "permission" : "1110" ,
          "ttl" : "+86400" ,
          "timestamp" : "Fri Sep 10 20:49:59 BST 2004" ,
          "reference" : []
        }
      ]
    };

    // a) per RFC 3651: HS_ADMIN, HS_SITE, HS_NA_DELEGATE, HS_SERV, HS_ALIAS, HS_PRIMARY, HS_VLIST
    //
    // HS_ADMIN test handle - "data" is object of AdminRecord type
    testHandle.HS_ADMIN =
    {
      "comment" : "OpenHandle (JSON) - see http://code.google.com/p/openhandle/" ,
      "handle" : "hdl:123/4567" ,
      "handleStatus" : {
        "code" : "1" ,
        "message" : "SUCCESS"
      } ,
      "handleValues" : [
        {
          "index" : "1" ,
          "type" : "HS_ADMIN" ,
          "data" : {
            "adminRef" : "hdl:0.NA/123/4567?index=200" ,
            "adminPermission" : "111111110111"
          } ,
          "permission" : "1110" ,
          "ttl" : "+86400" ,
          "timestamp" : "Thu Apr 13 16:08:57 BST 2000" ,
          "reference" : []
        }
      ]
    };
    // HS_SITE test handle - "data" is object of SiteInfo type
    testHandle.HS_SITE = 
    {
      "comment" : "OpenHandle (JSON) - see http://code.google.com/p/openhandle/" ,
      "handle" : "hdl:123/4567" ,
      "handleStatus" : {
        "code" : "1" ,
        "message" : "SUCCESS"
      } ,
      "handleValues" : [
        {
          "index" : "2" ,
          "type" : "HS_SITE" ,
          "data" : {
            "version" : "1" ,
            "protocolVersion" : {
              "majorProtocolVersion" : "2" ,
              "minorProtocolVersion" : "1"
            } ,
            "serialNumber" : "1" ,
            "primaryMask" : {
              "primary" : "true" ,
              "multiPrimary" : "false"
            } ,
            "hashOption" : "HASH_TYPE_BY_ALL" ,
            "hashFilter" : "" ,
            "attributes" : [
              {
                "name" : "desc" ,
                 "value" : "Site name"
              }
            ] ,
            "numOfServer" : 1 ,
            "serverRecords" : [
            ]
          } ,
          "permission" : "1110" ,
          "ttl" : "+86400" ,
          "timestamp" : "Thu Apr 13 16:08:57 BST 2000" ,
          "reference" : []
        }
      ]
    };
    // HS_NA_DELEGATE test handle (Unimplemented system type - same format as HS_SITE)
    testHandle.HS_NA_DELEGATE =
    {
      "comment" : "OpenHandle (JSON) - see http://code.google.com/p/openhandle/" ,
      "handle" : "hdl:123/4567" ,
      "handleStatus" : {
        "code" : "1" ,
        "message" : "SUCCESS"
      } ,
      "handleValues" : [
        {
          "index" : "3" ,
          "type" : "HS_NA_DELEGATE" ,
          "data" : "" ,
          "permission" : "1110" ,
          "ttl" : "+86400" ,
          "timestamp" : "Thu Apr 13 16:08:57 BST 2000" ,
          "reference" : []
        }
      ]
    };
    // HS_SERV test handle - "data" is reference to service handle
    testHandle.HS_SERV =
    {
      "comment" : "OpenHandle (JSON) - see http://code.google.com/p/openhandle/" ,
      "handle" : "hdl:123/4567" ,
      "handleStatus" : {
        "code" : "1" ,
        "message" : "SUCCESS"
      } ,
      "handleValues" : [
        {
          "index" : "4" ,
          "type" : "HS_SERV" ,
          "data" : "hdl:0.NA/123/7654" ,
          "permission" : "1110" ,
          "ttl" : "+86400" ,
          "timestamp" : "Thu Apr 13 16:08:57 BST 2000" ,
          "reference" : []
        }
      ]
    };
    // HS_ALIAS test handle - "data" is reference to aliased handle
    testHandle.HS_ALIAS =
    {
      "comment" : "OpenHandle (JSON) - see http://code.google.com/p/openhandle/" ,
      "handle" : "hdl:123/4567" ,
      "handleStatus" : {
        "code" : "1" ,
        "message" : "SUCCESS"
      } ,
      "handleValues" : [
        {
          "index" : "5" ,
          "type" : "HS_ALIAS" ,
          "data" : "hdl:0.NA/123/7654" ,
          "permission" : "1110" ,
          "ttl" : "+86400" ,
          "timestamp" : "Thu Apr 13 16:08:57 BST 2000" ,
          "reference" : []
        }
      ]
    };
    // HS_PRIMARY test handle (Unimplemented system type)
    // - "data" is list to references to HS_SITE values
    testHandle.HS_PRIMARY =
    {
      "comment" : "OpenHandle (JSON) - see http://code.google.com/p/openhandle/" ,
      "handle" : "hdl:123/4567" ,
      "handleStatus" : {
        "code" : "1" ,
        "message" : "SUCCESS"
      } ,
      "handleValues" : [
        {
          "index" : "6" ,
          "type" : "HS_PRIMARY" ,
          "data" : [
            "hdl:0.NA/123/7654?index=1" ,
            "hdl:0.NA/123/7654?index=2" ,
            "hdl:0.NA/123/7654?index=3"
          ] ,
          "permission" : "1110" ,
          "ttl" : "+86400" ,
          "timestamp" : "Thu Apr 13 16:08:57 BST 2000" ,
          "reference" : []
        }
      ]
    };
    // HS_VLIST test handle
    // - "data" is count plus list to references to handle values
    testHandle.HS_VLIST =
    {
      "comment" : "OpenHandle (JSON) - see http://code.google.com/p/openhandle/" ,
      "handle" : "hdl:123/4567" ,
      "handleStatus" : {
        "code" : "1" ,
        "message" : "SUCCESS"
      } ,
      "handleValues" : [
        {
          "index" : "7" ,
          "type" : "HS_VLIST" ,
          "data" : [
            "hdl:0.NA/123/7654?index=1" ,
            "hdl:0.NA/123/7654?index=2" ,
            "hdl:0.NA/123/7654?index=3"
          ] ,
          "permission" : "1110" ,
          "ttl" : "+86400" ,
          "timestamp" : "Thu Apr 13 16:08:57 BST 2000" ,
          "reference" : []
        }
      ]
    };
    // HS_VLIST test handle
    // - "data" is count plus list to references to handle values
    testHandle.CR_LR =
{
    "comment" : "OpenHandle (JSON) - see http://code.google.com/p/openhandle/" ,
    "handle" : "hdl:10.1130/B25510.1" ,
    "handleStatus" : {
        "code" : "1" ,
        "message" : "SUCCESS"
    } ,
    "handleValues" : [
        {
            "index" : "1" ,
            "type" : "URL" ,
            "data" : "http://www.crossref.org/iPage/resolver?doi=10.1130/B25510.1" ,
            "permission" : "1110" ,
            "ttl" : "+86400" ,
            "timestamp" : "Mon Mar 06 14:38:08 GMT 2006" ,
            "reference"  : []
        } ,
        {
            "index" : "2" ,
            "type" : "700050" ,
            "data" : "200508231619480000" ,
            "permission" : "1110" ,
            "ttl" : "+86400" ,
            "timestamp" : "Tue Aug 23 22:20:01 BST 2005" ,
            "reference"  : []
        } ,
        {
            "index" : "100" ,
            "type" : "HS_ADMIN" ,
           "data" : {
                "adminRef" : "hdl:0.na/10.1130?index=200" ,
                "adminPermission" : "101110101111"
            } ,
            "permission" : "1110" ,
            "ttl" : "+86400" ,
            "timestamp" : "Tue Aug 23 22:20:01 BST 2005" ,
            "reference"  : []
        } ,
        {
            "index" : "700100" ,
			"type" : "URL.0" ,
			"data" : "http://www.gsajournals.org/gsaonline/?request=get-abstract&doi=10%2E1130%2FB25510%2E1" ,
            "permission" : "1110" ,
            "ttl" : "+86400" ,
            "timestamp" : "Wed Feb 08 20:51:25 GMT 2006" ,
            "reference"  : []
        } ,
        {
            "index" : "700101" ,
            "type" : "URL.1" ,
            "data" : "http://bulletin.geoscienceworld.org/cgi/doi/10.1130/B25510.1" ,
            "permission" : "1110" ,
            "ttl" : "+86400" ,
            "timestamp" : "Mon Mar 06 14:38:09 GMT 2006" ,
            "reference"  : []
        } ,
        {
            "index" : "700052" ,
            "type" : "CR-LR" ,
            "data" : "<MR><LI label=\"GeoScienceWorld\" resource=\"URL.1\" /><LI label=\"Geological Society of America\" resource=\"URL.0\" /></MR>" ,
            "permission" : "1110" ,
            "ttl" : "+86400" ,
            "timestamp" : "Mon Mar 06 14:38:09 GMT 2006" ,
            "reference"  : []
        }
    ]
};

    if (!args) {
	  return testHandle.DEFAULT;
    }
    else {
	  switch (args) {
		case "HS_ADMIN":
        return testHandle.HS_ADMIN;
		case "HS_SITE":
        return testHandle.HS_SITE;
		case "HS_NA_DELEGATE":
        return testHandle.HS_NA_DELEGATE;
		case "HS_SERV":
        return testHandle.HS_SERV;
		case "HS_ALIAS":
        return testHandle.HS_ALIAS;
		case "HS_PRIMARY":
        return testHandle.HS_PRIMARY;
		case "HS_VLIST":
        return testHandle.HS_VLIST;
		case "CR_LR":
        return testHandle.CR_LR;
		default:
        return null;
	  }
    }
  };	

  var getTestHandleURI = function() {
    return 'hdl:10.1000/1?index=1&type=url,hs_admin&index=100&timestamp=789';
  }

  var listMethods = function() { var a = []; for (method in this) { a.push(method); } return a; };

  return {
    getRandomHandle: getRandomHandle,
    getTestHandleData: getTestHandleData,
    getTestHandleURI: getTestHandleURI,
    listMethods: listMethods
  }

};
