var envEnum = {
    envTest: "test",
    envProduce: "produce"
}
var envCurrent = !location.href.includes("127.0.0.1") ? envEnum.envProduce : envEnum.envTest;
function getNewHostIP() {
    let theHostIp = "http://127.0.0.1:11432";
    if (envCurrent == envEnum.envProduce) {
        theHostIp = "http://118.195.187.121";
    }
    return theHostIp;
}
function getNewServerURL() {
    if (window.isServerIndexEquals(1)) {
        return getNewHostIP() + "/SmartBowBusinessServerMeiGuo";
    }
    return getNewHostIP() + "/SmartBowBusinessServer";
}

window.setServerIndex = function(serverIndex) {
    localStorage.setItem("serverIndex", serverIndex + "");
}
window.isServerIndexEquals = function(serverIndex) {
    return localStorage.getItem("serverIndex") === (serverIndex + "");
}

window.getSavedAuthToken = function() {
    return localStorage.getItem("authToken") ?? "";
};
window.getSavedAuthTokenKeep = function() {
    return localStorage.getItem("authTokenKeep") ? true : false;
}
window.saveLoginAuth = function(a, b) {
    localStorage.setItem("authToken", a)
    if (b) {
        localStorage.setItem("authTokenKeep", "true");
    } else {
        localStorage.removeItem("authTokenKeep");
    }
}
window.clearLoginAuth = function() {
    localStorage.removeItem("authToken");
    localStorage.removeItem("authTokenKeep");
}
window.requestAPI = function(obj) {
    $.ajax({
        headers: {
            "x-access-token": window.getSavedAuthToken()
        },
        type: obj.type ? obj.type : "GET",
        url: window.getNewServerURL() + obj.url,
        data: obj.data ? obj.data : {},
        success: (data) => {
            if (obj.success) {
                obj.success(data);
            }
        },
        error: (err) => {
            if (obj.error) {
                obj.error(err);
            }
        }
    });
};
window.getBinary = function(url, data, success) {
    var xmlhttp = new XMLHttpRequest();
    var i = 0;
    for (var key in data) {
        if (i++ === 0) {
            url += '?' + key + "=" + data[key];
        } else {
            url += '&' + key + "=" + data[key];
        }
    }
    xmlhttp.open("GET", url, true);
    xmlhttp.responseType = "blob";
    xmlhttp.onload = function () {
        success(this.response);
    };
    xmlhttp.send();
}
window.goBackToLoginView = function() {
    window.location = 'page/login-3.html';
}