(function () {
    'use strict';

    var integrazoWzDeactivateUrl = '';

    function integrazoWzDeactivateInit() {
        if (typeof integrazo_wz_deactivate === 'undefined') return;

        var basename = integrazo_wz_deactivate.plugin_basename;
        var row = document.querySelector('tr[data-plugin="' + basename + '"]');
        if (!row) return;

        var link = row.querySelector('a[href*="action=deactivate"]');
        if (!link) return;

        link.addEventListener('click', function (e) {
            e.preventDefault();
            integrazoWzDeactivateUrl = link.href;
            integrazoWzDeactivateBuild();
            integrazoWzDeactivateShow();
        });
    }

    function integrazoWzDeactivateBuild() {
        if (document.getElementById('integrazo-wz-deactivate-overlay')) return;

        var html =
            '<div id="integrazo-wz-deactivate-overlay" class="integrazo-wz-deactivate-overlay" role="dialog" aria-modal="true">' +
                '<div class="integrazo-wz-deactivate-modal">' +
                    '<div class="integrazo-wz-deactivate-header">' +
                        '<p class="integrazo-wz-deactivate-title">Deactivate Integration for Zoho Apps?</p>' +
                        '<p class="integrazo-wz-deactivate-subtitle">Please share why you\'re deactivating</p>' +
                        '<button class="integrazo-wz-deactivate-close" aria-label="Close">&times;</button>' +
                    '</div>' +
                    '<div class="integrazo-wz-deactivate-body">' +
                        '<div class="integrazo-wz-deactivate-spinner" id="integrazo-wz-deactivate-spinner"></div>' +
                        '<iframe class="integrazo-wz-deactivate-iframe" id="integrazo-wz-deactivate-iframe" frameborder="0" allowtransparency="true" scrolling="auto"></iframe>' +
                    '</div>' +
                    '<div class="integrazo-wz-deactivate-footer">' +
                        '<button class="integrazo-wz-deactivate-skip">Deactivate</button>' +
                    '</div>' +
                '</div>' +
            '</div>';

        document.body.insertAdjacentHTML('beforeend', html);

        var overlay  = document.getElementById('integrazo-wz-deactivate-overlay');
        var iframe   = document.getElementById('integrazo-wz-deactivate-iframe');
        var spinner  = document.getElementById('integrazo-wz-deactivate-spinner');
        var closeBtn = overlay.querySelector('.integrazo-wz-deactivate-close');
        var skipBtn  = overlay.querySelector('.integrazo-wz-deactivate-skip');

        iframe.addEventListener('load', function () {
            if (spinner) spinner.style.display = 'none';
        });
        iframe.src = 'https://creatorapp.zohopublic.in/plugcrux/support/form-embed/Integrazo_Deactivation_Feedback/p5gJMO9HVbGDSjHTpnrpnFWQThnQ53jSCgygr60wK3q8nwdaaPgJEMYExsT9A44GOynGXQCKRdePX3VCupA5eqFQN4P2MPb4PNHP?Plugin_Name=integrazo-integration-for-zoho-apps';

        closeBtn.addEventListener('click', integrazoWzDeactivateClose);

        skipBtn.addEventListener('click', function () {
            window.location.href = integrazoWzDeactivateUrl;
        });

        overlay.addEventListener('click', function (e) {
            if (e.target === overlay) {
                integrazoWzDeactivateClose();
            }
        });

        window.addEventListener('message', function (e) {
            if (e.data === 'integrazo_deactivated') {
                var activeOverlay = document.getElementById('integrazo-wz-deactivate-overlay');
                if (activeOverlay && activeOverlay.classList.contains('integrazo-wz-deactivate-visible')) {
                    window.location.href = integrazoWzDeactivateUrl;
                }
            }
        });
    }

    function integrazoWzDeactivateShow() {
        var overlay = document.getElementById('integrazo-wz-deactivate-overlay');
        if (overlay) {
            overlay.classList.add('integrazo-wz-deactivate-visible');
            document.addEventListener('keydown', integrazoWzDeactivateEsc);
        }
    }

    function integrazoWzDeactivateClose() {
        var overlay = document.getElementById('integrazo-wz-deactivate-overlay');
        if (overlay) {
            overlay.classList.remove('integrazo-wz-deactivate-visible');
        }
        document.removeEventListener('keydown', integrazoWzDeactivateEsc);
    }

    function integrazoWzDeactivateEsc(e) {
        if (e.key === 'Escape') {
            integrazoWzDeactivateClose();
        }
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', integrazoWzDeactivateInit);
    } else {
        integrazoWzDeactivateInit();
    }
})();
