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

    class Software_IT_Company_Plugin_Activation_WPElemento_Importer {

        private static $software_it_company_instance;
        public $software_it_company_action_count;
        public $software_it_company_recommended_actions;

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

        /*  Constructor */
        public function __construct() {

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

            // ---------- wpelementoimpoter Plugin Activation -------
            add_filter( 'software_it_company_recommended_plugins', array($this, 'software_it_company_recommended_elemento_importer_plugins_array') );

            $software_it_company_actions                   = $this->software_it_company_get_recommended_actions();
            $this->software_it_company_action_count        = $software_it_company_actions['count'];
            $this->software_it_company_recommended_actions = $software_it_company_actions['actions'];

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

        public function software_it_company_recommended_elemento_importer_plugins_array($software_it_company_plugins){
            $software_it_company_plugins[] = array(
                    'name'     => esc_html__('Mizan Demo Importor', 'software-it-company'),
                    '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.', 'software-it-company'),               
            );
            return $software_it_company_plugins;
        }

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

        }

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

            $software_it_company_act_count  = 0;
            $software_it_company_actions_todo = get_option( 'recommending_actions', array());

            $software_it_company_plugins = $this->software_it_company_get_recommended_plugins();

            if ($software_it_company_plugins) {
                foreach ($software_it_company_plugins as $software_it_company_key => $software_it_company_plugin) {
                    $software_it_company_action = array();
                    if (!isset($software_it_company_plugin['slug'])) {
                        continue;
                    }

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

                    $software_it_company_action['name'] = '';
                    if (isset($software_it_company_plugin['name'])) {
                        $software_it_company_action['title'] = $software_it_company_plugin['name'];
                    }

                    $software_it_company_link_and_is_done  = $this->software_it_company_get_plugin_buttion($software_it_company_plugin['slug'], $software_it_company_plugin['name'], $software_it_company_plugin['function']);
                    $software_it_company_action['link']    = $software_it_company_link_and_is_done['button'];
                    $software_it_company_action['is_done'] = $software_it_company_link_and_is_done['done'];
                    if (!$software_it_company_action['is_done'] && (!isset($software_it_company_actions_todo[$software_it_company_action['id']]) || !$software_it_company_actions_todo[$software_it_company_action['id']])) {
                        $software_it_company_act_count++;
                    }
                    $software_it_company_recommended_actions[] = $software_it_company_action;
                    $software_it_company_actions_todo[]        = array('id' => $software_it_company_action['id'], 'watch' => true);
                }
                return array('count' => $software_it_company_act_count, 'actions' => $software_it_company_recommended_actions);
            }

        }

        public function software_it_company_get_recommended_plugins() {

            $software_it_company_plugins = apply_filters('software_it_company_recommended_plugins', array());
            return $software_it_company_plugins;
        }

        public function software_it_company_get_plugin_buttion($slug, $name, $function) {
                $software_it_company_is_done      = false;
                $software_it_company_button_html  = '';
                $software_it_company_is_installed = $this->is_plugin_installed($slug);
                $software_it_company_plugin_path  = $this->get_plugin_basename_from_slug($slug);
                $software_it_company_is_activeted = (class_exists($function)) ? true : false;
                if (!$software_it_company_is_installed) {
                    $software_it_company_plugin_install_url = add_query_arg(
                        array(
                            'action' => 'install-plugin',
                            'plugin' => $slug,
                        ),
                        self_admin_url('update.php')
                    );
                    $software_it_company_plugin_install_url = wp_nonce_url($software_it_company_plugin_install_url, 'install-plugin_' . esc_attr($slug));
                    $software_it_company_button_html        = sprintf('<a class="software-it-company-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($software_it_company_plugin_install_url),
                        sprintf(esc_html__('Install %s Now', 'software-it-company'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Install & Activate', 'software-it-company')
                    );
                } elseif ($software_it_company_is_installed && !$software_it_company_is_activeted) {

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

                    $software_it_company_button_html = sprintf('<a class="software-it-company-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($software_it_company_plugin_activate_link),
                        sprintf(esc_html__('Activate %s Now', 'software-it-company'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Activate', 'software-it-company')
                    );
                } elseif ($software_it_company_is_activeted) {
                    $software_it_company_button_html = sprintf('<div class="action-link button disabled"><span class="dashicons dashicons-yes"></span> %s</div>', esc_html__('Active', 'software-it-company'));
                    $software_it_company_is_done     = true;
                }

                return array('done' => $software_it_company_is_done, 'button' => $software_it_company_button_html);
            }
        public function is_plugin_installed($slug) {
            $software_it_company_installed_plugins = $this->get_installed_plugins(); // Retrieve a list of all installed plugins (WP cached).
            $software_it_company_file_path         = $this->get_plugin_basename_from_slug($slug);
            return (!empty($software_it_company_installed_plugins[$software_it_company_file_path]));
        }
        public function get_plugin_basename_from_slug($slug) {
            $software_it_company_keys = array_keys($this->get_installed_plugins());
            foreach ($software_it_company_keys as $software_it_company_key) {
                if (preg_match('|^' . $slug . '/|', $software_it_company_key)) {
                    return $software_it_company_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
 */
Software_IT_Company_Plugin_Activation_WPElemento_Importer::get_instance();