<?php

namespace BWDK\Front;

use BWDK\Support\Constant;

class Assets
{

    public $options;
    public function register(): void
    {
        $this->options = get_option(Constant::BWDK_OPTION_NAME);

        add_action('wp_enqueue_scripts', [$this, 'enqueue']);
        add_action( 'wp_head', [$this, 'bwdk_load_custom_css'],9999 );
        add_action( 'wp_footer', [$this, 'bwdk_load_custom_js'],9999 );

    }

    public function enqueue(): void
    {




        wp_enqueue_style(
            'bwdk-front-style',
            BWDK_PUBLIC_CSS . 'bwdk-public.css',
            [],
            '1.0.0'
        );


        wp_enqueue_script(
            'bwdk-front-script',
            BWDK_PUBLIC_JS . 'bwdk-public.js',
            ['jquery'],
            '1.0.0',
            true
        );

        wp_enqueue_script(
            'digify-widget-sdk',
            'https://digifycdn.com/front-statics/digify-bwdk-ui-kit/widget-sdk.min.js',
            [ 'bwdk-front-script' ],
            '1.0.0',
            true
        );

        $digify_logo   = get_site_icon_url( 512 ) ?: '';
        $digify_store  = get_bloginfo( 'name' );
        $digify_inline = '(function(){';
        $digify_inline .= 'var opts={';
        $digify_inline .= 'logo:' . wp_json_encode( $digify_logo ) . ',';
        $digify_inline .= 'storeName:' . wp_json_encode( $digify_store ) . ',';
        $digify_inline .= 'onButtonClick:function(){try{console.debug("[bwdk] DigifyWidget onButtonClick");}catch(e){}},';
        $digify_inline .= 'onBoxClick:function(){try{console.debug("[bwdk] DigifyWidget onBoxClick");}catch(e){}}';
        $digify_inline .= '};';
        $digify_inline .= 'function tryInit(){';
        $digify_inline .= 'if(window.__bwdkDigifyWidgetInitDone)return;';
        $digify_inline .= 'var Widget=window?.DigifyWidget?.default??window?.DigifyWidget;';
        $digify_inline .= 'if(!Widget||typeof Widget.init!=="function")return;';
        $digify_inline .= 'Widget.init(opts);';
        $digify_inline .= 'window.__bwdkDigifyWidgetInitDone=true;';
        $digify_inline .= '}';
        $digify_inline .= 'tryInit();';
        $digify_inline .= 'var el=document.querySelector(\'script[src*="widget-sdk"]\');';
        $digify_inline .= 'if(el){el.addEventListener("load",tryInit);}';
        $digify_inline .= 'window.addEventListener("load",tryInit);';
        $digify_inline .= '})();';
        wp_add_inline_script( 'digify-widget-sdk', $digify_inline, 'after' );




        wp_localize_script('bwdk-front-script', 'bwdk_ajax', [
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce'    => wp_create_nonce('bwdk_nonce')
        ]);

    }
    function bwdk_load_custom_css() {
        $custom_css = $this->options["custom"]["css"]??"";
        if ( ! empty( $custom_css ) ) {
            echo '<style>' . wp_strip_all_tags( $custom_css ) . '</style>';
        }
    }

    function bwdk_load_custom_js() {
        $custom_js = $this->options["custom"]["js"]??"";
        if ( ! empty( $custom_js ) ) {
            echo '<script>' . wp_strip_all_tags( $custom_js ) . '</script>';
        }
    }

}
