function loadMainDartJs() {
  if (scriptLoaded) {
    return;
  }
  //hideSplash();
  scriptLoaded = true;

  // document.getElementsByTagName('base')[0].setAttribute("href", window.STATIC_ROOT);

  var scriptTag = document.createElement('script');
  scriptTag.src = window.STATIC_ROOT + 'main.dart.js?v=' + serviceWorkerVersion;
  scriptTag.type = 'application/javascript';
  document.body.append(scriptTag);
}

function loadVersion(callback) {
  fetch('version.json?d=' + serviceWorkerVersion)
    .then(function (res) {
      return res.json();
    })
    .then(function (data) {
      window.FLYINSONO_VERSION = data;
      callback && callback();
    });
}

///外部通知
function externalNotification(a, b) {
  window.invokeJs(a, b);
}

///打印pdf
function printPdfByBase64(base64) {
  printJS({
    printable: base64,
    type: 'pdf',
    base64: true,
  });
}

function closeWindow(windowType) {
  var shell = window['FisShellApi'];
  if (shell) {
    shell.closeWindow(windowType);
  }
}

function getLanguageCode(callback) {
  var shell = window['FisShellApi'];
  if (shell) {
    var request = shell.getLanguageCode();
    request.then((v) => callback(v));
  }
}

function closeSlaveWindow() {
  var shell = window['FisShellApi'];
  if (shell) {
    shell.closeSlaveWindow();
  }
}

function beginWindowDrag(name) {
  var shell = window['FisShellApi'];
  if (shell) {
    shell.beginWindowDrag(name);
  }
}

function endWindowDrag(name) {
  var shell = window['FisShellApi'];
  if (shell) {
    shell.endWindowDrag(name);
  }
}

function closeWindow(name) {
  var shell = window['FisShellApi'];
  if (shell) {
    shell.closeWindow(name);
  }
}

function maximizeWindow(name) {
  var shell = window['FisShellApi'];
  if (shell) {
    shell.maximizeWindow(name);
  }
}

function minimizeWindow(name) {
  var shell = window['FisShellApi'];
  if (shell) {
    shell.minimizeWindow(name);
  }
}

function hideSplash() {
  console.log('onhide splash');
  var splash = document.getElementsByClassName('splash')[0];

  setTimeout(function () {
    splash.style.zIndex = 0;
  });
  var loading = document.getElementsByClassName('full-screen-center')[0];
  setTimeout(function () {
    loading.style.zIndex = 0;
  });
}

window.addEventListener('flutter-first-frame', function () {
  console.log('flutter-first-frame');
  hideSplash();
});

function loadService() {
  if ('serviceWorker' in navigator) {
    // Service workers are supported. Use them.
    window.addEventListener('load', function () {
      // Wait for registration to finish before dropping the <script> tag.
      // Otherwise, the browser will load the script multiple times,
      // potentially different versions.
      var serviceWorkerUrl =
        './flutter_service_worker.js?v=' + serviceWorkerVersion;
      navigator.serviceWorker.register(serviceWorkerUrl).then((reg) => {
        function waitForActivation(serviceWorker) {
          serviceWorker.addEventListener('statechange', () => {
            if (serviceWorker.state == 'activated') {
              console.log('Installed new service worker.');
              loadMainDartJs();
            }
          });
        }
        if (!reg.active && (reg.installing || reg.waiting)) {
          // No active web worker and we have installed or are installing
          // one for the first time. Simply wait for it to activate.
          waitForActivation(reg.installing || reg.waiting);
        } else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
          // When the app updates the serviceWorkerVersion changes, so we
          // need to ask the service worker to update.
          console.log('New service worker available.');
          reg.update().then((data) => {
            var worker = data.installing || data.waiting || data.active;
            waitForActivation(worker);
          });
          // waitForActivation(reg.installing);
        } else {
          // Existing service worker is still good.
          console.log('Loading app from service worker.');
          loadMainDartJs();
        }
      });

      // If service worker doesn't succeed in a reasonable amount of time,
      // fallback to plaint <script> tag.
      setTimeout(() => {
        if (!scriptLoaded) {
          console.warn(
            'Failed to load app from service worker. Falling back to plain <script> tag.',
          );
          loadMainDartJs();
        }
      }, 4000);
    });
  } else {
    // Service workers not supported. Just drop the <script> tag.
    loadMainDartJs();
  }
}

function kIsWechat() {
  return window.__wxjs_environment === 'miniprogram';
}
