var expandOrCollapse = function () {
    if ($("#outputNav").hasClass("col-2")) {
        $("#outputNav").removeClass("col-2").addClass("col-6");
        $("#mainBox").removeClass("col-8").addClass("col-4");
        $("#expand").html("Collapse");
    } else {
        $("#outputNav").removeClass("col-6").addClass("col-2");
        $("#mainBox").removeClass("col-4").addClass("col-8");
        $("#expand").html("Expand");
    }
};
var listContracts = function () {
    var request = {};
    request.action = "listContractProcess";
    request.filters = 2;
    global.wssocket.send(JSON.stringify(request));
};

var startContract = function () {
    var key = pubkeyDialogVue.sm2KeyList[pubkeyDialogVue.selectedSM2Key];
    var sm2Key = JSON.parse(key.sm2Key);
    var projectId = $("#selectedProject")[0].value;
    var project = global.projects[projectId];
    startResult.innerText = "Starting:" + project;

    var request = {};
    request.action = "startContract";
    request.isPrivate = $("#privateprojecttab").hasClass("active");

    request.owner = sm2Key.publicKey;
    request.requestID = new Date().getTime() + "";
    // request.contractid = $("#tabdiv")[0].value;
    // request.script = global.scriptEditor.getValue();
    request.path = "/" + project + "/mainfest.json";
    localStorage.setItem("persisStatus", JSON.stringify(request));
    {
        request.signature = sm2.doSignature("Fixed|" + request.path + "|"
            + sm2Key.publicKey, sm2Key.privateKey);
        request.script = "empty";
    }
    // console.log("path=" + request.path);
    global.wssocket.send(JSON.stringify(request));

};
var stopContract = function () {
    var request = {};
    request.action = "killContractProcess";
    request.requestID = new Date().getTime() + "";
    request.name = mainVue.contracts[selectedContract.value].name;
    request.id = mainVue.contracts[selectedContract.value].id;
    global.wssocket.send(JSON.stringify(request));
};

var queryContractInstanceDOI = function () {
    var request = {};
    request.action = "queryContractInstanceDOI";
    request.requestID = new Date().getTime() + "";
    request.name = mainVue.contracts[selectedContract.value].name;
    request.id = mainVue.contracts[selectedContract.value].id;
    global.wssocket.send(JSON.stringify(request));
};

var queryContractInstanceInfoByDOI = function () {
    var request = {};
    request.action = "queryContractInstanceInfoByDOI";
    request.requestID = new Date().getTime() + "";
    request.doi = contractInstanceDOIInput.value;
    global.wssocket.send(JSON.stringify(request));
};

var importContractInstanceCodeByDOI = function () {
    var request = {};
    request.action = "importContractInstanceCodeByDOI";
    request.requestID = new Date().getTime() + "";
    request.doi = contractInstanceDOIInput.value;
    global.wssocket.send(JSON.stringify(request));
};

var openInClient = function () {
    var path = document.location.pathname;
    path = path.replace("/SCIDE", "");
    path = path.replace("/OnlineIDE.html", "");
    var url = path + "/client/bdwareclient.html?self=true&contract="
        + mainVue.contracts[selectedContract.value].id;
    window.open(url);
};

var stopAllContract = function () {
    var request = {};
    request.action = "killAllContract";
    global.wssocket.send(JSON.stringify(request));
};
var selectContractToExecute = function () {
    var value = selectedContractAtExecute.value;
    if (value && value !== "Select Instance") {
        var exportedFunction = mainVue.contracts[value].exportedFunctions;
        mainVue.contractFunctions = exportedFunction;

        var pingObj = {};
        pingObj.action = "connectTo";
        pingObj.name = mainVue.contracts[value].name;
        pingObj.id = mainVue.contracts[value].id;
        global.wssocket.send(JSON.stringify(pingObj));

    }
};

var executeContract = function () {
    if (selectedFunction.value == undefined) {
        myToast("Invoke Failed", "Please select a function.");
        return;
    }
    if (selectedContractAtExecute.value == undefined) {
        myToast("Invoke Failed", "Please select an instance.");
        return;
    }
    var key = pubkeyDialogVue.sm2KeyList[pubkeyDialogVue.selectedSM2Key];
    var sm2Key = JSON.parse(key.sm2Key);

    var request = {};


    request.action = "executeContract";
    request.requestID = new Date().getTime() + "_"
        + Math.floor(Math.random() * 10000);
    var contract = mainVue.contracts[selectedContractAtExecute.value];

    request.contractID = contract.id;
    var arg = {};
    arg.action = contract.exportedFunctions[selectedFunction.value].functionName;
    arg.arg = executeContractArgInput.value;
    //console.log("[global.withEvaluatesAnalysis]"+global.withEvaluatesAnalysis);
    //if (global.withEvaluatesAnalysis != undefined) {
    //console.log("test");
    if ($('#gasLimit').val() == undefined || $('#gasLimit').val() == "") {
        arg.gasLimit = '1000';
    } else {
        arg.gasLimit = $('#gasLimit').val();
    }
    console.log("[arg.gasLimit]" + arg.gasLimit);
    //request.withEvaluatesAnalysis = global.withEvaluatesAnalysis;
    //}
    request.pubkey = sm2Key.publicKey;
    request.signature = sm2.doSignature(request.contractID + "|" + arg.action
        + "|" + arg.arg + "|" + sm2Key.publicKey, sm2Key.privateKey);
    request.arg = JSON.stringify(arg);

    localStorage.setItem("persisArg", JSON.stringify(request));
    global.wssocket.send(JSON.stringify(request));
};
var executeContractWithDynamicResult = function () {
    if (selectedFunction.value == undefined) {
        myToast("Invoke Failed", "Please select a function.");
        return;
    }
    if (selectedContractAtExecute.value == undefined) {
        myToast("Invoke Failed", "Please select an instance.");
        return;
    }
    console.log("executeContractWithDynamicResult");
    var request = {};
    request.action = "executeContract";
    request.requestID = new Date().getTime() + "";

    var contract = mainVue.contracts[selectedContractAtExecute.value];
    request.contractName = contract.id;

    var arg = {};
    arg.action = contract.exportedFunctions[selectedFunction.value].functionName;
    arg.arg = executeContractArgInput.value;

    request.withDyanmicAnalysis = true;
    request.arg = "{\"action\":\"" + arg.action + "\",\"arg\":\"" + arg.arg
        + "\"}";
    request.pubkey = global.sm2Key.publicKey;
    request.signature = sm2.doSignature(request.contractID + "|" + arg.action
        + "|" + arg.arg + "|" + global.sm2Key.publicKey,
        global.sm2Key.privateKey);
    localStorage.setItem("persisArg", JSON.stringify(request));
    global.wssocket.send(JSON.stringify(request));
};
var testEvaluates = function () {
    if (selectedFunction.value == undefined) {
        myToast("Invoke Failed", "Please select a function.");
        return;
    }
    if (selectedContractAtExecute.value == undefined) {
        myToast("Invoke Failed", "Please select an instance.");
        return;
    }
    console.log("Evaluates");
    var contract = mainVue.contracts[selectedContractAtExecute.value];

    var request = {};
    request.action = "evaluates";
    request.contractName = contract.id;
    request.functionName = contract.exportedFunctions[selectedFunction.value].functionName;
    request.args = executeContractArgInput.value;

    global.wssocket.send(JSON.stringify(request));
};

