<?php
if ( ! class_exists( 'Comic_Ebook_Plugin_Activation_WPElemento_Importer' ) ) {
    /**
     * Comic_Ebook_Plugin_Activation_WPElemento_Importer initial setup
     *
     * @since 1.6.2
     */

    class Comic_Ebook_Plugin_Activation_WPElemento_Importer {

        private static $comic_ebook_instance;
        public $comic_ebook_action_count;
        public $comic_ebook_recommended_actions;

        /** Initiator **/
        public static function get_instance() {
          if ( ! isset( self::$comic_ebook_instance) ) {
            self::$comic_ebook_instance = new self();
          }
          return self::$comic_ebook_instance;
        }

        /*  Constructor */
        public function __construct() {

            add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );

            // ---------- wpelementoimpoter Plugin Activation -------
            add_filter( 'comic_ebook_recommended_plugins', array($this, 'comic_ebook_recommended_elemento_importer_plugins_array') );

            $comic_ebook_actions                   = $this->comic_ebook_get_recommended_actions();
            $this->comic_ebook_action_count        = $comic_ebook_actions['count'];
            $this->comic_ebook_recommended_actions = $comic_ebook_actions['actions'];

            add_action( 'wp_ajax_create_pattern_setup_builder', array( $this, 'create_pattern_setup_builder' ) );
        }

        public function comic_ebook_recommended_elemento_importer_plugins_array($comic_ebook_plugins){
            $comic_ebook_plugins[] = array(
                    'name'     => esc_html__('WPElemento Importer', 'comic-ebook'),
                    'slug'     =>  'wpelemento-importer',
                    'function' => 'WPElemento_Importer_ThemeWhizzie',
                    'desc'     => esc_html__('We highly recommend installing the WPElemento Importer plugin for importing the demo content with Elementor.', 'comic-ebook'),               
            );
            return $comic_ebook_plugins;
        }

        public function enqueue_scripts() {
            wp_enqueue_script('updates');      
            wp_register_script( 'comic-ebook-plugin-activation-script', esc_url(get_template_directory_uri()) . '/includes/getstart/js/plugin-activation.js', array('jquery') );
            wp_localize_script('comic-ebook-plugin-activation-script', 'comic_ebook_plugin_activate_plugin',
                array(
                    'installing' => esc_html__('Installing', 'comic-ebook'),
                    'activating' => esc_html__('Activating', 'comic-ebook'),
                    'error' => esc_html__('Error', 'comic-ebook'),
                    'ajax_url' => esc_url(admin_url('admin-ajax.php')),
                    'wpelementoimpoter_admin_url' => esc_url(admin_url('admin.php?page=wpelemento-importer-tgmpa-install-plugins')),
                    'addon_admin_url' => esc_url(admin_url('admin.php?page=wpelementoimporter-wizard'))
                )
            );
            wp_enqueue_script( 'comic-ebook-plugin-activation-script' );

        }

        // --------- Plugin Actions ---------
        public function comic_ebook_get_recommended_actions() {

            $comic_ebook_act_count  = 0;
            $comic_ebook_actions_todo = get_option( 'recommending_actions', array());

            $comic_ebook_plugins = $this->comic_ebook_get_recommended_plugins();

            if ($comic_ebook_plugins) {
                foreach ($comic_ebook_plugins as $comic_ebook_key => $comic_ebook_plugin) {
                    $comic_ebook_action = array();
                    if (!isset($comic_ebook_plugin['slug'])) {
                        continue;
                    }

                    $comic_ebook_action['id']   = 'install_' . $comic_ebook_plugin['slug'];
                    $comic_ebook_action['desc'] = '';
                    if (isset($comic_ebook_plugin['desc'])) {
                        $comic_ebook_action['desc'] = $comic_ebook_plugin['desc'];
                    }

                    $comic_ebook_action['name'] = '';
                    if (isset($comic_ebook_plugin['name'])) {
                        $comic_ebook_action['title'] = $comic_ebook_plugin['name'];
                    }

                    $comic_ebook_link_and_is_done  = $this->comic_ebook_get_plugin_buttion($comic_ebook_plugin['slug'], $comic_ebook_plugin['name'], $comic_ebook_plugin['function']);
                    $comic_ebook_action['link']    = $comic_ebook_link_and_is_done['button'];
                    $comic_ebook_action['is_done'] = $comic_ebook_link_and_is_done['done'];
                    if (!$comic_ebook_action['is_done'] && (!isset($comic_ebook_actions_todo[$comic_ebook_action['id']]) || !$comic_ebook_actions_todo[$comic_ebook_action['id']])) {
                        $comic_ebook_act_count++;
                    }
                    $comic_ebook_recommended_actions[] = $comic_ebook_action;
                    $comic_ebook_actions_todo[]        = array('id' => $comic_ebook_action['id'], 'watch' => true);
                }
                return array('count' => $comic_ebook_act_count, 'actions' => $comic_ebook_recommended_actions);
            }

        }

        public function comic_ebook_get_recommended_plugins() {

            $comic_ebook_plugins = apply_filters('comic_ebook_recommended_plugins', array());
            return $comic_ebook_plugins;
        }

        public function comic_ebook_get_plugin_buttion($slug, $name, $function) {
                $comic_ebook_is_done      = false;
                $comic_ebook_button_html  = '';
                $comic_ebook_is_installed = $this->is_plugin_installed($slug);
                $comic_ebook_plugin_path  = $this->get_plugin_basename_from_slug($slug);
                $comic_ebook_is_activeted = (class_exists($function)) ? true : false;
                if (!$comic_ebook_is_installed) {
                    $comic_ebook_plugin_install_url = add_query_arg(
                        array(
                            'action' => 'install-plugin',
                            'plugin' => $slug,
                        ),
                        self_admin_url('update.php')
                    );
                    $comic_ebook_plugin_install_url = wp_nonce_url($comic_ebook_plugin_install_url, 'install-plugin_' . esc_attr($slug));
                    $comic_ebook_button_html        = sprintf('<a class="comic-ebook-plugin-install install-now button-secondary button" data-slug="%1$s" href="%2$s" aria-label="%3$s" data-name="%4$s">%5$s</a>',
                        esc_attr($slug),
                        esc_url($comic_ebook_plugin_install_url),
                        sprintf(esc_html__('Install %s Now', 'comic-ebook'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Install & Activate', 'comic-ebook')
                    );
                } elseif ($comic_ebook_is_installed && !$comic_ebook_is_activeted) {

                    $comic_ebook_plugin_activate_link = add_query_arg(
                        array(
                            'action'        => 'activate',
                            'plugin'        => rawurlencode($comic_ebook_plugin_path),
                            'plugin_status' => 'all',
                            'paged'         => '1',
                            '_wpnonce'      => wp_create_nonce('activate-plugin_' . $comic_ebook_plugin_path),
                        ), self_admin_url('plugins.php')
                    );

                    $comic_ebook_button_html = sprintf('<a class="comic-ebook-plugin-activate activate-now button-primary button" data-slug="%1$s" href="%2$s" aria-label="%3$s" data-name="%4$s">%5$s</a>',
                        esc_attr($slug),
                        esc_url($comic_ebook_plugin_activate_link),
                        sprintf(esc_html__('Activate %s Now', 'comic-ebook'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Activate', 'comic-ebook')
                    );
                } elseif ($comic_ebook_is_activeted) {
                    $comic_ebook_button_html = sprintf('<div class="action-link button disabled"><span class="dashicons dashicons-yes"></span> %s</div>', esc_html__('Active', 'comic-ebook'));
                    $comic_ebook_is_done     = true;
                }

                return array('done' => $comic_ebook_is_done, 'button' => $comic_ebook_button_html);
            }
        public function is_plugin_installed($slug) {
            $comic_ebook_installed_plugins = $this->get_installed_plugins(); // Retrieve a list of all installed plugins (WP cached).
            $comic_ebook_file_path         = $this->get_plugin_basename_from_slug($slug);
            return (!empty($comic_ebook_installed_plugins[$comic_ebook_file_path]));
        }
        public function get_plugin_basename_from_slug($slug) {
            $comic_ebook_keys = array_keys($this->get_installed_plugins());
            foreach ($comic_ebook_keys as $comic_ebook_key) {
                if (preg_match('|^' . $slug . '/|', $comic_ebook_key)) {
                    return $comic_ebook_key;
                }
            }
            return $slug;
        }

        public function get_installed_plugins() {

            if (!function_exists('get_plugins')) {
                require_once ABSPATH . 'wp-admin/includes/plugin.php';
            }

            return get_plugins();
        }
        public function create_pattern_setup_builder() {

            $edit_page = admin_url().'post-new.php?post_type=page&create_pattern=true';
            echo json_encode(['page_id'=>'','edit_page_url'=> $edit_page ]);

            exit;
        }

    }
}
/**
 * Kicking this off by calling 'get_instance()' method
 */
Comic_Ebook_Plugin_Activation_WPElemento_Importer::get_instance();