/*
 * 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 itemTypeCharacteristics = {
    "Parcel": {weight: true, size: true, lbs: true},
    // USPS
    "Card": {weight: false, size: false, lbs: false},
    "Letter": {weight: true, size: false, lbs: false},
    "Flat": {weight: true, size: false, lbs: true},
    "FlatRateEnvelope": {weight: false, size: false, lbs: false},
    "FlatRateLegalEnvelope": {weight: false, size: false, lbs: false},
    "FlatRatePaddedEnvelope": {weight: false, size: false, lbs: false},
    "SmallFlatRateBox": {weight: false, size: false, lbs: false},
    "MediumFlatRateBox": {weight: false, size: false, lbs: false},
    "LargeFlatRateBox": {weight: false, size: false, lbs: false},
    "LargeFlatRateBoxAPOFPO": {weight: false, size: false, lbs: false},
    "RegionalRateBoxA": {weight: true, size: false, lbs: true},
    "RegionalRateBoxB": {weight: true, size: false, lbs: true},
    // UPS
    "UPSLetter": {weight: true, size: false, lbs: true},
    "UPSExpressBox": {weight: true, size: false, lbs: true},
    "Pak": {weight: true, size: false, lbs: true},
    "Tube": {weight: true, size: false, lbs: true},
    "SmallExpressBox": {weight: true, size: false, lbs: true},
    "MediumExpressBox": {weight: true, size: false, lbs: true},
    "LargeExpressBox": {weight: true, size: false, lbs: true},
    // FedEx
    "FedExEnvelope": {weight: true, size: false, lbs: true},
    "FedExBox": {weight: true, size: false, lbs: true},
    "FedExPak": {weight: true, size: false, lbs: true},
    "FedExTube": {weight: true, size: false, lbs: true},
    "FedExSmallBox": {weight: true, size: false, lbs: true},
    "FedExMediumBox": {weight: true, size: false, lbs: true},
    "FedExLargeBox": {weight: true, size: false, lbs: true},
    "FedExExtraLargeBox": {weight: true, size: false, lbs: true}
};

function updateRateForm() {
    if ($("#to_country").val() == "" || $("#to_country").val() == "US") {
        $(".citystateboxes").css("display", "none");
        $("#ziptext").html("ZIP Code");
        $("#to_zip").attr("inputmode", "numeric");
        $("#to_zip").attr("required", "true");
        $("#to_zip").attr("placeholder", "ZIP Code");
    } else {
        $(".citystateboxes").css("display", "");
        $("#ziptext").html("Postal Code");
        $("#to_zip").attr("inputmode", "text");
        $("#to_zip").removeAttr("required");
        $("#to_zip").attr("placeholder", "Postal Code");
    }


    $("#itemweight").css("display", "none");
    $("#itemsize").css("display", "none");
    $("#itemweightlbs").css("display", "none");

    if ($("#itemType").val() == "") {
        // no selection, leave all boxes hidden
        return;
    }
    if (typeof itemTypeCharacteristics[$("#itemType").val()] == 'undefined') {
        // not in list of item type info
        $("#itemweight").css("display", "");
        $("#itemsize").css("display", "");
        $("#itemweightlbs").css("display", "");
        return;
    }

    var chars = itemTypeCharacteristics[$("#itemType").val()];
    if (chars.weight) {
        // show weight box for ounces
        $("#itemweight").css("display", "");
    } else {
        $("#weightLbs").val("0");
        $("#weightOz").val("0");
    }
    if (chars.lbs) {
        // show weight box for pounds
        $("#itemweightlbs").css("display", "");
    } else {
        $("#weightLbs").val("0");
    }
    if (chars.size) {
        $("#itemsize").css("display", "");
    }
}

$("#app").on("click change", "#itemType", updateRateForm);
$("#app").on("click change", "#to_country", updateRateForm);

function initRateForm() {
    $("#itemType").val("");
    updateRateForm();
}

function getRates() {
    app.dialog.preloader("Loading...");

    apirequest(
            SETTINGS.apis.rates,
            {
                from_street1: $("#from_street1").val(),
                from_zip: $("#from_zip").val(),
                to_street1: $("#to_street1").val(),
                to_zip: $("#to_zip").val(),
                to_city: $("#to_city").val(),
                to_state: $("#to_state").val(),
                to_country: $("#to_country").val(),
                itemType: $("#itemType").val(),
                length: $("input[name=length]").val(),
                width: $("input[name=width]").val(),
                height: $("input[name=height]").val(),
                weightLbs: $("#weightLbs").val(),
                weightOz: $("#weightOz").val()
            },
            function (resp) {
                app.dialog.close();
                if (resp.status == "OK") {
                    if (resp.rates.length == 0) {
                        resp.rates = false;
                    }
                    if (resp.hints.length == 0) {
                        resp.hints = false;
                    }
                    $("#rateResultPopup").html(compiledPages.rateresult(resp));
                    app.popup.open("#rateResultPopup");
                    sendActionReport("Rates", "Got rates", $("#from_zip").val() + " -> " + $("#to_zip").val());
                } else {
                    app.dialog.alert(resp.message, "Error");
                    sendErrorReport("Rates", "Couldn't get rates", resp.message);
                }
            },
            function (xhr) {
                app.dialog.close();
                try {
                    var error = $.parseJSON(xhr.responseText);
                    if (error && typeof error.msg != 'undefined') {
                        app.dialog.alert(error.msg, "Error");
                        sendErrorReport("Rates", "Couldn't get rates", error.msg);
                    } else {
                        app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later.", "Error");
                        sendErrorReport("Rates", "Couldn't get rates", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
                    }
                } catch (ex) {
                    app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later.", "Error");
                    sendErrorReport("Rates", "Couldn't get rates", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
                }
            }, "GET");
}