/*
 * 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/.
 */


function loadHomePage(reload) {
    if (typeof reload == 'undefined') {
        reload = false;
    }
    $.post(localStorage.getItem("syncurl"), {
        username: localStorage.getItem("username"),
        key: localStorage.getItem("key"),
        action: "listapps"
    }, function (data) {
        if (data.status === 'OK') {
            if (!reload) {
                loadingProgressDialog.setProgress(70);
            }
            var appcards = [];
            Object.keys(data.apps).forEach(function (k) {
                var app = data.apps[k];
                var iconurl = app.icon;
                if (!app.icon.startsWith("http")) {
                    iconurl = app.url + app.icon;
                }
                if (!app.card) {
                    return;
                }
                appcards.push({
                    api: app.mobileapi,
                    url: app.url,
                    icon: iconurl,
                    title: app.title,
                    text: app.card.string,
                    color: app.card.color
                });
            });
            if (!reload) {
                loadingProgressDialog.setProgress(80);
                loadingProgressDialog.setText('Notifications');
            }
            getNotifications(function (notifs) {
                notifications = [];
                if (notifs != false) {
                    Object.keys(notifs).forEach(function (k) {
                        var notif = notifs[k];
                        if (!notif.seen) {
                            notifications.push(notif);
                        }
                    });
                }
                if (notifications == []) {
                    notifications = false;
                }
                if (reload) {
                    // Make it seem like it's doing something on fast connections
                    setTimeout(function () {
                        app.ptr.done();
                        router.navigate("/home", {
                            context: {
                                appcards: appcards,
                                notifications: notifications,
                                unreadnotifications: (notifications != false && notifications != []),
                                homeloaded: true
                            },
                            reloadCurrent: true
                        });
                    }, 500);
                } else {
                    loadingProgressDialog.setText('Home');
                    loadingProgressDialog.setProgress(90);
                    router.navigate("/home", {
                        context: {
                            appcards: appcards,
                            notifications: notifications,
                            unreadnotifications: (notifications != false && notifications != []),
                            homeloaded: true
                        }
                    });
                    $(".view-main").on("click", "#applist .applist-item", function () {
                        launchapp($(this).data("api"), $(this).data("url"), $(this).data("icon"), $(this).data("title"));
                    });
                    loadingProgressDialog.setProgress(100);
                    loadingProgressDialog.close();
                }
            });
        } else {
            if (reload) {
                app.ptr.done();
                return;
            }
            loadingProgressDialog.close();
            app.dialog.alert(data.msg, "Error", function () {
                restartApplication();
            });
        }
    }, "json").fail(function () {
        loadingProgressDialog.close();
        app.dialog.alert("Could not connect to the server.  Try again later.", "Error", function () {
            restartApplication();
        });
    });

}


$(".view-main").on("ptr:refresh", ".ptr-content", function () {
    loadHomePage(true);
});

$(".view-main").on("swipeout:deleted", ".notificationlist-item", function () {
    var id = $(this).data("id");
    deleteNotification(id);
});