<?php

namespace Blazemedia\OneSignal\Frontend;

/**
 * Frontend functionality class
 */
class Frontend
{
    protected $settings;

    /**
     * Initialize frontend functionality
     */
    public function __construct()    
    {

        $this->settings = $this->getSettings();

        add_action('wp_head', [ $this, 'addOneSignalScripts' ], 5);
    }


    /**
     * Enqueue scripts
     */
    public function addOneSignalScripts()
    {
        // Add DNS prefetch and preconnect for OneSignal CDN
        $this->addOneSignalPreload();
        $this->addOneSignalMeta();
        $this->loadSDK();
        $this->initJS();        
    }


    /**
     * Add OneSignal preload links
     */
    protected function addOneSignalPreload()
    {
        $prefetch = <<<HTML
            <link rel="dns-prefetch" href="https://cdn.onesignal.com" />
            <link rel="preconnect" href="https://cdn.onesignal.com" crossorigin >
        HTML;

        echo $prefetch;       
    }


    /**
     * Add Service Worker registration
     */
    protected function loadSDK() : void
    {
        $loadSDK = <<<HTML
            <!-- OneSignal SDK -->
            <script src="https://cdn.onesignal.com/sdks/web/v16/OneSignalSDK.page.js" defer></script>
        HTML;

        echo $loadSDK;
    }

    
    public function initJS() : void
    {	    
        $init = <<<JS
            
            // Initialize OneSignal		
            window.OneSignalDeferred = window.OneSignalDeferred || [];

            OneSignalDeferred.push(async function(OneSignal) {

                await OneSignal.init({
                    appId: "{$this->settings->appId}",                    
                    safari_web_id: "{$this->settings->safariWebId}",
                    autoResubscribe: true,
                    promptOptions: {
                        slidedown: {
                            prompts: [{
                                type: "push",
                                autoPrompt: false,
                                text: {
                                    actionMessage: "{$this->settings->promptMessage}",
                                    acceptButton : "{$this->settings->acceptButton}",
                                    cancelButton : "{$this->settings->cancelButton}",
                                },
                                delay: {
                                    pageViews: 0,
                                    timeDelay: {$this->settings->delaySeconds}
                                },
                            }]
                        }
                    },
                    
                });

                let permission = OneSignal.Notifications.permission;

                console.log( '__os permission', permission);

                if(!permission) {
                    OneSignal.Slidedown.promptPush();
                }
        
            });
        JS;

        echo "<script>{$init}</script>";
    }


    /**
     * Add OneSignal meta tags
     */
    public function addOneSignalMeta()
    {        
        echo '<meta name="onesignal-plugin" content="blazemedia/onesignal">'     . PHP_EOL;

        if ($app_id) {            
            echo '<meta name="onesignal-app-id" content="' . $this->settings->appId . '">'. PHP_EOL;
        }
    }


    protected function getSettings() {

        return (object) [

            'appId'         => get_option('onesignal_app_id' ),
            'safariWebId'   => get_option('onesignal_safari_web_id'),
            'promptMessage' => get_option('onesignal_prompt_message', 'Vorremmo inviarti notifiche sulle ultime novità pubblicate su questo sito'),
            'acceptButton'  => get_option('onesignal_accept_button', 'CONTINUA'),
            'cancelButton'  => get_option('onesignal_cancel_button', 'NO GRAZIE'),
            'delaySeconds'  => get_option('onesignal_delay_seconds', '10')
        ];
    }
} 