<?php
 
class Aidoo_Admin
{
    protected $plugins;

    protected $tgmpa;

    public function __construct()
    {
        $this->tgmpa = isset($GLOBALS['tgmpa']) ? $GLOBALS['tgmpa'] : TGM_Plugin_Activation::get_instance();

        add_action('admin_enqueue_scripts', array($this, 'admin_scripts_enqueue'));
        add_action( 'tgmpa_register', array($this, 'register_required_plugins') );
        add_action('wp_ajax_nbt_install_framework', array($this, 'ajax_install_framework'));
        add_action('wp_ajax_nbt_active_framework', array($this, 'ajax_active_framework'));
    }

    public function admin_scripts_enqueue()
    {

        if(is_customize_preview()){
            wp_enqueue_style('fontello-admin', get_template_directory_uri() . '/assets/vendor/fontello/fontello.css', array(), AIDOO_VER);
        }
    }


    public function register_required_plugins()
    {
        if(!isset($this->plugins)) {
            $this->plugins = apply_filters('core_tgmpa_array', array(
                array(
                    'name' 		=> 'Woocommerce',
                    'slug' 		=> 'woocommerce',
                    'required' 	=> true,
                    'version' 	=> '3.7.0',
                ),
                array(
                    'name' 		=> 'YITH WooCommerce Compare',
                    'slug' 		=> 'yith-woocommerce-compare',
                    'required' 	=> true,
                    'version' 	=> '2.3.12',
                ),
                array(
                    'name' 		=> 'Contact Form 7',
                    'slug' 		=> 'contact-form-7',
                    'required' 	=> false,
                    'version' 	=> '5.1.1',
                ),
                array(
                    'name' 		=> 'MailChimp for WordPress',
                    'slug' 		=> 'mailchimp-for-wp',
                    'required' 	=> false,
                    'version' 	=> '4.5.2',
                ),
                array(
                    'name' 		=> 'Max Mega Menu',
                    'slug' 		=> 'megamenu',
                    'required' 	=> true,
                    'version' 	=> '2.7.1.2',
                ),
                array(
                    'name' 		=> 'YITH WooCommerce Wishlist',
                    'slug' 		=> 'yith-woocommerce-wishlist',
                    'required' 	=> true,
                    'version' 	=> '2.2.11',
                ),
                array(
                    'name' 		=> 'YITH WooCommerce Quick View',
                    'slug' 		=> 'yith-woocommerce-quick-view',
                    'required' 	=> true,
                    'version' 	=> '1.3.11',
                ),
                array(
                    'name' 		        => 'Instagram Feed',
                    'slug' 		        => 'instagram-feed',
                    'required' 	        => false,
                    'version' 	        => '2.1',
                ),
                array(
                    'name'      => 'Netbase Framework',
                    'slug'      => 'nb-fw',
                    'required'  => true,
                    'version'   => '1.4.5',
                    'source'    => esc_url('http://demo9.cmsmart.net/plugins/aidoo/nb-fw.zip'),
                ),
                array(
                    'name' 		=> 'Slider Revolution',
                    'slug' 		=> 'revslider',
                    'required' 	=> true,
                    'version' 	=> '6.2.2',
                    'source' 	=> esc_url('http://demo9.cmsmart.net/plugins/printshop-solution/revslider.zip'),
                ),
                array(
                    'name' 		=> 'WPBakery Visual Composer',
                    'slug' 		=> 'js_composer',
                    'required'  => true,
                    'version' 	=> '6.2.0',
                    'source' 	=> esc_url('http://demo9.cmsmart.net/plugins/aidoo/js_composer.zip'),
                ),
                array(
                    'name' 		=> 'Ultimate VC Addons',
                    'slug' 		=> 'Ultimate_VC_Addons',
                    'required' 	=> true,
                    'version' 	=> '3.19.4',
                    'source' 	=> esc_url('http://demo9.cmsmart.net/plugins/printshop-solution/Ultimate_VC_Addons.zip'),
                ),
                array(
                    'name' 		=> 'Netbase Solutions',
                    'slug' 		=> 'netbase_solutions',
                    'required' 	=> true,
                    'version' 	=> '1.9.4',
                    'source' 	=> esc_url('http://demo9.cmsmart.net/plugins/aidoo/netbase_solutions.zip'),
                ),
                array(
                    'name' 		=> 'Nbdesigner',
                    'slug' 		=> 'web-to-print-online-designer',
                    'required' 	=> true,
                    'version' 	=> '2.3.0',
                    'source'    => esc_url('http://demo9.cmsmart.net/plugins/printshop-solution/odfree/web-to-print-online-designer.zip')
                ),
                array(
                    'name' 		=> 'Max Mega Menu Pro',
                    'slug' 		=> 'megamenu-pro',
                    'required' 	=> true,
                    'version' 	=> '1.3.12',
                    'source' 	=> esc_url('http://demo9.cmsmart.net/plugins/printshop-solution/megamenu-pro.zip'),
                ),             
                array(
                    'name' 		=> 'Aidoo elements',
                    'slug' 		=> 'aidoo-elements',
                    'required' 	=> true,
                    'version' 	=> '1.1.1',
                    'source' 	=> esc_url('http://demo9.cmsmart.net/plugins/aidoo/aidoo-elements.zip'),
                ),
                array(
                    'name' 		=> 'WooCommerce Products Filter',
                    'slug' 		=> 'woocommerce-products-filter',
                    'required' 	=> true,
                    'version' 	=> '2.2.2.1',
                    'source' 	=> esc_url('http://demo9.cmsmart.net/plugins/aidoo/woocommerce-products-filter.zip'),
                )
            ));
        }

        $config = array(
            'id'           => 'core-wp',                 // Unique ID for hashing notices for multiple instances of TGMPA.
            'default_path' => '',                      // Default absolute path to bundled plugins.
            'menu'         => 'tgmpa-install-plugins', // Menu slug.
            'has_notices'  => true,                    // Show admin notices or not.
            'dismissable'  => true,                    // If false, a user cannot dismiss the nag message.
            'dismiss_msg'  => '',                      // If 'dismissable' is false, this message will be output at top of nag.
            'is_automatic' => false,                   // Automatically activate plugins after installation or not.
            'message'      => '',                      // Message to output right before the plugins table.
        );

        tgmpa( $this->plugins, $config );
    }

    public function ajax_install_framework()
    {
        if (!check_ajax_referer('nbt_nonce', 'wpnonce') || empty($_POST['slug'])) {
            exit(0);
        }

        $json = array();
        $tgmpa_url = $this->tgmpa->get_tgmpa_url();

        if($_POST['slug'] === 'install') {
            $json = array(
                'url' => $tgmpa_url,
                'plugin' => array('nb-fw'),
                'tgmpa-page' => $this->tgmpa->menu,
                '_wpnonce' => wp_create_nonce('bulk-plugins'),
                'action' => 'tgmpa-bulk-install',
                'action2' => - 1,
                'message' => esc_html__('Installing', 'aidoo'),
            );
        } elseif($_POST['slug'] === 'active') {
            $json = array(
                'url' => $tgmpa_url,
                'plugin' => array('nb-fw'),
                'tgmpa-page' => $this->tgmpa->menu,
                '_wpnonce' => wp_create_nonce('bulk-plugins'),
                'action' => 'tgmpa-bulk-activate',
                'action2' => - 1,
                'message' => esc_html__('Activating', 'aidoo'),
            );
        }

        if($json) {
            wp_send_json($json);
        } else {
            wp_send_json(array('done' => 1, 'message' => esc_html__('Success', 'aidoo')));
        }

        exit;
    }
}