(function () {
    if (!window.customerBoxID) window.customerBoxID = {};
    if (!window.AuthorizeNetPopup) window.AuthorizeNetPopup = {};
    if (!AuthorizeNetPopup.options)
        AuthorizeNetPopup.options = {
            onPopupClosed: null,
        };
    AuthorizeNetPopup.closePopup = function () {
        $("body").css("overflow", "auto");
        document.getElementById("divAuthorizeNetPopupScreen").style.display = "none";
        document.getElementById("divAuthorizeNetPopup").style.display = "none";
        document.getElementById("iframeAuthorizeNet").src = "/src/authorize_net/empty.html";
        document.getElementById("btnOpenAuthorizeNetPopup").disabled = false;
        if (AuthorizeNetPopup.options.onPopupClosed) AuthorizeNetPopup.options.onPopupClosed();
    };
    AuthorizeNetPopup.openPopup = function (
        action,
        source,
        customerProfileId,
        buttonText,
        hostedProfileManageOptions,
        hostedProfileBillingAddressOptions,
        customerPaymentProfileId = null,
        customerAddressId = null,
        customerBoxID = null
    ) {
        $("#authorizeNetHeader").remove();
        $(".AuthorizeNetPopupTop").prepend(`<h6 id="authorizeNetHeader">Payment Method</h6>`);
        $("body").css("overflow", "hidden");
        var popup = document.getElementById("divAuthorizeNetPopup");
        var popupScreen = document.getElementById("divAuthorizeNetPopupScreen");
        var ifrm = document.getElementById("iframeAuthorizeNet");
        var form = document.forms["formAuthorizeNetPopup"];
        window.customerBoxID = customerBoxID;
        let url = null;
        if (action == "addShipping") {
            url = authorizeNetUrl + "/customer/addShipping";
        } else if (action == "editShipping") {
            url = authorizeNetUrl + "/customer/editShipping";
            $("#authorize_shippingAddressId").val(customerAddressId);
        } else if (action == "addPayment") {
            url = authorizeNetUrl + "/customer/addPayment";
            $("#authorize_paymentProfileId").val("");
        } else if (action == "editPayment") {
            url = authorizeNetUrl + "/customer/editPayment";
            $("#authorize_paymentProfileId").val(customerPaymentProfileId);
        }
        form.action = url;
        // ifrm.style.width = "480px";
        // ifrm.style.height = "300px";
        let hostedProfileValidationMode = "testMode";
        if(source == 'e-gift-card')
        {
            hostedProfileValidationMode = 'live';
        }
        let payload = {
            buttonText,
            hostedProfileManageOptions,
            hostedProfileBillingAddressOptions,
            hostedProfileIFrameCommunicatorUrl: `https://${window.location.host}/src/authorize_net/hostedPaymentIFrameCommunicator.html`,
            hostedProfileValidationMode: hostedProfileValidationMode,
            customerBoxID: customerBoxID
        };
        showLoading();
        $.ajax({
            method: "GET",
            url: API_URL + "/mail-order/payment/get-accept-customer-profile-page/" + customerProfileId,
            dataType: "json",
            data: payload,
            contentType: "application/json",
            success: function (data) {
                if (data.success) {
                    $("#popupToken").val(data.token);
                    form.submit();
                    popup.style.display = "";
                    popupScreen.style.display = "";
                }
                hideLoading();
            },
            error: function (xhr, status, error) {
                hideLoading();
            },
        });
    };

    AuthorizeNetPopup.onReceiveCommunication = function (querystr) {
        var params = parseQueryString(querystr);
        switch (params["action"]) {
            case "successfulSave":
                AuthorizeNetPopup.closePopup();
                if (window.customerBoxID) {
                    updateDeclinedSubscriptionPayment(window.customerBoxID);
                } else {
                    reloadSavedPayments();
                }
                break;
            case "cancel":
                AuthorizeNetPopup.closePopup();
                break;
            case "transactResponse":
                var response = params["response"];
                // document.getElementById("token").value = response;
                AuthorizeNetPopup.closePopup();
                break;
            case "resizeWindow":
                // var w = parseInt(params["width"]);
                // var h = parseInt(params["height"]);
                // var ifrm = document.getElementById("iframeAuthorizeNet");
                // ifrm.style.width = w.toString() + "px";
                // ifrm.style.height = h.toString() + "px";
                // centerPopup();
                break;
        }
    };

    function centerPopup() {
        // var d = document.getElementById("divAuthorizeNetPopup");
        // d.style.left = "50%";
        // d.style.top = "40%";
        // // d.style.position = "fixed";
        // //console.log("d.clientWidth", d.clientWidth);
        // //console.log("d.clientHeight", d.clientHeight);
        // var left = -Math.floor(d.clientWidth / 2);
        // var top = -Math.floor(d.clientHeight / 2);

        // //console.log("left", left);
        // //console.log("top", top);
        // d.style.marginLeft = left.toString() + "px";
        // d.style.marginTop = top.toString() + "px";
        // d.style.zIndex = "2";
        // if (d.offsetLeft < 16)
        // {
        //     d.style.left = "16px";
        //     d.style.marginLeft = "0px";
        // }
        // if (d.offsetTop < 16)
        // {
        //     d.style.top = "16px";
        //     d.style.marginTop = "0px";
        // }
        return;
    }

    function parseQueryString(str) {
        var vars = [];
        var arr = str.split("&");
        var pair;
        for (var i = 0; i < arr.length; i++) {
            pair = arr[i].split("=");
            vars.push(pair[0]);
            vars[pair[0]] = unescape(pair[1]);
        }
        return vars;
    }
})();
