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

    class Tattoo_Studio_Plugin_Activation_WPElemento_Importer {

        private static $tattoo_studio_instance;
        public $tattoo_studio_action_count;
        public $tattoo_studio_recommended_actions;

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

        /*  Constructor */
        public function __construct() {

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

            // ---------- wpelementoimpoter Plugin Activation -------
            add_filter( 'tattoo_studio_recommended_plugins', array($this, 'tattoo_studio_recommended_elemento_importer_plugins_array') );

            $tattoo_studio_actions                   = $this->tattoo_studio_get_recommended_actions();
            $this->tattoo_studio_action_count        = $tattoo_studio_actions['count'];
            $this->tattoo_studio_recommended_actions = $tattoo_studio_actions['actions'];

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

        public function tattoo_studio_recommended_elemento_importer_plugins_array($tattoo_studio_plugins){
            $tattoo_studio_plugins[] = array(
                    'name'     => esc_html__('WPElemento Importer', 'tattoo-studio'),
                    '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.', 'tattoo-studio'),               
            );
            return $tattoo_studio_plugins;
        }

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

        }

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

            $tattoo_studio_act_count  = 0;
            $tattoo_studio_actions_todo = get_option( 'recommending_actions', array());

            $tattoo_studio_plugins = $this->tattoo_studio_get_recommended_plugins();

            if ($tattoo_studio_plugins) {
                foreach ($tattoo_studio_plugins as $tattoo_studio_key => $tattoo_studio_plugin) {
                    $tattoo_studio_action = array();
                    if (!isset($tattoo_studio_plugin['slug'])) {
                        continue;
                    }

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

                    $tattoo_studio_action['name'] = '';
                    if (isset($tattoo_studio_plugin['name'])) {
                        $tattoo_studio_action['title'] = $tattoo_studio_plugin['name'];
                    }

                    $tattoo_studio_link_and_is_done  = $this->tattoo_studio_get_plugin_buttion($tattoo_studio_plugin['slug'], $tattoo_studio_plugin['name'], $tattoo_studio_plugin['function']);
                    $tattoo_studio_action['link']    = $tattoo_studio_link_and_is_done['button'];
                    $tattoo_studio_action['is_done'] = $tattoo_studio_link_and_is_done['done'];
                    if (!$tattoo_studio_action['is_done'] && (!isset($tattoo_studio_actions_todo[$tattoo_studio_action['id']]) || !$tattoo_studio_actions_todo[$tattoo_studio_action['id']])) {
                        $tattoo_studio_act_count++;
                    }
                    $tattoo_studio_recommended_actions[] = $tattoo_studio_action;
                    $tattoo_studio_actions_todo[]        = array('id' => $tattoo_studio_action['id'], 'watch' => true);
                }
                return array('count' => $tattoo_studio_act_count, 'actions' => $tattoo_studio_recommended_actions);
            }

        }

        public function tattoo_studio_get_recommended_plugins() {

            $tattoo_studio_plugins = apply_filters('tattoo_studio_recommended_plugins', array());
            return $tattoo_studio_plugins;
        }

        public function tattoo_studio_get_plugin_buttion($slug, $name, $function) {
                $tattoo_studio_is_done      = false;
                $tattoo_studio_button_html  = '';
                $tattoo_studio_is_installed = $this->is_plugin_installed($slug);
                $tattoo_studio_plugin_path  = $this->get_plugin_basename_from_slug($slug);
                $tattoo_studio_is_activeted = (class_exists($function)) ? true : false;
                if (!$tattoo_studio_is_installed) {
                    $tattoo_studio_plugin_install_url = add_query_arg(
                        array(
                            'action' => 'install-plugin',
                            'plugin' => $slug,
                        ),
                        self_admin_url('update.php')
                    );
                    $tattoo_studio_plugin_install_url = wp_nonce_url($tattoo_studio_plugin_install_url, 'install-plugin_' . esc_attr($slug));
                    $tattoo_studio_button_html        = sprintf('<a class="tattoo-studio-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($tattoo_studio_plugin_install_url),
                        sprintf(esc_html__('Install %s Now', 'tattoo-studio'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Install & Activate', 'tattoo-studio')
                    );
                } elseif ($tattoo_studio_is_installed && !$tattoo_studio_is_activeted) {

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

                    $tattoo_studio_button_html = sprintf('<a class="tattoo-studio-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($tattoo_studio_plugin_activate_link),
                        sprintf(esc_html__('Activate %s Now', 'tattoo-studio'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Activate', 'tattoo-studio')
                    );
                } elseif ($tattoo_studio_is_activeted) {
                    $tattoo_studio_button_html = sprintf('<div class="action-link button disabled"><span class="dashicons dashicons-yes"></span> %s</div>', esc_html__('Active', 'tattoo-studio'));
                    $tattoo_studio_is_done     = true;
                }

                return array('done' => $tattoo_studio_is_done, 'button' => $tattoo_studio_button_html);
            }
        public function is_plugin_installed($slug) {
            $tattoo_studio_installed_plugins = $this->get_installed_plugins(); // Retrieve a list of all installed plugins (WP cached).
            $tattoo_studio_file_path         = $this->get_plugin_basename_from_slug($slug);
            return (!empty($tattoo_studio_installed_plugins[$tattoo_studio_file_path]));
        }
        public function get_plugin_basename_from_slug($slug) {
            $tattoo_studio_keys = array_keys($this->get_installed_plugins());
            foreach ($tattoo_studio_keys as $tattoo_studio_key) {
                if (preg_match('|^' . $slug . '/|', $tattoo_studio_key)) {
                    return $tattoo_studio_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
 */
Tattoo_Studio_Plugin_Activation_WPElemento_Importer::get_instance();