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

    class Event_Organizer_Elemento_Plugin_Activation_WPElemento_Importer {

        private static $event_organizer_elemento_instance;
        public $event_organizer_elemento_action_count;
        public $event_organizer_elemento_recommended_actions;

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

        /*  Constructor */
        public function __construct() {

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

            // ---------- wpelementoimpoter Plugin Activation -------
            add_filter( 'event_organizer_elemento_recommended_plugins', array($this, 'event_organizer_elemento_recommended_elemento_importer_plugins_array') );

            $event_organizer_elemento_actions                   = $this->event_organizer_elemento_get_recommended_actions();
            $this->event_organizer_elemento_action_count        = $event_organizer_elemento_actions['count'];
            $this->event_organizer_elemento_recommended_actions = $event_organizer_elemento_actions['actions'];

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

        public function event_organizer_elemento_recommended_elemento_importer_plugins_array($event_organizer_elemento_plugins){
            $event_organizer_elemento_plugins[] = array(
                    'name'     => esc_html__('WPElemento Importer', 'event-organizer-elemento'),
                    '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.', 'event-organizer-elemento'),               
            );
            return $event_organizer_elemento_plugins;
        }

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

        }

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

            $event_organizer_elemento_act_count  = 0;
            $event_organizer_elemento_actions_todo = get_option( 'recommending_actions', array());

            $event_organizer_elemento_plugins = $this->event_organizer_elemento_get_recommended_plugins();

            if ($event_organizer_elemento_plugins) {
                foreach ($event_organizer_elemento_plugins as $event_organizer_elemento_key => $event_organizer_elemento_plugin) {
                    $event_organizer_elemento_action = array();
                    if (!isset($event_organizer_elemento_plugin['slug'])) {
                        continue;
                    }

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

                    $event_organizer_elemento_action['name'] = '';
                    if (isset($event_organizer_elemento_plugin['name'])) {
                        $event_organizer_elemento_action['title'] = $event_organizer_elemento_plugin['name'];
                    }

                    $event_organizer_elemento_link_and_is_done  = $this->event_organizer_elemento_get_plugin_buttion($event_organizer_elemento_plugin['slug'], $event_organizer_elemento_plugin['name'], $event_organizer_elemento_plugin['function']);
                    $event_organizer_elemento_action['link']    = $event_organizer_elemento_link_and_is_done['button'];
                    $event_organizer_elemento_action['is_done'] = $event_organizer_elemento_link_and_is_done['done'];
                    if (!$event_organizer_elemento_action['is_done'] && (!isset($event_organizer_elemento_actions_todo[$event_organizer_elemento_action['id']]) || !$event_organizer_elemento_actions_todo[$event_organizer_elemento_action['id']])) {
                        $event_organizer_elemento_act_count++;
                    }
                    $event_organizer_elemento_recommended_actions[] = $event_organizer_elemento_action;
                    $event_organizer_elemento_actions_todo[]        = array('id' => $event_organizer_elemento_action['id'], 'watch' => true);
                }
                return array('count' => $event_organizer_elemento_act_count, 'actions' => $event_organizer_elemento_recommended_actions);
            }

        }

        public function event_organizer_elemento_get_recommended_plugins() {

            $event_organizer_elemento_plugins = apply_filters('event_organizer_elemento_recommended_plugins', array());
            return $event_organizer_elemento_plugins;
        }

        public function event_organizer_elemento_get_plugin_buttion($slug, $name, $function) {
                $event_organizer_elemento_is_done      = false;
                $event_organizer_elemento_button_html  = '';
                $event_organizer_elemento_is_installed = $this->is_plugin_installed($slug);
                $event_organizer_elemento_plugin_path  = $this->get_plugin_basename_from_slug($slug);
                $event_organizer_elemento_is_activeted = (class_exists($function)) ? true : false;
                if (!$event_organizer_elemento_is_installed) {
                    $event_organizer_elemento_plugin_install_url = add_query_arg(
                        array(
                            'action' => 'install-plugin',
                            'plugin' => $slug,
                        ),
                        self_admin_url('update.php')
                    );
                    $event_organizer_elemento_plugin_install_url = wp_nonce_url($event_organizer_elemento_plugin_install_url, 'install-plugin_' . esc_attr($slug));
                    $event_organizer_elemento_button_html        = sprintf('<a class="event-organizer-elemento-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($event_organizer_elemento_plugin_install_url),
                        sprintf(esc_html__('Install %s Now', 'event-organizer-elemento'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Install & Activate', 'event-organizer-elemento')
                    );
                } elseif ($event_organizer_elemento_is_installed && !$event_organizer_elemento_is_activeted) {

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

                    $event_organizer_elemento_button_html = sprintf('<a class="event-organizer-elemento-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($event_organizer_elemento_plugin_activate_link),
                        sprintf(esc_html__('Activate %s Now', 'event-organizer-elemento'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Activate', 'event-organizer-elemento')
                    );
                } elseif ($event_organizer_elemento_is_activeted) {
                    $event_organizer_elemento_button_html = sprintf('<div class="action-link button disabled"><span class="dashicons dashicons-yes"></span> %s</div>', esc_html__('Active', 'event-organizer-elemento'));
                    $event_organizer_elemento_is_done     = true;
                }

                return array('done' => $event_organizer_elemento_is_done, 'button' => $event_organizer_elemento_button_html);
            }
        public function is_plugin_installed($slug) {
            $event_organizer_elemento_installed_plugins = $this->get_installed_plugins(); // Retrieve a list of all installed plugins (WP cached).
            $event_organizer_elemento_file_path         = $this->get_plugin_basename_from_slug($slug);
            return (!empty($event_organizer_elemento_installed_plugins[$event_organizer_elemento_file_path]));
        }
        public function get_plugin_basename_from_slug($slug) {
            $event_organizer_elemento_keys = array_keys($this->get_installed_plugins());
            foreach ($event_organizer_elemento_keys as $event_organizer_elemento_key) {
                if (preg_match('|^' . $slug . '/|', $event_organizer_elemento_key)) {
                    return $event_organizer_elemento_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
 */
Event_Organizer_Elemento_Plugin_Activation_WPElemento_Importer::get_instance();