$("#startButton").button({
	icon : "ui-icon-circle-triangle-e",
	showLabel : false
});
$("#queryTransBtn").button({
	icon : "ui-icon-circle-triangle-e",
	showLabel : false
});
$("#sendTransBtn").button({
	icon : "ui-icon-circle-triangle-e",
	showLabel : false
});
$("#queryRecentBtn").button({
	icon : "ui-icon-circle-triangle-e",
	showLabel : false
});
global = {};
global.outputStreamEditor = CodeMirror.fromTextArea($("#outputStreamArea")[0],
		{
			matchBrackets : true,
			autoCloseBrackets : true,
			lineNumbers : true,
			mode : "application/ld+json",
			lineWrapping : true
		});
var getRequestParameters = function() {
	var arr = (location.search || "").replace(/^\?/, '').split("&");
	var params = {};
	for (var i = 0; i < arr.length; i++) {
		var data = arr[i].split("=");
		if (data.length == 2) {
			params[data[0]] = data[1];
		}
	}
	return params;
};
var sendMockData = function() {
	var c = $("#transCount")[0].value;
	if (c.length == 0) {
		alert("请输入交易量，>0");
		return;
	}
	global.outputStreamEditor.setValue("");
	var startDot = setInterval(function() {
		var v = global.outputStreamEditor.getValue();
		v += ".";
		global.outputStreamEditor.setValue(v);
	}, 1000);
	$.ajax({
		url : "./SCManager",
		data : {
			"action" : "sendMockTransaction",
			"count" : c
		},
		dataType : "jsonp"
	}).done(function(result) {
		console.log(result);
		global.r = result;
		clearInterval(startDot);
		formatDisplay(result);
	})
};
var startDoting = function() {
	global.outputStreamEditor.setValue("");
	return setInterval(function() {
		var v = global.outputStreamEditor.getValue();
		v += ".";
		global.outputStreamEditor.setValue(v);
	}, 1000);
};
var formatDisplay = function(result) {
	global.outputStreamEditor.setValue(JSON.stringify(result));
	var totalLines = global.outputStreamEditor.lineCount();
	global.outputStreamEditor.autoFormatRange({
		line : 0,
		ch : 0
	}, {
		line : totalLines
	});
};
var queryTransaction = function() {
	var hash = $("#transHash")[0].value;
	if (hash.length == 0)
		hash = "2d4a7ba461c284434ec88be3e095b450c4145c1d";
	global.outputStreamEditor.setValue("");
	var dot = startDoting();
	$.ajax({
		url : "./SCManager",
		data : {
			"action" : "queryTransByHash",
			"hash" : hash
		},
		dataType : "jsonp"
	}).done(function(result) {
		console.log(result);
		clearInterval(dot);

		global.r = result;
		formatDisplay(result);
	})
};
var init = function() {
	var params = getRequestParameters();
	if (params["queryHash"] != undefined) {
		$("#transHash")[0].value = params["queryHash"];
		queryTransaction();
	}
	var val = localStorage.getItem("ReqTestTool", JSON.stringify(val));
	if (val == undefined || val.length < 2)
		return;
	val = JSON.parse(val);
	
	$("#cidInput")[0].value = val.contractID;
	$("#actionInput")[0].value = val.action;
	$("#argInput")[0].value = val.arg;
	$(".CodeMirror-wrap").css("border-radius", "10px");
	$(".CodeMirror-wrap").css("background", "#f0e4e4");
	$(".CodeMirror-wrap").css("border-radius", "10px");
	$(".CodeMirror-wrap").css("border-radius", "10px");
	$(".ui-button").css("border-radius", "10px");

};
var getReqObj = function() {
	var val = {};
	val.contractID = $("#cidInput")[0].value;
	val.action = $("#actionInput")[0].value;
	val.arg = $("#argInput")[0].value;
	return val;
}
var sendTrans = function() {
	var val = getReqObj();
	localStorage.setItem("ReqTestTool", JSON.stringify(val));
	global.requestID = new Date().getTime();
	global.outputStreamEditor.setValue("");
	var startDot = startDoting();
	setGeneratedUrl();
	$.ajax({
		url : "./SCManager",
		data : {
			"action" : "executeContract",
			"contractID" : val.contractID,
			"operation" : val.action,
			"arg" : val.arg,
			"requestID" : global.requestID
		},
		dataType : "jsonp"
	}).done(function(result) {
		console.log(result);
		clearInterval(startDot);
		formatDisplay(result);
	})
	setTimeout(getRequestHash, 500);
};
var setGeneratedUrl = function() {
	var val = getReqObj();
	var plainUrl = document.location.href;
	plainUrl = plainUrl.replace("queryTrans.html", "SCManager");
	plainUrl += "?action=executeContract&";
	plainUrl += "&contractID=" + val.contractID;
	plainUrl += "&operation=" + val.action;
	plainUrl += "&arg=" + encodeURIComponent(val.arg);
	$("#generatedPlainUrlInput")[0].value = plainUrl;
	var url = document.location.href;
	url = url.replace("queryTrans.html", "SCManager");
	var privKey = localStorage.getItem("PrivKey");
	var pubKey = loadRSAKey(privKey);
	var eReq = encryptReq(val, pubKey);
	url += "?action=executeContractEncrypted&contractRequest=";
	url += encodeURIComponent(JSON.stringify(eReq));
	$("#generatedUrlInput")[0].value = url;

};
var getRequestHash = function() {
	$.ajax({
		url : "./SCManager",
		data : {
			"action" : "queryHashByReqID",
			"requestID" : global.requestID,
		},
		dataType : "jsonp"
	}).done(function(result) {
		global.r = result;
		if (global.r.data != undefined && result.data.length > 10)
			$("#transHash")[0].value = result.data;
		else
			setTimeout(getRequestHash, 200);
	})
};
var queryRecent = function() {
	var blockCount = $("#queryRecentInput")[0].value;
	if (blockCount.length == 0) {
		blockCount = "5";
	}
	var startDot = startDoting();

	$.ajax({
		url : "./SCManager",
		data : {
			"action" : "queryRecent",
			"blockCount" : blockCount,
		},
		dataType : "jsonp"
	}).done(function(result) {
		global.r = result;
		console.log(result);
		clearInterval(startDot);
		formatDisplay(result);
	})
};
init();