window.integration = wc_el_inv_integration || {};
jQuery(document).ready(function ($) {
    function notifyDialog(message, options) {
        options = options || {};

        if (!window.Swal || 'function' !== typeof window.Swal.fire) {
            window.alert(message);
            return Promise.resolve();
        }

        return window.Swal.fire({
            title: options.title || '',
            text: message,
            icon: options.icon || 'info',
            timer: options.timer || 2200,
            timerProgressBar: true,
            showConfirmButton: false,
        }).then(function () {
            return true;
        });
    }

    if ('dev' === window.integration.mode) {
        console.log('window.integration', window.integration);
    }
    $(".save-option-button:not(.disabled) .integration_button").on("click", function () {
        const card = $(this).closest('.save-option-button');
        const optionName = card.data("option-name");
        const optionValue = card.data("option-value");
        card.addClass("loading");

        if ('dev' === window.integration.mode) {
            console.log(optionName, optionValue)
        }

        $.ajax({
            url: window.integration.ajax_url,
            method: "POST",
            data: {
                action: "integration_save_option",
                security: window.integration.nonce,
                option_name: optionName,
                option_value: optionValue,
            },
            success: function (response) {
                if ('dev' === window.integration.mode) {
                    console.log('response', response);
                }
                if (response.success) {
                    let $module = $("[data-option-name='" + response.plugin + "']");
                    if ('dev' === window.integration.mode) {
                        console.log('module', $module);
                    }
                    if ("active" === response.status) {
                        $module.find('.active').remove();
                        $module.addClass('plugin-active').data('option-value', 0);

                        if (['wc_el_inv-addon-fattureincloud'].includes(response.plugin)) {
                            $("[data-option-name='wc_el_inv-addon-fattureincloud-stock']").removeClass('disabled');
                            $("[data-option-name='wc_el_inv-addon-fattureincloud-receipts']").removeClass('disabled');
                        }

                        if ([
                            'wc_el_inv-addon-fattureincloud',
                            'wc_el_inv-addon-aruba',
                            'wc_el_inv-addon-sdi-pec',
                            'wc_el_inv-addon-cozmos',
                            'wc_el_inv-addon-pmpro',
                            'wc_el_inv-addon-sdi-via-pop',
                            'wc_el_inv-addon-peppol-via-pop',
                            'wc_el_inv-addon-ksef-via-pop',
                            'wc_el_inv-addon-zoho-via-pop'
                        ].includes(response.plugin)) {
                            location.reload();
                        }
                    } else {
                        $module.find('.active').remove();
                        $module.removeClass('plugin-active').data('option-value', 1);

                        if ('wc_el_inv-addon-fattureincloud' === response.plugin) {
                            let $activeElemStock = $("[data-option-name='wc_el_inv-addon-fattureincloud-stock']");
                            let $activeElemReceipts = $("[data-option-name='wc_el_inv-addon-fattureincloud-receipts']");
                            $activeElemStock.find('.active').remove();
                            $activeElemReceipts.find('.active').remove();

                            $activeElemStock.removeClass('plugin-active').addClass('disabled').data('option-value', 1);
                            $activeElemReceipts.removeClass('plugin-active').addClass('disabled').data('option-value', 1);
                        }

                        if ([
                            'wc_el_inv-addon-fattureincloud',
                            'wc_el_inv-addon-aruba',
                            'wc_el_inv-addon-sdi-pec',
                            'wc_el_inv-addon-cozmos',
                            'wc_el_inv-addon-pmpro',
                            'wc_el_inv-addon-sdi-via-pop',
                            'wc_el_inv-addon-peppol-via-pop',
                            'wc_el_inv-addon-ksef-via-pop',
                            'wc_el_inv-addon-zoho-via-pop'
                        ].includes(response.plugin)) {
                            location.reload();
                        }
                    }
                } else {
                    notifyDialog(response.message, {icon: 'error', timer: 2600});
                }
            },
            error: function () {
                notifyDialog(window.integration.error, {icon: 'error', timer: 2600});
            },
            complete: function () {
                card.removeClass("loading");
            },
        });
    });
});
