(function () {
    var deferredPrompt = null;
    var installBanner = document.getElementById('install-banner');
    var installBtn = document.getElementById('install-app-btn');
    var dismissBtn = document.getElementById('install-dismiss-btn');
    var installLinks = [document.getElementById('hero-install-btn'), document.getElementById('open-install-link'), document.getElementById('app-install-chip')];
    var dismissedKey = 'pr_install_banner_dismissed';

    function showBanner() {
        if (!installBanner) {
            return;
        }
        if (window.matchMedia && window.matchMedia('(display-mode: standalone)').matches) {
            return;
        }
        if (localStorage.getItem(dismissedKey) === '1') {
            return;
        }
        installBanner.classList.remove('hidden');
        document.body.classList.add('install-available');
    }

    function hideBanner(persist) {
        if (!installBanner) {
            return;
        }
        installBanner.classList.add('hidden');
        document.body.classList.remove('install-available');
        if (persist) {
            localStorage.setItem(dismissedKey, '1');
        }
    }

    function isIos() {
        return /iphone|ipad|ipod/i.test(window.navigator.userAgent || '');
    }

    function isStandaloneIos() {
        return window.navigator.standalone === true;
    }

    function iosInstallHelp() {
        alert('No iPhone/iPad, abra o menu Compartilhar do Safari e escolha "Adicionar à Tela de Início".');
    }

    function triggerInstall() {
        if (deferredPrompt) {
            deferredPrompt.prompt();
            deferredPrompt.userChoice.then(function () {
                deferredPrompt = null;
                hideBanner(false);
            });
            return;
        }

        if (isIos() && !isStandaloneIos()) {
            iosInstallHelp();
            return;
        }

        alert('Abra o menu do navegador e procure a opção "Instalar app" ou "Adicionar à tela inicial".');
    }

    if ('serviceWorker' in navigator) {
        window.addEventListener('load', function () {
            navigator.serviceWorker.register((window.PREFEITURA_RESOLVE ? window.PREFEITURA_RESOLVE.baseUrl : '') + '/sw.js').catch(function () {});
        });
    }

    window.addEventListener('beforeinstallprompt', function (e) {
        e.preventDefault();
        deferredPrompt = e;
        showBanner();
    });

    window.addEventListener('appinstalled', function () {
        deferredPrompt = null;
        hideBanner(false);
        if (window.PREFEITURA_RESOLVE && window.PREFEITURA_RESOLVE.homeUrl) {
            setTimeout(function () {
                window.location.href = window.PREFEITURA_RESOLVE.homeUrl;
            }, 300);
        }
    });

    if (installBtn) {
        installBtn.addEventListener('click', triggerInstall);
    }

    if (dismissBtn) {
        dismissBtn.addEventListener('click', function () {
            hideBanner(true);
        });
    }

    for (var i = 0; i < installLinks.length; i++) {
        if (installLinks[i]) {
            installLinks[i].addEventListener('click', function (event) {
                event.preventDefault();
                triggerInstall();
            });
        }
    }

    if (isIos() && !isStandaloneIos() && installBanner && localStorage.getItem(dismissedKey) !== '1') {
        showBanner();
    }
})();
