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

var playerProfile = [];

var messages = null;

function loadHomePage(callback) {
    callAPI("getprofile", {
        username: localStorage.getItem("username"),
        password: localStorage.getItem("password")
    }, function (data) {
        playerProfile = data.profile;
        setupProfile();
        initChat();
        refreshChat();
        setInterval(refreshChat, 1000 * 15);
        // Refresh health bar
        setInterval(function () {
            callAPI("getprofile", {
                username: localStorage.getItem("username"),
                password: localStorage.getItem("password")
            }, function (data) {
                playerProfile = data.profile;
                setupProfile();
            });
        }, 1000 * 30);
        callback();
    });
}

function setupProfile() {
    $(".player-nickname").text(playerProfile.name);
    $("#player-level-badge").html("<i class=\"" + SETTINGS["teams"][playerProfile.teamid]["icon"] + "\"></i> &nbsp; Lv. " + playerProfile.level * 1);
    $("#player-level-badge").addClass("bg-color-" + SETTINGS["teams"][playerProfile.teamid]["color"]);
    $("#player-level-badge").addClass("text-color-" + SETTINGS["teams"][playerProfile.teamid]["textcolor"]);
    app.progressbar.set($("#player-healthbar"), (playerProfile.energy / playerProfile.maxenergy) * 100, 500);
}

function scanCode() {
    if (platform_type != "cordova") {
        app.dialog.alert("You can't scan barcodes with this device.", "Sorry!");
    }
    cordova.plugins.barcodeScanner.scan(
            function (result) {
                if (!result.cancelled) {
                    callAPI("code", {
                        username: localStorage.getItem("username"),
                        password: localStorage.getItem("password"),
                        code: result.text
                    }, function (resp) {
                        if (resp.item == "" && resp.munzee == "") {
                            app.dialog.alert("You didn't find anything new.", "");
                        } else {
                            if (resp.item == "") {
                                $("#founditem-block").addClass("display-none");
                            } else {
                                $("#founditem-name").text(resp.item);
                                $("#founditem-block").removeClass("display-none");
                            }
                            if (resp.munzee == "") {
                                $("#foundmunzee-block").addClass("display-none");
                            } else {
                                $("#foundmunzee-name").text(resp.munzee);
                                $("#foundmunzee-block").removeClass("display-none");
                            }
                            app.popup.open("#founditem-popup");
                        }
                    }, function (msg) {
                        app.dialog.alert(msg, "Error");
                    });
                }
            },
            function (error) {
                app.dialog.alert(error, "Scan Error");
            },
            {
                showTorchButton: true,
                prompt: "Find a code",
                resultDisplayDuration: 0,
                disableSuccessBeep: true
            }
    );
}