import { ApplicationRef, Injectable } from '@angular/core';
import { SwUpdate } from '@angular/service-worker';
import { TranslateService } from '@ngx-translate/core';
import { concat, interval } from 'rxjs';
import { first } from 'rxjs/operators';
import Swal from 'sweetalert2';

@Injectable({providedIn: 'root'})
export class UpdaterService {
  //https://angular.io/guide/service-worker-communications
  constructor(
    appRef: ApplicationRef,
    updates: SwUpdate,
    translate: TranslateService
  ) {
    if(!updates.isEnabled) return;

    // Allow the app to stabilize first, before starting
    // polling for updates with `interval()`.
    const appIsStable$ = appRef.isStable.pipe(first(isStable => isStable === true));
    const everySixHours$ = interval(6 * 60 * 60 * 1000);
    const everySixHoursOnceAppIsStable$ = concat(appIsStable$, everySixHours$);

    everySixHoursOnceAppIsStable$.subscribe(async () => {
      try {
        const updateFound = await updates.checkForUpdate();
        console.log(updateFound ? 'A new version is available.' : 'Already on the latest version.');
      } catch (err) {
        console.error('Failed to check for updates:', err);
      }
    });

    updates.unrecoverable.subscribe(event => {
      console.error(event);
      document.location.reload();
    });

    updates.versionUpdates.subscribe(evt => {
      switch (evt.type) {
        case 'VERSION_DETECTED':
          console.log(`Downloading new app version: ${evt.version.hash}`);
          break;
        case 'VERSION_READY':
          console.log(`Current app version: ${evt.currentVersion.hash}`);
          console.log(`New app version ready for use: ${evt.latestVersion.hash}`);
          Swal.fire({
            title: translate.instant('update_available'),
            text: translate.instant('update_available_text'),
            icon: 'info',
            showCancelButton: true,
            confirmButtonText: translate.instant('update_now'),
            cancelButtonText: translate.instant('cancel')
          }).then((result) => {
            if (result.isConfirmed) {
              document.location.reload();
            }
          });
          break;
        case 'VERSION_INSTALLATION_FAILED':
          console.log(`Failed to install app version '${evt.version.hash}': ${evt.error}`);
        break;
      }
    });
  }
}
