<?php
/**
 * It's for Appsbd Related Apps Module
 *
 * @package Minicart_Lite\Modules
 */

namespace Minicart_Lite\Modules;

use Appsbd_Lite\V5\libs\Ajax_Confirm_Response;
use Appsbd_Lite\V5\libs\AppInput;
use Minicart_Lite\Core\Minicart_Module_Lite;

/**
 * Class Appsbd Related App
 *
 * @author: appsbd
 */
class Appsbd_Related_App extends Minicart_Module_Lite {

	/**
	 * The initialize is generated by appsbd
	 */
	public function initialize() {
	}

	/**
	 * The on init is generated by appsbd
	 */
	public function on_init() {
		if ( $this->check_user_access() ) {
			$this->add_ajax_action( 'data', array( $this, 'data' ) );
			$this->add_ajax_action( 'activate', array( $this, 'activate_related_plugin' ) );
			$this->add_ajax_action( 'install-lite', array( $this, 'install_related_plugin' ) );
		}
		parent::on_init();
	}

	/**
	 * The data is generated by appsbd
	 */
	public function data() {
		$addons  = $this->get_addons();
		$respons = new Ajax_Confirm_Response();
		$respons->display_with_response( true, $addons );
	}

	/**
	 * The next actions is generated by appsbd
	 *
	 * @param mixed $addon It is addon param.
	 *
	 * @return array
	 */
	public function next_actions( $addon ) {
		$addons = array();
		$obj    = new \stdClass();
		if ( ! empty( $addon->pro_package ) || ! empty( $addon->lite_package ) ) {
			if ( ! $this->is_exist_plugin( $addon->pro_package ) && ! $this->is_exist_plugin( $addon->lite_package ) ) {
				$obj->button_text  = 'Install';
				$obj->button_class = 'btn-warning';
				$obj->btn_icon     = 'install';
				$obj->next_actn    = 'install';
				$addons[]          = $obj;
			} elseif ( appsbd_is_activated_plugin( $addon->pro_package ) ) {

				$obj->button_text  = 'Installed';
				$obj->button_class = 'text-success';
				$obj->btn_icon     = 'vco vco-check-circle';
				$obj->next_actn    = '';
				$addons[]          = $obj;
			} elseif ( $this->is_exist_plugin( $addon->pro_package ) ) {

				$obj->button_text  = 'Activate Pro';
				$obj->button_class = 'btn-success';
				$obj->btn_icon     = 'activate';
				$obj->next_actn    = 'activate_pro';
				$addons[]          = $obj;
			} elseif ( $this->is_exist_plugin( $addon->lite_package ) ) {

				if ( ! appsbd_is_activated_plugin( $addon->lite_package ) ) {
					$obj->button_text  = 'Activate';
					$obj->button_class = 'btn-primary';
					$obj->btn_icon     = 'activate';
					$obj->next_actn    = 'activate_lite';
					$addons[]          = $obj;
				} elseif ( ! empty( $addon->pro_package ) ) {
					$obj->button_text  = 'Get Pro';
					$obj->button_class = 'btn-primary';
					$obj->btn_icon     = '';
					$obj->next_actn    = 'get_pro';
					$addons[]          = $obj;
				}
			} else {
				$obj->button_text  = 'Install';
				$obj->button_class = 'btn-warning';
				$obj->btn_icon     = 'install';
				$obj->next_actn    = 'install';
				$addons[]          = $obj;
			}
		}

		return $addons;
	}

	/**
	 * The get addons is generated by appsbd
	 *
	 * @return mixed|null
	 */
	public function get_addons() {
		$addons = array();
		/**
		 * Its for check is there any change before process
		 *
		 * @since 1.0
		 */
		$addons = apply_filters( 'mini-cart-drawer/filter/addons-list', $addons );
		foreach ( $addons as &$addon ) {
			$addon->footer_btns = array();
			$addon->footer_btns = $this->next_actions( $addon );
		}

		return $addons;
	}

	/**
	 * The is exist plugin is generated by appsbd
	 *
	 * @param mixed $package It is package param.
	 *
	 * @return bool
	 */
	public function is_exist_plugin( $package ) {
		return file_exists( WP_PLUGIN_DIR . '/' . $package );
	}

	/**
	 * The install related plugin is generated by appsbd
	 */
	public function install_related_plugin() {
		$respons       = new Ajax_Confirm_Response();
		$download_link = AppInput::post_value( 'dl_link' );
		$package       = AppInput::post_value( 'package' );
		if ( ! empty( $download_link ) && ! empty( $package ) ) {
			include_once ABSPATH . 'wp-includes/pluggable.php';
			include_once ABSPATH . 'wp-admin/includes/misc.php';
			include_once ABSPATH . 'wp-admin/includes/file.php';
			include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
			include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
			$skin             = new \Automatic_Upgrader_Skin();
			$upgrader         = new \Plugin_Upgrader( $skin );
			$plugin_installed = $upgrader->install( $download_link );
			if ( $plugin_installed ) {
				activate_plugin( $package );
				$this->add_info( 'Plugin installed and activated successfully!' );
				$respons->display_with_response( true, $this->get_addons() );
			} else {
				$this->add_error( 'Plugin installation failed!' );
				$respons->display_with_response( false );
			}
		}
		$this->add_error( 'Invalid request!!' );
		$respons->display_with_response( false );
	}

	/**
	 * The activate related plugin is generated by appsbd
	 */
	public function activate_related_plugin() {
		$respons = new Ajax_Confirm_Response();
		$package = AppInput::post_value( 'package' );
		if ( empty( $package ) ) {
			$this->add_error( 'Invalid Request' );
			$respons->display_with_response( false );
		}
		if ( file_exists( WP_PLUGIN_DIR . '/' . $package ) ) {
			activate_plugin( "$package" );
			$this->add_info( 'Plugin Activated' );
			$respons->display_with_response( true, $this->get_addons() );
		} else {
			$this->add_error( 'Plugin not found' );
			$respons->display_with_response( false );
		}
	}
}
