
(function () {
  var installPrompt = null;
  var installChip = document.getElementById('mobile-install-chip');
  var installSheet = document.getElementById('install-sheet');
  var installNowBtn = document.getElementById('install-now-btn');
  var installLaterBtn = document.getElementById('install-later-btn');
  var form = document.getElementById('citizen-mobile-form');
  var submitBtn = document.getElementById('mobile-submit-btn');
  var feedback = document.getElementById('form-feedback');

  function isStandalone() {
    return (window.matchMedia && window.matchMedia('(display-mode: standalone)').matches) || window.navigator.standalone === true;
  }

  function showInstallControls() {
    if (isStandalone()) return;
    if (installChip) installChip.classList.remove('hidden');
  }

  function openInstallSheet() {
    if (!installSheet) return;
    installSheet.classList.add('show');
  }

  function closeInstallSheet() {
    if (!installSheet) return;
    installSheet.classList.remove('show');
  }

  function triggerInstall() {
    if (installPrompt) {
      installPrompt.prompt();
      installPrompt.userChoice.then(function () {
        installPrompt = null;
        closeInstallSheet();
        if (installChip) installChip.classList.add('hidden');
      });
      return;
    }

    var isiOS = /iphone|ipad|ipod/i.test(window.navigator.userAgent || '');
    if (isiOS) {
      alert('No iPhone/iPad, abra Compartilhar no Safari e toque em "Adicionar à Tela de Início".');
      return;
    }

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

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

  window.addEventListener('beforeinstallprompt', function (e) {
    e.preventDefault();
    installPrompt = e;
    showInstallControls();
  });

  window.addEventListener('appinstalled', function () {
    installPrompt = null;
    closeInstallSheet();
    if (installChip) installChip.classList.add('hidden');
  });

  if (installChip) {
    installChip.addEventListener('click', function () {
      openInstallSheet();
    });
  }

  if (installNowBtn) installNowBtn.addEventListener('click', triggerInstall);
  if (installLaterBtn) installLaterBtn.addEventListener('click', closeInstallSheet);

  document.addEventListener('click', function (event) {
    var target = event.target;
    if (target && target.matches && target.matches('[data-open-install]')) {
      event.preventDefault();
      openInstallSheet();
    }
  });

  if (form && submitBtn) {
    form.addEventListener('submit', function () {
      submitBtn.classList.add('loading');
      submitBtn.setAttribute('disabled', 'disabled');
      submitBtn.innerHTML = 'Enviando ocorrência...';
      if (feedback) {
        feedback.style.display = 'block';
        feedback.innerHTML = 'Estamos enviando sua ocorrência. Aguarde alguns segundos.';
      }
    });
  }

  if (document.body.classList.contains('public-body')) {
    showInstallControls();
  }
})();
