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

// Open geo: url
$(".view-main").on("click", "a #geocoords", function () {
    window.open($(this).attr("href"), "_system");
});

function addressLookup() {
    if ($("#numberstreetinput").val() == "") {
        app.dialog.alert("A street address is required.", "Not enough info");
        return;
    }
    if ($("#cityinput").val() == "" || $("#stateinput").val() == "") {
        var ziprequired = true;
    }
    if ($("#zipcodeinput").val() == "" && ziprequired) {
        app.dialog.alert("A ZIP code or city and state are required.", "Not enough info");
        return;
    }
    app.dialog.preloader("Working...");
    var addrlookupdialogopen = true;
    $.ajax({
        url: SETTINGS.addrlookupapi,
        dataType: 'json',
        method: 'post',
        data: {
            "street": $("#numberstreetinput").val(),
            "city": $("#cityinput").val(),
            "state": $("#stateinput").val(),
            "zip": $("#zipcodeinput").val()
        },
        timeout: 15 * 1000,
        success: function (resp) {
            if (addrlookupdialogopen) {
                app.dialog.close();
                addrlookupdialogopen = false;
            }
            if (resp.status == "OK") {
                if (resp.address.status == "OK") {
                    var address = resp.address;
                    $(".item-text #address").text(address.address);
                    var zipstr = "";
                    var postnetstr = "";
                    var postnetspacedstr = "";
                    if (address.zip != "") {
                        zipstr = address.zip;
                        postnetstr = address.zip;
                        postnetspacedstr = address.zip;
                    }
                    if (address.plus4 != "") {
                        zipstr += "-" + address.plus4;
                        postnetstr += "" + address.plus4;
                        postnetspacedstr += " " + address.plus4;
                    }
                    if (address.delivery_point != "") {
                        postnetstr += "" + address.delivery_point;
                        postnetspacedstr += " " + address.delivery_point;
                    }
                    $(".item-text #citystate").text(address.city + " " + address.state + " " + zipstr);

                    $(".item-text #dp").text(address.delivery_point);
                    $(".item-text #route").text(address.route);
                    $(".item-text #county").text(address.county);
                    $(".item-text #dpvconfirmed").text(address.dpv_confirmed ? "Yes" : "No");
                    $(".addrresult").removeClass("display-none");
                    let canvas = document.createElement('canvas');
                    bwipjs.toCanvas(canvas, {
                        bcid: 'postnet', // Barcode type
                        text: postnetstr, // Text to encode
                        scale: 2,
                        height: 3, // Bar height, in millimeters
                        alttext: postnetspacedstr,
                        includetext: true, // Show human-readable text
                        textxalign: 'center', // Always good to set this
                    });
                    document.getElementById("postnetbarcode").src = canvas.toDataURL('image/png');
                    $(".barcoderesult").removeClass("display-none");
                } else {
                    $(".addrresult").addClass("display-none");
                    $(".barcoderesult").addClass("display-none");
                    app.dialog.alert(resp.address.message, "Error");
                }
                if (resp.geocode.status == "OK") {
                    $(".georesult").removeClass("display-none");
                    $(".item-text #geocoords").text(resp.geocode.latitude.toFixed(4) + ", " + resp.geocode.longitude.toFixed(4));
                    $(".item-text #geocoords").attr("href", "geo:" + resp.geocode.latitude.toFixed(4) + "," + resp.geocode.longitude.toFixed(4));
                } else {
                    $(".georesult").addClass("display-none");
                }
            } else {
                app.dialog.alert(resp.message, "Error");
            }
        },
        error: function (jqXHR, status, errorThrown) {
            if (addrlookupdialogopen) {
                app.dialog.close();
                addrlookupdialogopen = false;
            }
            app.dialog.alert("There was a network or server issue.  Please try again.", "Error");
        }
    });
}