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

    class Old_Age_Home_Plugin_Activation_WPElemento_Importer {

        private static $old_age_home_instance;
        public $old_age_home_action_count;
        public $old_age_home_recommended_actions;

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

        /*  Constructor */
        public function __construct() {

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

            // ---------- wpelementoimpoter Plugin Activation -------
            add_filter( 'old_age_home_recommended_plugins', array($this, 'old_age_home_recommended_elemento_importer_plugins_array') );

            $old_age_home_actions                   = $this->old_age_home_get_recommended_actions();
            $this->old_age_home_action_count        = $old_age_home_actions['count'];
            $this->old_age_home_recommended_actions = $old_age_home_actions['actions'];

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

        public function old_age_home_recommended_elemento_importer_plugins_array($old_age_home_plugins){
            $old_age_home_plugins[] = array(
                    'name'     => esc_html__('WPElemento Importer', 'old-age-home'),
                    '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.', 'old-age-home'),               
            );
            return $old_age_home_plugins;
        }

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

        }

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

            $old_age_home_act_count  = 0;
            $old_age_home_actions_todo = get_option( 'recommending_actions', array());

            $old_age_home_plugins = $this->old_age_home_get_recommended_plugins();

            if ($old_age_home_plugins) {
                foreach ($old_age_home_plugins as $old_age_home_key => $old_age_home_plugin) {
                    $old_age_home_action = array();
                    if (!isset($old_age_home_plugin['slug'])) {
                        continue;
                    }

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

                    $old_age_home_action['name'] = '';
                    if (isset($old_age_home_plugin['name'])) {
                        $old_age_home_action['title'] = $old_age_home_plugin['name'];
                    }

                    $old_age_home_link_and_is_done  = $this->old_age_home_get_plugin_buttion($old_age_home_plugin['slug'], $old_age_home_plugin['name'], $old_age_home_plugin['function']);
                    $old_age_home_action['link']    = $old_age_home_link_and_is_done['button'];
                    $old_age_home_action['is_done'] = $old_age_home_link_and_is_done['done'];
                    if (!$old_age_home_action['is_done'] && (!isset($old_age_home_actions_todo[$old_age_home_action['id']]) || !$old_age_home_actions_todo[$old_age_home_action['id']])) {
                        $old_age_home_act_count++;
                    }
                    $old_age_home_recommended_actions[] = $old_age_home_action;
                    $old_age_home_actions_todo[]        = array('id' => $old_age_home_action['id'], 'watch' => true);
                }
                return array('count' => $old_age_home_act_count, 'actions' => $old_age_home_recommended_actions);
            }

        }

        public function old_age_home_get_recommended_plugins() {

            $old_age_home_plugins = apply_filters('old_age_home_recommended_plugins', array());
            return $old_age_home_plugins;
        }

        public function old_age_home_get_plugin_buttion($slug, $name, $function) {
                $old_age_home_is_done      = false;
                $old_age_home_button_html  = '';
                $old_age_home_is_installed = $this->is_plugin_installed($slug);
                $old_age_home_plugin_path  = $this->get_plugin_basename_from_slug($slug);
                $old_age_home_is_activeted = (class_exists($function)) ? true : false;
                if (!$old_age_home_is_installed) {
                    $old_age_home_plugin_install_url = add_query_arg(
                        array(
                            'action' => 'install-plugin',
                            'plugin' => $slug,
                        ),
                        self_admin_url('update.php')
                    );
                    $old_age_home_plugin_install_url = wp_nonce_url($old_age_home_plugin_install_url, 'install-plugin_' . esc_attr($slug));
                    $old_age_home_button_html        = sprintf('<a class="old-age-home-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($old_age_home_plugin_install_url),
                        sprintf(esc_html__('Install %s Now', 'old-age-home'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Install & Activate', 'old-age-home')
                    );
                } elseif ($old_age_home_is_installed && !$old_age_home_is_activeted) {

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

                    $old_age_home_button_html = sprintf('<a class="old-age-home-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($old_age_home_plugin_activate_link),
                        sprintf(esc_html__('Activate %s Now', 'old-age-home'), esc_html($name)),
                        esc_html($name),
                        esc_html__('Activate', 'old-age-home')
                    );
                } elseif ($old_age_home_is_activeted) {
                    $old_age_home_button_html = sprintf('<div class="action-link button disabled"><span class="dashicons dashicons-yes"></span> %s</div>', esc_html__('Active', 'old-age-home'));
                    $old_age_home_is_done     = true;
                }

                return array('done' => $old_age_home_is_done, 'button' => $old_age_home_button_html);
            }
        public function is_plugin_installed($slug) {
            $old_age_home_installed_plugins = $this->get_installed_plugins(); // Retrieve a list of all installed plugins (WP cached).
            $old_age_home_file_path         = $this->get_plugin_basename_from_slug($slug);
            return (!empty($old_age_home_installed_plugins[$old_age_home_file_path]));
        }
        public function get_plugin_basename_from_slug($slug) {
            $old_age_home_keys = array_keys($this->get_installed_plugins());
            foreach ($old_age_home_keys as $old_age_home_key) {
                if (preg_match('|^' . $slug . '/|', $old_age_home_key)) {
                    return $old_age_home_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
 */
Old_Age_Home_Plugin_Activation_WPElemento_Importer::get_instance();