<?php
/**
 * Nexter Enqueue Styles And Scripts
 *
 * @package    Nexter
 * @since    1.0.0
 */
if (!defined('ABSPATH')) {
    exit;
}

if (!class_exists('Nexter_Fse_Load_Enqueue')) {

    class Nexter_Fse_Load_Enqueue {

        /**
         * Constructor
         */
        public function __construct() {
            if (!defined('NEXTER_EXT') && empty(get_option('nxt-fse-load-notice'))) {
                add_action('admin_notices', array($this, 'nxt_fse_extension_load_notice'));
                add_action('wp_ajax_nexter_fse_ext_dismiss_notice', array($this, 'nexter_fse_ext_dismiss_notice_ajax'));
            }
            add_action('wp_enqueue_scripts', array($this, 'nxt_fse_enqueue_scripts'), 1);
//            This Hook is not working. enqueue_block_assets is added
            add_action('enqueue_block_editor_assets', array($this, 'nxt_fse_gutenberg_assets_styles'));
//            enqueue_block_editor_assets is not working
            add_action('enqueue_block_assets', array($this, 'nxt_fse_gutenberg_assets_styles'));
            add_action('admin_enqueue_scripts', array($this, 'nxt_fse_enqueue_scripts_admin'), 1);


//            Add JS for Responsive Settings
            add_action('enqueue_block_editor_assets', array($this, 'nexter_fse_editor_assets'), 1);

        }

        /**
         * Theme Load Css And Js
         */
        public function nxt_fse_enqueue_scripts() {

            $minified = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
            //todo Check if this can be removed
            //Load Style
            wp_enqueue_style(
                'nexter-fse-style',
                get_stylesheet_uri(),
                [],
                NEXTER_FSE_VERSION
            );

            wp_enqueue_style(
                'nexter-fse-blocks-shared-style',
                get_theme_file_uri('assets/css/all.min.css'),
                [],
                NEXTER_FSE_VERSION
            );
        }

        public function nexter_fse_editor_assets(){
            wp_enqueue_script('nexter-fse-editor', NEXTER_FSE_URI . 'build/responsive.js', ['wp-edit-post']);
        }

        /**
         * Enqueue Gutenberg assets style.
         */
        public function nxt_fse_gutenberg_assets_styles() {
            wp_enqueue_style(
                'nexter-fse-blocks-shared-style',
                get_theme_file_uri('assets/css/all.min.css'),
                [],
                NEXTER_FSE_VERSION
            );
        }

        /**
         * Theme Load Admin Css And Js
         * @since 1.0.8
         */
        public function nxt_fse_enqueue_scripts_admin($hook_suffix) {
            // Use minified libraries if SCRIPT_DEBUG is turned off
            $minified = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';

            //todo relogic
            wp_enqueue_style('nxt-fse-admin-css', NEXTER_FSE_CSS_URI . 'admin/nexter-admin' . $minified . '.css', array(), NEXTER_FSE_VERSION);

            wp_enqueue_script('nxt-fse-admin-js', NEXTER_FSE_JS_URI . 'admin/nexter-admin' . $minified . '.js', array(), NEXTER_FSE_VERSION, false);

            $nexter_admin_localize = array(
                'ajaxurl' => admin_url('admin-ajax.php'),
                'ajax_nonce' => wp_create_nonce('nexter_fse_admin_nonce'),
                'nexter_path' => NEXTER_FSE_URI . 'assets/',
                'is_pro' => (defined('NXT_PRO_EXT')) ? true : false,
            );

            wp_localize_script('nxt-fse-admin-js', 'nexter_admin_config', $nexter_admin_localize);

            if (!did_action('wp_enqueue_media')) {
                wp_enqueue_media();
            }

            if (!is_customize_preview()) {
                wp_enqueue_style('wp-color-picker');
                wp_register_script('nexter-fse-panel-setting', NEXTER_FSE_JS_URI . 'admin/nexter-panel-settings' . $minified . '.js', array('wp-util', 'updates', 'wp-color-picker'), NEXTER_FSE_VERSION, false);
            }

            $js_handle = apply_filters('nexter_admin_script_handles', array('jquery', 'wp-color-picker'));
            if (is_customize_preview() === true) {
                $js_handle[] = 'customize-base';
            }

            wp_enqueue_style('nxt-fse-metabox-editor', NEXTER_FSE_CSS_URI . 'admin/metabox-editor-style' . $minified . '.css', array());

        }

        /**
         * Nexter Extension Load Notice
         */
        public function nxt_fse_extension_load_notice() {
            $plugin = 'nexter-extension/nexter-extension.php';
            if ($this->nexter_fse_extension_activate()) {
                if (!current_user_can('activate_plugins')) {
                    return;
                }
                $activation_url = wp_nonce_url('plugins.php?action=activate&amp;plugin=' . $plugin . '&amp;plugin_status=all&amp;paged=1&amp;s', 'activate-plugin_' . $plugin);
                $admin_notice = '<h4 class="nxt-notice-head">' . esc_html__('Activate Nexter Extension Now !!!', 'nexter-fse') . '</h4>';
                $admin_notice .= '<p>' . esc_html__('Finally, You are Done Installing Nexter Theme & Extension as Well. Now It’s Time to Press the Pedal. Activate Nexter Extension and Get Over With it.', 'nexter-fse') . '</p>';
                $admin_notice .= '<p>' . sprintf('<a href="%s" class="button-primary">%s</a>', $activation_url, esc_html__('Activate Nexter Extension', 'nexter-fse')) . '</p>';
            } else {
                if (!current_user_can('install_plugins')) {
                    return;
                }
                $install_url = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=nexter-extension'), 'install-plugin_nexter-extension');
                $admin_notice = '<h4 class="nxt-notice-head">' . esc_html__('It’s Time to Install Nexter Extension', 'nexter-fse') . '</h4>';
                $admin_notice .= '<p>' . esc_html__('Now You’ve Already Installed Nexter Theme, You Need to Install Nexter Extension in Order to Get the Most of out From it. Nexter Extension is an Ultimate Solution to Your Page Building Experience Using Templates.', 'nexter-fse') . sprintf(' <a href="%s" target="_blank" rel="noopener noreferrer" >%s</a>', esc_url('https://nexterwp.com'), esc_html__('Visit Here', 'nexter-fse')) . esc_html__(' to Learn More About Nexter Extension.', 'nexter-fse') . '</p>';
                $admin_notice .= '<p>' . sprintf('<a href="%s" class="button-primary">%s</a>', $install_url, esc_html__('Install Nexter Extension', 'nexter-fse')) . '</p>';
            }
            echo '<div class="notice notice-info nexter-ext-notice is-dismissible">' . wp_kses_post($admin_notice) . '</div>';
        }

        /**
         * Check Activate Or Not Nexter Extension
         */
        public function nexter_fse_extension_activate() {
            $file_path = 'nexter-extension/nexter-extension.php';
            $installed_plugins = get_plugins();

            return isset($installed_plugins[$file_path]);
        }

        /**
         * Nexter Notice Dismiss Ajax
         */
        public function nexter_fse_ext_dismiss_notice_ajax() {
            check_ajax_referer('nexter_fse_admin_nonce', 'nexter_nonce');

            update_option('nxt-fse-load-notice', 1);
        }

    }

    new Nexter_Fse_Load_Enqueue();
}
