/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

userinfo = null;
theme = {"title": "Business", "color": "#ffffff", "bgcolor": ""};
accountid = 0;

/**
 * Check if the accounts configuration seems valid.
 * @returns {Boolean} true if valid, otherwise false
 */
function isconfigvalid() {
    if (localStorage.getItem("accounts") !== null) {
        if (JSON.parse(localStorage.getItem("accounts")).length > 0) {
            return true;
        }
    }
    return false;
}

function getaccounts() {
    if (isconfigvalid()) {
        return JSON.parse(localStorage.getItem("accounts"));
    }
    return [];
}

function saveaccounts(accounts) {
    localStorage.setItem("accounts", JSON.stringify(accounts));
    NativeStorage.setItem("accounts", JSON.stringify(accounts));
}

function recoveraccounts(callback) {
    NativeStorage.getItem("accounts", function (data) {
        if (data == null || data == "" || data == "[]") {
            callback(false);
            return;
        }
        localStorage.setItem("accounts", data);
        callback(true);
    }, function () {
        callback(false);
    });
}

/**
 * Switch to a different account.
 * @param {int} account The selected account index from localStorage.getItem('accounts')
 * @param {boolean} showmsg [HAS NO EFFECT] Whether to show the "switched accounts" alert dialog.  Default True.
 * @returns {undefined}
 */
function switchaccount(account, showmsg) {
    if (typeof showmsg != 'boolean') {
        showmsg = true;
    }
    // If there isn't an accounts list yet, this should take us back to the setup
    if (!isconfigvalid()) {
        restartApplication();
    }
    var accounts = getaccounts();
    // Selected account doesn't exist, choose the first one instead
    if (typeof accounts[account] === 'undefined') {
        account = 0;
    }
    var accountinfo = accounts[account];
    localStorage.setItem("username", accountinfo['username']);
    localStorage.setItem("password", accountinfo['password']);
    localStorage.setItem("syncurl", accountinfo['syncurl']);
    localStorage.setItem("key", accountinfo['key']);
    accountid = account;

    restartApplication();
    if (showmsg == true) {
        //navigator.notification.alert("Successfully switched accounts.", null, "Switched", 'OK');
    }
}

/**
 * Add an Apps account.
 * @param {String} username
 * @param {String} password
 * @param {String} url
 * @param {String} key
 * @returns {Number} The index of the new account
 */
function addaccount(username, password, url, key) {
    var accounts = getaccounts();
    accounts.push({"username": username, "password": password, "syncurl": url, "key": key});
    saveaccounts(accounts);
    return accounts.length - 1;
}

/**
 * Remove an Apps account.
 * @param {Number} Account index
 */
function rmaccount(id) {
    var accounts = getaccounts();
    accounts.splice(id, 1);
    saveaccounts(accounts);
    if (id == accountid) {
        switchaccount(0);
    }
}

/**
 * Fetch user info (name, email, etc) from the server and save to the global
 * variable `userinfo`.
 * @param function callback An optional function to run if/when the request
 * succeeds.
 */
function getuserinfo(callback) {
    if (localStorage.getItem("username") === null
            || localStorage.getItem("password") === null
            || localStorage.getItem("syncurl") === null
            || localStorage.getItem("key") === null) {
        switchaccount(0);
    }
    $.post(localStorage.getItem("syncurl"), {
        username: localStorage.getItem("username"),
        key: localStorage.getItem("key"),
        password: localStorage.getItem("password"),
        action: "user_info"
    }, function (data) {
        if (data.status === 'OK') {
            userinfo = data.info;
            if (typeof callback == 'function') {
                callback();
            }
        } else {
            router.navigate("/loaderror");
            navigator.notification.alert(data.msg, null, "Error", 'Dismiss');
        }
    }, "json").fail(function () {
        navigator.notification.alert("Could not connect to the server.  Try again later.", null, "Error", 'Dismiss');
        router.navigate("/loaderror");
    });
}

/**
 * Set a new password for the current account
 * @param {String} newpass
 * @returns {undefined}
 */
function passwd(newpass) {
    var accounts = getaccounts();
    accounts[accountid]["password"] = newpass;
    saveaccounts(accounts);
}

function openAccountSwitcher(reload) {
    if (typeof reload == 'undefined') {
        reload = false;
    }
    var accounts = getaccounts();
    var noaccounts = (accounts.length == 0);
    for (var i = 0; i < accounts.length; i++) {
        accounts[i]["id"] = i;
        // Escape HTML characters
        accounts[i]["username"] = $('<div/>').html(accounts[i]["username"]).html();
        var synckey = accounts[i]["key"];
        var stars = "";
        for (var j = 0; j < synckey.length - 6; j++) {
            stars += "*";
        }
        accounts[i]["synckey"] = $('<div/>').html(synckey.slice(0, 3) + stars + synckey.slice(-3)).html();

        accounts[i]["syncurl"] = $('<div/>').html(accounts[i]["syncurl"].replace("/mobile/index.php", "")).html();
        accounts[i]["showsyncurl"] = false;
        accounts[i]["showtypestring"] = true;

        if (accounts[i]["syncurl"].includes(".netsyms.biz")) {
            accounts[i]["type"] = "Business";
            accounts[i]["icon"] = "business";
        } else if (accounts[i]["syncurl"].includes(".netsyms.com")) {
            accounts[i]["type"] = "Personal";
            accounts[i]["icon"] = "personal";
        } else {
            accounts[i]["type"] = "Self-hosted";
            accounts[i]["icon"] = "selfhosted";
            accounts[i]["showsyncurl"] = true;
            accounts[i]["showtypestring"] = false;
        }

        if (localStorage.getItem("username") == accounts[i]['username']) {
            accounts[i]["active"] = true;
        }
    }
    router.navigate("/settings/accounts", {
        context: {
            accounts: accounts,
            noaccounts: noaccounts
        },
        reloadCurrent: reload
    });
}