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

    class Tour_Travel_Package_Plugin_Activation_WPElemento_Importer {

        private static $tour_travel_package_instance;
        public $tour_travel_package_action_count;
        public $tour_travel_package_recommended_actions;

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

        /*  Constructor */
        public function __construct() {

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

            // ---------- wpelementoimpoter Plugin Activation -------
            add_filter( 'tour_travel_package_recommended_plugins', array($this, 'tour_travel_package_recommended_elemento_importer_plugins_array') );

            $tour_travel_package_actions                   = $this->tour_travel_package_get_recommended_actions();
            $this->tour_travel_package_action_count        = $tour_travel_package_actions['count'];
            $this->tour_travel_package_recommended_actions = $tour_travel_package_actions['actions'];

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

        public function tour_travel_package_recommended_elemento_importer_plugins_array($tour_travel_package_plugins){
            $tour_travel_package_plugins[] = array(
                    'name'     => esc_html__('Mizan Demo Importor', 'tour-travel-package'),
                    'slug'     =>  'mizan-demo-importer',
                    'function' => 'Mizan_Importer_ThemeWhizzie',
                    'desc'     => esc_html__('We highly recommend installing the Mizan Demo Importor plugin for importing the demo content with Elementor.', 'tour-travel-package'),               
            );
            return $tour_travel_package_plugins;
        }

        public function enqueue_scripts() {
            wp_enqueue_script('updates');      
            wp_register_script( 'tour-travel-package-plugin-activation-script', esc_url(get_template_directory_uri()) . '/inc/getstart/js/plugin-activation.js', array('jquery') );
            wp_localize_script('tour-travel-package-plugin-activation-script', 'tour_travel_package_plugin_activate_plugin',
                array(
                    'installing' => esc_html__('Installing', 'tour-travel-package'),
                    'activating' => esc_html__('Activating', 'tour-travel-package'),
                    'error' => esc_html__('Error', 'tour-travel-package'),
                    'ajax_url' => esc_url(admin_url('admin-ajax.php')),
                    'wpelementoimpoter_admin_url' => esc_url(admin_url('admin.php?page=mizan-demo-importer-tgmpa-install-plugins')),
                    'addon_admin_url' => esc_url(admin_url('admin.php?page=mizandemoimporter-wizard'))
                )
            );
            wp_enqueue_script( 'tour-travel-package-plugin-activation-script' );

        }

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

            $tour_travel_package_act_count  = 0;
            $tour_travel_package_actions_todo = get_option( 'recommending_actions', array());

            $tour_travel_package_plugins = $this->tour_travel_package_get_recommended_plugins();

            if ($tour_travel_package_plugins) {
                foreach ($tour_travel_package_plugins as $tour_travel_package_key => $tour_travel_package_plugin) {
                    $tour_travel_package_action = array();
                    if (!isset($tour_travel_package_plugin['slug'])) {
                        continue;
                    }

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

                    $tour_travel_package_action['name'] = '';
                    if (isset($tour_travel_package_plugin['name'])) {
                        $tour_travel_package_action['title'] = $tour_travel_package_plugin['name'];
                    }

                    $tour_travel_package_link_and_is_done  = $this->tour_travel_package_get_plugin_buttion($tour_travel_package_plugin['slug'], $tour_travel_package_plugin['name'], $tour_travel_package_plugin['function']);
                    $tour_travel_package_action['link']    = $tour_travel_package_link_and_is_done['button'];
                    $tour_travel_package_action['is_done'] = $tour_travel_package_link_and_is_done['done'];
                    if (!$tour_travel_package_action['is_done'] && (!isset($tour_travel_package_actions_todo[$tour_travel_package_action['id']]) || !$tour_travel_package_actions_todo[$tour_travel_package_action['id']])) {
                        $tour_travel_package_act_count++;
                    }
                    $tour_travel_package_recommended_actions[] = $tour_travel_package_action;
                    $tour_travel_package_actions_todo[]        = array('id' => $tour_travel_package_action['id'], 'watch' => true);
                }
                return array('count' => $tour_travel_package_act_count, 'actions' => $tour_travel_package_recommended_actions);
            }

        }

        public function tour_travel_package_get_recommended_plugins() {

            $tour_travel_package_plugins = apply_filters('tour_travel_package_recommended_plugins', array());
            return $tour_travel_package_plugins;
        }

        public function tour_travel_package_get_plugin_buttion($slug, $name, $function) {
                $tour_travel_package_is_done      = false;
                $tour_travel_package_button_html  = '';
                $tour_travel_package_is_installed = $this->is_plugin_installed($slug);
                $tour_travel_package_plugin_path  = $this->get_plugin_basename_from_slug($slug);
                $tour_travel_package_is_activeted = (class_exists($function)) ? true : false;
                if (!$tour_travel_package_is_installed) {
                    $tour_travel_package_plugin_install_url = add_query_arg(
                        array(
                            'action' => 'install-plugin',
                            'plugin' => $slug,
                        ),
                        self_admin_url('update.php')
                    );
                    $tour_travel_package_plugin_install_url = wp_nonce_url($tour_travel_package_plugin_install_url, 'install-plugin_' . esc_attr($slug));
                    $tour_travel_package_button_html        = sprintf('<a class="tour-travel-package-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($tour_travel_package_plugin_install_url),
                        sprintf(esc_html__('Install %s Now', 'tour-travel-package'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Install & Activate', 'tour-travel-package')
                    );
                } elseif ($tour_travel_package_is_installed && !$tour_travel_package_is_activeted) {

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

                    $tour_travel_package_button_html = sprintf('<a class="tour-travel-package-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($tour_travel_package_plugin_activate_link),
                        sprintf(esc_html__('Activate %s Now', 'tour-travel-package'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Activate', 'tour-travel-package')
                    );
                } elseif ($tour_travel_package_is_activeted) {
                    $tour_travel_package_button_html = sprintf('<div class="action-link button disabled"><span class="dashicons dashicons-yes"></span> %s</div>', esc_html__('Active', 'tour-travel-package'));
                    $tour_travel_package_is_done     = true;
                }

                return array('done' => $tour_travel_package_is_done, 'button' => $tour_travel_package_button_html);
            }
        public function is_plugin_installed($slug) {
            $tour_travel_package_installed_plugins = $this->get_installed_plugins(); // Retrieve a list of all installed plugins (WP cached).
            $tour_travel_package_file_path         = $this->get_plugin_basename_from_slug($slug);
            return (!empty($tour_travel_package_installed_plugins[$tour_travel_package_file_path]));
        }
        public function get_plugin_basename_from_slug($slug) {
            $tour_travel_package_keys = array_keys($this->get_installed_plugins());
            foreach ($tour_travel_package_keys as $tour_travel_package_key) {
                if (preg_match('|^' . $slug . '/|', $tour_travel_package_key)) {
                    return $tour_travel_package_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
 */
Tour_Travel_Package_Plugin_Activation_WPElemento_Importer::get_instance();