/*
 * 调用管理
 */


// 终止本地合约
var stopContractAtSlave = function() {
	var temp = global.contractInstance;

	if (temp == undefined || temp == "选择...") {
		myToast("提示","请选择合约进程!");
		return;
	}

	var request = {};
	request.action = "killContractProcess";
	request.requestID = new Date().getTime() + "";
	request.id = temp;
	global.wssocket.send(JSON.stringify(request));
};

var onStopContract = function(obj) {
	if(obj.data == "success"){
		myToast("提示","停止该合约成功，用时" + obj.executeTime + "ms!")
	}else{
		myToast("提示","停止该合约失败，用时" + obj.executeTime + "ms!")
	}
	listContractProcess();
};


// 终止本地全部合约
var killAllContractLocal = function() {
	var request = {};
	request.action = "killAllContract";
	global.wssocket.send(JSON.stringify(request));
};

var onKillAllContract = function(obj) {
	customAlert(obj.data);
	listContractProcess();
};

var stopContractP2PTrustfully = function() {
	var request = {};
	request.action = "killContractProcess";
	request.requestID = new Date().getTime() + "";
	request.name = $("#contractProcess")[0].value;
	global.wssocket.send(JSON.stringify(request));
};



/*
 * 权限管理
 */
var showPermissionList = function(data) {
	var liHtml = "";
	var result = {};
	result.name = data;
	var x = global.permissionNameMap[data];
	if (x == undefined) {
		console.log("undefined x" + undefined);
		liHtml = "<li class='list-group-item d-flex justify-content-between align-items-center'>没有智能合约进程,请先选择合约进程  </li>";
		$("#ulPermission").html(liHtml);
		$('[data-toggle="toggle"]').bootstrapToggle();
	} else {
		x = JSON.parse(x);
		var xlen = 0;
		for ( var key in x) {
			xlen++;
			liHtml += "<li  class='list-group-item d-flex justify-content-between align-items-center'>";
			var permission = key;
			liHtml += permission;
			if (x[key] == "open") {
				liHtml += "<input id='toggle-event' type='checkbox' checked data-toggle='toggle' data-onstyle='dark' data-style='fast' data-size='xs' data-on=' ' data-off=' '/></li>";
			} else {
				liHtml += "<input id='toggle-event' type='checkbox'  data-toggle='toggle' data-onstyle='dark' data-style='fast' data-size='xs' data-on=' ' data-off=' '/></li>";

			}
		}
		if (xlen == 0) {
			liHtml = "<li class='list-group-item d-flex justify-content-between align-items-center'>没有IO权限 <input id='toggle-event' class='form-check-input' type='checkbox' data-toggle='toggle' disabled data-size='xs'></li>";
		}
		$("#ulPermission").html(liHtml);
		$('[data-toggle="toggle"]').bootstrapToggle();
		var Uarry = $("#ulPermission li");
		var Tresult = "";
		$("#ulPermission li").click(function() {
			var count = $(this).index();
			Tresult = Uarry.eq(count).text().trim();
		});
		$('[data-toggle="toggle"]').off("change", ":input").on(
			"change",
			":input",
			function() {
				var isChosen = $(this).prop('checked');
				if (isChosen == false) {
					var word = "智能合约IO权限中" + Tresult
						+ "将被取消，可能会导致智能合约无法正常执行,继续取消点击“确定按钮”";

					$("#dialogBodyDiv").text(word);
					showDialog("权限操作警告", function() {
						result.permission = Tresult;
						result.isOpen = "close";
						myToast("取消权限", "success");
						setPermission(result);
					});
				} else {
					var word = "智能合约IO权限中" + Tresult + "将被启用，继续取消点击“确定按钮”";

					$("#dialogBodyDiv").text(word);
					showDialog("权限操作警告", function() {
						result.permission = Tresult;
						result.isOpen = "open";
						myToast("启用权限", "success");
						setPermission(result);
					});

				}
			});
	}
};


/*
 * 状态管理
 */
var showChangeDumpPeriod = function() {
	var contractName = global.contractInstance;
	if (contractName == undefined || contractName == "选择...") {
		myToast("提示","请选择合约进程!");
		return;
	}

	getDumpPeriod(contractName);
};

var getDumpPeriod = function(contractName){
	var request = {};
	request.action = "getDumpPeriod";
	request.contractName = contractName;
	global.wssocket.send(JSON.stringify(request));
};

var onGetDumpPeriod = function(obj){
	var period = obj.data;
	var contractName = obj.contractName;

	var html = "<div class='input-group mb-2'>";

	html += "<div class='input-group-prepend'>";
	html += "<span class='input-group-text' id='inputGroup-sizing-default'>";
	html += "状态保存策略：" + "</span></div>";
	html += "<div class='input-group-append'>";
	html += "<select id='selectDumpPeriod2' class='custom-select' style='appearance: none; -webkit-appearance: none;'>";

	if(period == "43200000"){
		html += "<option selected>/1day自动保存</option>";
		html += "<option selected>/2days自动保存</option> ";
		html += "<option selected>不保存</option>";
		html += "<option selected>/12hours自动保存</option>";
	}else if(period == "86400000"){
		html += "<option selected>/2days自动保存</option> ";
		html += "<option selected>不保存</option>";
		html += "<option selected>/12hours自动保存</option>";
		html += "<option selected>/1day自动保存</option>";
	}else if(period == "172800000"){
		html += "<option selected>不保存</option>";
		html += "<option selected>/12hours自动保存</option>";
		html += "<option selected>/1day自动保存</option>";
		html += "<option selected>/2days自动保存</option> ";
	}else{
		html += "<option selected>/12hours自动保存</option>";
		html += "<option selected>/1day自动保存</option>";
		html += "<option selected>/2days自动保存</option> ";
		html += "<option selected>不保存</option>";
	}

	html += "</select>";
	html += "</div></div>";
	$("#dialogBodyDiv").html(html);

	showDialog("更改合约实例"+ contractName +"的自动状态保存周期", function() {
		changeDumpPeriod(contractName);
	});
};

var changeDumpPeriod = function(contractName) {
	var period = $("#selectDumpPeriod2")[0].value;
	var dumpPeriod = switchPeriod(period);
	var request = {};
	request.action = "changeDumpPeriod";
	request.dumpPeriod = dumpPeriod;
	// request.owner = global.sm2Key.publicKey;
	request.contractName = contractName;
	global.wssocket.send(JSON.stringify(request));
};

var onChangeDumpPeriod = function(obj) {
	updateResultDisplay(obj);
};