/*
 * 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 openGenericBarcodeScanner() {
    scanBarcode(function (result) {
        var code = "";
        var action = "";
        if (result.startsWith("https://helena.express/track#")) {
            // tracking code from receipt
            code = result.split("#")[1];
            action = "track";
        } else if (result.startsWith("https://helena.express/dropandsend#")) {
            code = result.split("#")[1];
            action = "pickupcode";
        } else if (result.startsWith("https://helena.express/das/pickup#")) {
            code = result.split("#")[1];
            action = "pickupcode";
        } else if (result.startsWith("http") && result.includes("#")) {
            if (trackingcoderegex.test(result.split("#")[1])) {
                code = result.split("#")[1];
                action = "track";
            }
        } else if (paymentRequestRegex.test(result)) {
            code = encodeURIComponent(result);
            action = "sendcrypto";
        } else if (walletPubKeyRegex.test(result)) {
            code = result;
            action = "cryptowallet";
        } else if (trackingcoderegex.test(result)) {
            code = result;
            action = "track";
        }

        switch (action) {
            case "track":
                openTrackingInfoPage(code);
                break;
            case "pickupcode":
                sendPickupCode(code);
                break;
            case "cryptowallet":
                router.navigate("/crypto/" + code);
                break;
            case "sendcrypto":
                router.navigate("/crypto?paymenturi=" + code);
                //app.dialog.alert("Not implemented.");
                break;
            default:
                app.dialog.alert("This app can't understand what's in that barcode.", "Error");
                return;
        }
    }, function () {
        app.dialog.alert("Something went wrong and we can't scan right now.", "Error");
    });
}