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

    class Tour_Booking_Agency_Plugin_Activation_WPElemento_Importer {

        private static $tour_booking_agency_instance;
        public $tour_booking_agency_action_count;
        public $tour_booking_agency_recommended_actions;

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

        /*  Constructor */
        public function __construct() {

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

            // ---------- wpelementoimpoter Plugin Activation -------
            add_filter( 'tour_booking_agency_recommended_plugins', array($this, 'tour_booking_agency_recommended_elemento_importer_plugins_array') );

            $tour_booking_agency_actions                   = $this->tour_booking_agency_get_recommended_actions();
            $this->tour_booking_agency_action_count        = $tour_booking_agency_actions['count'];
            $this->tour_booking_agency_recommended_actions = $tour_booking_agency_actions['actions'];

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

        public function tour_booking_agency_recommended_elemento_importer_plugins_array($tour_booking_agency_plugins){
            $tour_booking_agency_plugins[] = array(
                    'name'     => esc_html__('WPElemento Importer', 'tour-booking-agency'),
                    '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.', 'tour-booking-agency'),               
            );
            return $tour_booking_agency_plugins;
        }

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

        }

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

            $tour_booking_agency_act_count  = 0;
            $tour_booking_agency_actions_todo = get_option( 'recommending_actions', array());

            $tour_booking_agency_plugins = $this->tour_booking_agency_get_recommended_plugins();

            if ($tour_booking_agency_plugins) {
                foreach ($tour_booking_agency_plugins as $tour_booking_agency_key => $tour_booking_agency_plugin) {
                    $tour_booking_agency_action = array();
                    if (!isset($tour_booking_agency_plugin['slug'])) {
                        continue;
                    }

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

                    $tour_booking_agency_action['name'] = '';
                    if (isset($tour_booking_agency_plugin['name'])) {
                        $tour_booking_agency_action['title'] = $tour_booking_agency_plugin['name'];
                    }

                    $tour_booking_agency_link_and_is_done  = $this->tour_booking_agency_get_plugin_buttion($tour_booking_agency_plugin['slug'], $tour_booking_agency_plugin['name'], $tour_booking_agency_plugin['function']);
                    $tour_booking_agency_action['link']    = $tour_booking_agency_link_and_is_done['button'];
                    $tour_booking_agency_action['is_done'] = $tour_booking_agency_link_and_is_done['done'];
                    if (!$tour_booking_agency_action['is_done'] && (!isset($tour_booking_agency_actions_todo[$tour_booking_agency_action['id']]) || !$tour_booking_agency_actions_todo[$tour_booking_agency_action['id']])) {
                        $tour_booking_agency_act_count++;
                    }
                    $tour_booking_agency_recommended_actions[] = $tour_booking_agency_action;
                    $tour_booking_agency_actions_todo[]        = array('id' => $tour_booking_agency_action['id'], 'watch' => true);
                }
                return array('count' => $tour_booking_agency_act_count, 'actions' => $tour_booking_agency_recommended_actions);
            }

        }

        public function tour_booking_agency_get_recommended_plugins() {

            $tour_booking_agency_plugins = apply_filters('tour_booking_agency_recommended_plugins', array());
            return $tour_booking_agency_plugins;
        }

        public function tour_booking_agency_get_plugin_buttion($slug, $name, $function) {
                $tour_booking_agency_is_done      = false;
                $tour_booking_agency_button_html  = '';
                $tour_booking_agency_is_installed = $this->is_plugin_installed($slug);
                $tour_booking_agency_plugin_path  = $this->get_plugin_basename_from_slug($slug);
                $tour_booking_agency_is_activeted = (class_exists($function)) ? true : false;
                if (!$tour_booking_agency_is_installed) {
                    $tour_booking_agency_plugin_install_url = add_query_arg(
                        array(
                            'action' => 'install-plugin',
                            'plugin' => $slug,
                        ),
                        self_admin_url('update.php')
                    );
                    $tour_booking_agency_plugin_install_url = wp_nonce_url($tour_booking_agency_plugin_install_url, 'install-plugin_' . esc_attr($slug));
                    $tour_booking_agency_button_html        = sprintf('<a class="tour-booking-agency-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_booking_agency_plugin_install_url),
                        sprintf(esc_html__('Install %s Now', 'tour-booking-agency'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Install & Activate', 'tour-booking-agency')
                    );
                } elseif ($tour_booking_agency_is_installed && !$tour_booking_agency_is_activeted) {

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

                    $tour_booking_agency_button_html = sprintf('<a class="tour-booking-agency-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_booking_agency_plugin_activate_link),
                        sprintf(esc_html__('Activate %s Now', 'tour-booking-agency'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Activate', 'tour-booking-agency')
                    );
                } elseif ($tour_booking_agency_is_activeted) {
                    $tour_booking_agency_button_html = sprintf('<div class="action-link button disabled"><span class="dashicons dashicons-yes"></span> %s</div>', esc_html__('Active', 'tour-booking-agency'));
                    $tour_booking_agency_is_done     = true;
                }

                return array('done' => $tour_booking_agency_is_done, 'button' => $tour_booking_agency_button_html);
            }
        public function is_plugin_installed($slug) {
            $tour_booking_agency_installed_plugins = $this->get_installed_plugins(); // Retrieve a list of all installed plugins (WP cached).
            $tour_booking_agency_file_path         = $this->get_plugin_basename_from_slug($slug);
            return (!empty($tour_booking_agency_installed_plugins[$tour_booking_agency_file_path]));
        }
        public function get_plugin_basename_from_slug($slug) {
            $tour_booking_agency_keys = array_keys($this->get_installed_plugins());
            foreach ($tour_booking_agency_keys as $tour_booking_agency_key) {
                if (preg_match('|^' . $slug . '/|', $tour_booking_agency_key)) {
                    return $tour_booking_agency_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_Booking_Agency_Plugin_Activation_WPElemento_Importer::get_instance();