<?php

class pwa
{
    private static $serviceWorker = "serviceWorker.js";

    private static function getManifeste()
    {
        switch ($_SERVER['HTTP_HOST']) {
            case URL_PROD_CONTROL:
                return "manifest-control.json";
                break;
            case URL_PROD_EVENTS:
                return "manifest-events.json";
                break;
            case URL_PROD_CMS:
                return "manifest-cms.json";
                break;
            case URL_DEV_CONTROL:
                return "manifest-control-dev.json";
                break;
            case URL_DEV_EVENTS:
                return "manifest-events-dev.json";
                break;
            case URL_DEV_CMS:
                return "manifest-cms-dev.json";
                break;
        }
    }

    public static function printServiceWorker()
    {
        echo '<script>';
        if (PWA == 1) {
            echo '  if("serviceWorker" in navigator){
                        navigator.serviceWorker.register("/' . self::$serviceWorker . '");
                    }';
        } else {
            echo '  navigator.serviceWorker.getRegistrations().then(function(registrations) {
                        for(let registration of registrations) {
                            registration.unregister();
                        } 
                    });';
        }
        echo '</script>';
    }

    public static function printManifeste()
    {
        if (PWA == 1) {
            echo '<link rel="manifest" href="' . self::getManifeste() . '">';
        }
    }
}
