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

    class Travel_Hiking_Plugin_Activation_WPElemento_Importer {

        private static $travel_hiking_instance;
        public $travel_hiking_action_count;
        public $travel_hiking_recommended_actions;

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

        /*  Constructor */
        public function __construct() {

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

            // ---------- wpelementoimpoter Plugin Activation -------
            add_filter( 'travel_hiking_recommended_plugins', array($this, 'travel_hiking_recommended_elemento_importer_plugins_array') );

            $travel_hiking_actions                   = $this->travel_hiking_get_recommended_actions();
            $this->travel_hiking_action_count        = $travel_hiking_actions['count'];
            $this->travel_hiking_recommended_actions = $travel_hiking_actions['actions'];

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

        public function travel_hiking_recommended_elemento_importer_plugins_array($travel_hiking_plugins){
            $travel_hiking_plugins[] = array(
                    'name'     => esc_html__('WPElemento Importer', 'travel-hiking'),
                    '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.', 'travel-hiking'),               
            );
            return $travel_hiking_plugins;
        }

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

        }

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

            $travel_hiking_act_count  = 0;
            $travel_hiking_actions_todo = get_option( 'recommending_actions', array());

            $travel_hiking_plugins = $this->travel_hiking_get_recommended_plugins();

            if ($travel_hiking_plugins) {
                foreach ($travel_hiking_plugins as $travel_hiking_key => $travel_hiking_plugin) {
                    $travel_hiking_action = array();
                    if (!isset($travel_hiking_plugin['slug'])) {
                        continue;
                    }

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

                    $travel_hiking_action['name'] = '';
                    if (isset($travel_hiking_plugin['name'])) {
                        $travel_hiking_action['title'] = $travel_hiking_plugin['name'];
                    }

                    $travel_hiking_link_and_is_done  = $this->travel_hiking_get_plugin_buttion($travel_hiking_plugin['slug'], $travel_hiking_plugin['name'], $travel_hiking_plugin['function']);
                    $travel_hiking_action['link']    = $travel_hiking_link_and_is_done['button'];
                    $travel_hiking_action['is_done'] = $travel_hiking_link_and_is_done['done'];
                    if (!$travel_hiking_action['is_done'] && (!isset($travel_hiking_actions_todo[$travel_hiking_action['id']]) || !$travel_hiking_actions_todo[$travel_hiking_action['id']])) {
                        $travel_hiking_act_count++;
                    }
                    $travel_hiking_recommended_actions[] = $travel_hiking_action;
                    $travel_hiking_actions_todo[]        = array('id' => $travel_hiking_action['id'], 'watch' => true);
                }
                return array('count' => $travel_hiking_act_count, 'actions' => $travel_hiking_recommended_actions);
            }

        }

        public function travel_hiking_get_recommended_plugins() {

            $travel_hiking_plugins = apply_filters('travel_hiking_recommended_plugins', array());
            return $travel_hiking_plugins;
        }

        public function travel_hiking_get_plugin_buttion($slug, $name, $function) {
                $travel_hiking_is_done      = false;
                $travel_hiking_button_html  = '';
                $travel_hiking_is_installed = $this->is_plugin_installed($slug);
                $travel_hiking_plugin_path  = $this->get_plugin_basename_from_slug($slug);
                $travel_hiking_is_activeted = (class_exists($function)) ? true : false;
                if (!$travel_hiking_is_installed) {
                    $travel_hiking_plugin_install_url = add_query_arg(
                        array(
                            'action' => 'install-plugin',
                            'plugin' => $slug,
                        ),
                        self_admin_url('update.php')
                    );
                    $travel_hiking_plugin_install_url = wp_nonce_url($travel_hiking_plugin_install_url, 'install-plugin_' . esc_attr($slug));
                    $travel_hiking_button_html        = sprintf('<a class="travel-hiking-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($travel_hiking_plugin_install_url),
                        sprintf(esc_html__('Install %s Now', 'travel-hiking'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Install & Activate', 'travel-hiking')
                    );
                } elseif ($travel_hiking_is_installed && !$travel_hiking_is_activeted) {

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

                    $travel_hiking_button_html = sprintf('<a class="travel-hiking-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($travel_hiking_plugin_activate_link),
                        sprintf(esc_html__('Activate %s Now', 'travel-hiking'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Activate', 'travel-hiking')
                    );
                } elseif ($travel_hiking_is_activeted) {
                    $travel_hiking_button_html = sprintf('<div class="action-link button disabled"><span class="dashicons dashicons-yes"></span> %s</div>', esc_html__('Active', 'travel-hiking'));
                    $travel_hiking_is_done     = true;
                }

                return array('done' => $travel_hiking_is_done, 'button' => $travel_hiking_button_html);
            }
        public function is_plugin_installed($slug) {
            $travel_hiking_installed_plugins = $this->get_installed_plugins(); // Retrieve a list of all installed plugins (WP cached).
            $travel_hiking_file_path         = $this->get_plugin_basename_from_slug($slug);
            return (!empty($travel_hiking_installed_plugins[$travel_hiking_file_path]));
        }
        public function get_plugin_basename_from_slug($slug) {
            $travel_hiking_keys = array_keys($this->get_installed_plugins());
            foreach ($travel_hiking_keys as $travel_hiking_key) {
                if (preg_match('|^' . $slug . '/|', $travel_hiking_key)) {
                    return $travel_hiking_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
 */
Travel_Hiking_Plugin_Activation_WPElemento_Importer::get_instance();