$(document).ready(function () {
    // Your code here
    console.log("setting.js is loaded and ready.");

    loadSetting();

    $('#logoutbtn').on('click', function () {
        // Clear session storage on logout
        fetch('/logout').then(() => {
            window.location.href = '/login.html';
        });
    });

    $('#savefarm').on('click', function () {
        saveFarmParameter();
    });

    $('#saveaas').on('click', function () {
        saveAASParameter();
    });
});

$(window).on('unload', function () {
    console.log("User is leaving setting.html");
    // Your cleanup code here
});

function loadSetting() {
    Get("getSetting", function (data) {
        if (data) {
            if (data.farm) {
                if (data.aas1) {
                    if (data.aas2) {
                        if (data.aas3) {
                            // all good
                            $('#brokerurl').val(data.farm.url);
                            $('#brokerusername').val(data.farm.username);
                            $('#brokerpassword').val(data.farm.password);
                            $('#brokerqueue').val(data.farm.queue);
                            $('#aas1url').val(data.aas1.url);
                            $('#aas1username').val(data.aas1.username);
                            $('#aas1password').val(data.aas1.password);
                            $('#aas2url').val(data.aas2.url);
                            $('#aas2username').val(data.aas2.username);
                            $('#aas2password').val(data.aas2.password);
                            $('#aas3url').val(data.aas3.url);
                            $('#aas3username').val(data.aas3.username);
                            $('#aas3password').val(data.aas3.password);
                        } else console.log("No AAS 3 data received");
                    } else console.log("No AAS 2 data received");
                } else console.log("No AAS 1 data received");
            } else console.log("No FARM data received");
        } else console.log("No data received");

    }, function (error) {
        alert(error);
    })

}

function saveFarmParameter() {
    let url = $('#brokerurl').val();
    let username = $('#brokerusername').val();
    let password = $('#brokerpassword').val();
    let queue = $('#brokerqueue').val();

    if (ValidString(url) == false) {
        alert("FARM Broker URL is required.");
        return;
    }
    if (ValidString(username) == false) {
        alert("FARM Broker Username is required.");
        return;
    }
    if (ValidString(password) == false) {
        alert("FARM Broker Password is required.");
        return;
    }
    if (ValidString(queue) == false) {
        alert("FARM Broker Queue is required.");
        return;
    }


    Post("saveFARM", {
        farm: {
            url: url,
            username: username,
            password: password,
            queue: queue
        }

    }, function (data) {
        alert(data.message);
    }, function (error) {
        alert(error);
    });

}
function saveAASParameter() {
    let aas1URL = $('#aas1url').val();
    let aas1username = $('#aas1username').val();
    let aas1password = $('#aas1password').val();
    let aas2URL = $('#aas2url').val();
    let aas2username = $('#aas2username').val();
    let aas2password = $('#aas2password').val();
    let aas3URL = $('#aas3url').val();
    let aas3username = $('#aas3username').val();
    let aas3password = $('#aas3password').val();

    if (ValidString(aas1URL) == false) {
        alert("AAS1 URL is required.");
        return;
    }
    if (ValidString(aas1username) == false) {
        alert("AAS1 Username is required.");
        return;
    }
    if (ValidString(aas1password) == false) {
        alert("AAS1 Password is required.");
        return;
    }
    if (ValidString(aas2URL) == false) {
        alert("AAS2 URL is required.");
        return;
    }
    if (ValidString(aas2username) == false) {
        alert("AAS2 Username is required.");
        return;
    }
    if (ValidString(aas2password) == false) {
        alert("AAS2 Password is required.");
        return;
    }
    if (ValidString(aas3URL) == false) {
        alert("AAS3 URL is required.");
        return;
    }
    if (ValidString(aas3username) == false) {
        alert("AAS3 Username is required.");
        return;
    }
    if (ValidString(aas3password) == false) {
        alert("AAS3 Password is required.");
        return;
    }
    Post("saveAAS", {
        aas1: {
            url: aas1URL,
            username: aas1username,
            password: aas1password
        },
        aas2: {
            url: aas2URL,
            username: aas2username,
            password: aas2password
        },
        aas3: {
            url: aas3URL,
            username: aas3username,
            password: aas3password
        }
    }, function (data) {
        alert(data.message);
    }, function (error) {
        alert(error)
    });
}