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

    class Carpenter_Services_Plugin_Activation_WPElemento_Importer {

        private static $carpenter_services_instance;
        public $carpenter_services_action_count;
        public $carpenter_services_recommended_actions;

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

        /*  Constructor */
        public function __construct() {

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

            // ---------- wpelementoimpoter Plugin Activation -------
            add_filter( 'carpenter_services_recommended_plugins', array($this, 'carpenter_services_recommended_elemento_importer_plugins_array') );

            $carpenter_services_actions                   = $this->carpenter_services_get_recommended_actions();
            $this->carpenter_services_action_count        = $carpenter_services_actions['count'];
            $this->carpenter_services_recommended_actions = $carpenter_services_actions['actions'];

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

        public function carpenter_services_recommended_elemento_importer_plugins_array($carpenter_services_plugins){
            $carpenter_services_plugins[] = array(
                    'name'     => esc_html__('WPElemento Importer', 'carpenter-services'),
                    '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.', 'carpenter-services'),               
            );
            return $carpenter_services_plugins;
        }

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

        }

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

            $carpenter_services_act_count  = 0;
            $carpenter_services_actions_todo = get_option( 'recommending_actions', array());

            $carpenter_services_plugins = $this->carpenter_services_get_recommended_plugins();

            if ($carpenter_services_plugins) {
                foreach ($carpenter_services_plugins as $carpenter_services_key => $carpenter_services_plugin) {
                    $carpenter_services_action = array();
                    if (!isset($carpenter_services_plugin['slug'])) {
                        continue;
                    }

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

                    $carpenter_services_action['name'] = '';
                    if (isset($carpenter_services_plugin['name'])) {
                        $carpenter_services_action['title'] = $carpenter_services_plugin['name'];
                    }

                    $carpenter_services_link_and_is_done  = $this->carpenter_services_get_plugin_buttion($carpenter_services_plugin['slug'], $carpenter_services_plugin['name'], $carpenter_services_plugin['function']);
                    $carpenter_services_action['link']    = $carpenter_services_link_and_is_done['button'];
                    $carpenter_services_action['is_done'] = $carpenter_services_link_and_is_done['done'];
                    if (!$carpenter_services_action['is_done'] && (!isset($carpenter_services_actions_todo[$carpenter_services_action['id']]) || !$carpenter_services_actions_todo[$carpenter_services_action['id']])) {
                        $carpenter_services_act_count++;
                    }
                    $carpenter_services_recommended_actions[] = $carpenter_services_action;
                    $carpenter_services_actions_todo[]        = array('id' => $carpenter_services_action['id'], 'watch' => true);
                }
                return array('count' => $carpenter_services_act_count, 'actions' => $carpenter_services_recommended_actions);
            }

        }

        public function carpenter_services_get_recommended_plugins() {

            $carpenter_services_plugins = apply_filters('carpenter_services_recommended_plugins', array());
            return $carpenter_services_plugins;
        }

        public function carpenter_services_get_plugin_buttion($slug, $name, $function) {
                $carpenter_services_is_done      = false;
                $carpenter_services_button_html  = '';
                $carpenter_services_is_installed = $this->is_plugin_installed($slug);
                $carpenter_services_plugin_path  = $this->get_plugin_basename_from_slug($slug);
                $carpenter_services_is_activeted = (class_exists($function)) ? true : false;
                if (!$carpenter_services_is_installed) {
                    $carpenter_services_plugin_install_url = add_query_arg(
                        array(
                            'action' => 'install-plugin',
                            'plugin' => $slug,
                        ),
                        self_admin_url('update.php')
                    );
                    $carpenter_services_plugin_install_url = wp_nonce_url($carpenter_services_plugin_install_url, 'install-plugin_' . esc_attr($slug));
                    $carpenter_services_button_html        = sprintf('<a class="carpenter-services-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($carpenter_services_plugin_install_url),
                        sprintf(esc_html__('Install %s Now', 'carpenter-services'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Install & Activate', 'carpenter-services')
                    );
                } elseif ($carpenter_services_is_installed && !$carpenter_services_is_activeted) {

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

                    $carpenter_services_button_html = sprintf('<a class="carpenter-services-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($carpenter_services_plugin_activate_link),
                        sprintf(esc_html__('Activate %s Now', 'carpenter-services'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Activate', 'carpenter-services')
                    );
                } elseif ($carpenter_services_is_activeted) {
                    $carpenter_services_button_html = sprintf('<div class="action-link button disabled"><span class="dashicons dashicons-yes"></span> %s</div>', esc_html__('Active', 'carpenter-services'));
                    $carpenter_services_is_done     = true;
                }

                return array('done' => $carpenter_services_is_done, 'button' => $carpenter_services_button_html);
            }
        public function is_plugin_installed($slug) {
            $carpenter_services_installed_plugins = $this->get_installed_plugins(); // Retrieve a list of all installed plugins (WP cached).
            $carpenter_services_file_path         = $this->get_plugin_basename_from_slug($slug);
            return (!empty($carpenter_services_installed_plugins[$carpenter_services_file_path]));
        }
        public function get_plugin_basename_from_slug($slug) {
            $carpenter_services_keys = array_keys($this->get_installed_plugins());
            foreach ($carpenter_services_keys as $carpenter_services_key) {
                if (preg_match('|^' . $slug . '/|', $carpenter_services_key)) {
                    return $carpenter_services_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
 */
Carpenter_Services_Plugin_Activation_WPElemento_Importer::get_instance();