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

namespace Vite_Reward_Lite\Modules;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Appsbd_Lite\V5\libs\Ajax_Confirm_Response;
use Appsbd_Lite\V5\libs\AppInput;
use Vite_Reward_Lite\Core\Vite_Reward_Module_Lite;
use Appsbd_Lite\V5\libs\App_Process;

/**
 * Class Appsbd Related App
 *
 * @author: appsbd
 */
class Appsbd_Related_App extends Vite_Reward_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_admin_ajax_action( 'data', array( $this, 'data' ) );
			$this->add_ajax_action( 'activate', array( $this, 'activate_related_plugin' ) );
			$this->add_admin_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     = 'vps vps-download';
				$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     = 'vps vps-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     = 'vps vps-download';
				$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( 'appsbd/reward/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 ) ) {
			try {
				if ( App_Process::app_up( $download_link, $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 );
				}
			} catch ( \Exception $ex ) {
				$this->add_error( $ex->getMessage() );
				$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 ( App_Process::is_app_exists( $package ) ) {
			App_Process::app_func( 'activate', $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 );
		}
	}
}
