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

    class Cafe_Elementor_Plugin_Activation_WPElemento_Importer {

        private static $cafe_elementor_instance;
        public $cafe_elementor_action_count;
        public $cafe_elementor_recommended_actions;

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

        /*  Constructor */
        public function __construct() {

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

            // ---------- wpelementoimpoter Plugin Activation -------
            add_filter( 'cafe_elementor_recommended_plugins', array($this, 'cafe_elementor_recommended_elemento_importer_plugins_array') );

            $cafe_elementor_actions                   = $this->cafe_elementor_get_recommended_actions();
            $this->cafe_elementor_action_count        = $cafe_elementor_actions['count'];
            $this->cafe_elementor_recommended_actions = $cafe_elementor_actions['actions'];

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

        public function cafe_elementor_recommended_elemento_importer_plugins_array($cafe_elementor_plugins){
            $cafe_elementor_plugins[] = array(
                    'name'     => esc_html__('WPElemento Importer', 'cafe-elementor'),
                    '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.', 'cafe-elementor'),               
            );
            return $cafe_elementor_plugins;
        }

        public function enqueue_scripts() {
            wp_enqueue_script('updates');      
            wp_register_script( 'cafe-elementor-plugin-activation-script', esc_url(get_template_directory_uri()) . '/includes/getstart/js/plugin-activation.js', array('jquery') );
            wp_localize_script('cafe-elementor-plugin-activation-script', 'cafe_elementor_plugin_activate_plugin',
                array(
                    'installing' => esc_html__('Installing', 'cafe-elementor'),
                    'activating' => esc_html__('Activating', 'cafe-elementor'),
                    'error' => esc_html__('Error', 'cafe-elementor'),
                    '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( 'cafe-elementor-plugin-activation-script' );

        }

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

            $cafe_elementor_act_count  = 0;
            $cafe_elementor_actions_todo = get_option( 'recommending_actions', array());

            $cafe_elementor_plugins = $this->cafe_elementor_get_recommended_plugins();

            if ($cafe_elementor_plugins) {
                foreach ($cafe_elementor_plugins as $cafe_elementor_key => $cafe_elementor_plugin) {
                    $cafe_elementor_action = array();
                    if (!isset($cafe_elementor_plugin['slug'])) {
                        continue;
                    }

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

                    $cafe_elementor_action['name'] = '';
                    if (isset($cafe_elementor_plugin['name'])) {
                        $cafe_elementor_action['title'] = $cafe_elementor_plugin['name'];
                    }

                    $cafe_elementor_link_and_is_done  = $this->cafe_elementor_get_plugin_buttion($cafe_elementor_plugin['slug'], $cafe_elementor_plugin['name'], $cafe_elementor_plugin['function']);
                    $cafe_elementor_action['link']    = $cafe_elementor_link_and_is_done['button'];
                    $cafe_elementor_action['is_done'] = $cafe_elementor_link_and_is_done['done'];
                    if (!$cafe_elementor_action['is_done'] && (!isset($cafe_elementor_actions_todo[$cafe_elementor_action['id']]) || !$cafe_elementor_actions_todo[$cafe_elementor_action['id']])) {
                        $cafe_elementor_act_count++;
                    }
                    $cafe_elementor_recommended_actions[] = $cafe_elementor_action;
                    $cafe_elementor_actions_todo[]        = array('id' => $cafe_elementor_action['id'], 'watch' => true);
                }
                return array('count' => $cafe_elementor_act_count, 'actions' => $cafe_elementor_recommended_actions);
            }

        }

        public function cafe_elementor_get_recommended_plugins() {

            $cafe_elementor_plugins = apply_filters('cafe_elementor_recommended_plugins', array());
            return $cafe_elementor_plugins;
        }

        public function cafe_elementor_get_plugin_buttion($slug, $name, $function) {
                $cafe_elementor_is_done      = false;
                $cafe_elementor_button_html  = '';
                $cafe_elementor_is_installed = $this->is_plugin_installed($slug);
                $cafe_elementor_plugin_path  = $this->get_plugin_basename_from_slug($slug);
                $cafe_elementor_is_activeted = (class_exists($function)) ? true : false;
                if (!$cafe_elementor_is_installed) {
                    $cafe_elementor_plugin_install_url = add_query_arg(
                        array(
                            'action' => 'install-plugin',
                            'plugin' => $slug,
                        ),
                        self_admin_url('update.php')
                    );
                    $cafe_elementor_plugin_install_url = wp_nonce_url($cafe_elementor_plugin_install_url, 'install-plugin_' . esc_attr($slug));
                    $cafe_elementor_button_html        = sprintf('<a class="cafe-elementor-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($cafe_elementor_plugin_install_url),
                        sprintf(esc_html__('Install %s Now', 'cafe-elementor'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Install & Activate', 'cafe-elementor')
                    );
                } elseif ($cafe_elementor_is_installed && !$cafe_elementor_is_activeted) {

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

                    $cafe_elementor_button_html = sprintf('<a class="cafe-elementor-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($cafe_elementor_plugin_activate_link),
                        sprintf(esc_html__('Activate %s Now', 'cafe-elementor'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Activate', 'cafe-elementor')
                    );
                } elseif ($cafe_elementor_is_activeted) {
                    $cafe_elementor_button_html = sprintf('<div class="action-link button disabled"><span class="dashicons dashicons-yes"></span> %s</div>', esc_html__('Active', 'cafe-elementor'));
                    $cafe_elementor_is_done     = true;
                }

                return array('done' => $cafe_elementor_is_done, 'button' => $cafe_elementor_button_html);
            }
        public function is_plugin_installed($slug) {
            $cafe_elementor_installed_plugins = $this->get_installed_plugins(); // Retrieve a list of all installed plugins (WP cached).
            $cafe_elementor_file_path         = $this->get_plugin_basename_from_slug($slug);
            return (!empty($cafe_elementor_installed_plugins[$cafe_elementor_file_path]));
        }
        public function get_plugin_basename_from_slug($slug) {
            $cafe_elementor_keys = array_keys($this->get_installed_plugins());
            foreach ($cafe_elementor_keys as $cafe_elementor_key) {
                if (preg_match('|^' . $slug . '/|', $cafe_elementor_key)) {
                    return $cafe_elementor_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
 */
Cafe_Elementor_Plugin_Activation_WPElemento_Importer::get_instance();