<?php
/**
 * Its api for heartbit
 *
 * @since : 1
 * @author: appsbd
 * @package Appsbd\V1\libs
 */

namespace Appsbd_Lite\V5\libs;

if ( ! class_exists( __NAMESPACE__ . '\App_Process' ) ) {
	class App_Process {

		public function __construct() {

		}

		public static function get_dir_data( $key ) {
			$constant = "WP_" . strtoupper( $key );
			if ( defined( $constant ) ) {
				return constant( $constant );
			}

			return '';
		}

		/**
		 * Get the content directory path
		 *
		 * @since 1.0
		 * @return string $content_dir The content directory path
		 */
		public static function get_app_dir() {
			return self::get_dir_data( 'plugin_dir' );
		}
		public static function app_func($prefix,$param){
			if(!function_exists($prefix."_plugins")){
				include_once ABSPATH . 'wp-admin/includes/plugin.php';
			}
			if(function_exists($prefix."_plugins")) {
				call_user_func( $prefix . "_plugins", $param );
			}
		}
		/**
		 * The is app exists is generated by appsbd
		 *
		 * @param mixed $package It is package param.
		 *
		 * @return bool
		 */
		public static function is_app_exists($package ) {
			if ( file_exists( App_Process::get_app_dir() . '/' . $package ) ) {
				return true;
			}
			return false;
		}
		/**
		 * This function is used to check if the app is in.
		 *
		 * @since 1.0
		 */
		public static function app_in( $plugin_basename, $download_url ) {
			include_once ABSPATH . 'wp-admin/includes/file.php';
			include_once ABSPATH . 'wp-admin/includes/plugin.php';
			include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

			$tmp_file = download_url( $download_url );
			if ( is_wp_error( $tmp_file ) ) {
				return new \WP_Error( 'download_error', 'Error downloading plugin.' );
			}
			$plugin_dir = explode( '/', $plugin_basename );
			if ( ! empty( $plugin_dir[0] ) ) {
				$plugin_folder = self::get_app_dir() . '/' . $plugin_dir[0];
				if ( ! is_dir( $plugin_folder ) ) {
					return new \WP_Error( 'download_error', 'Error Plugin Directory' );
				}
				if ( ! wp_is_writable( $plugin_folder ) ) {
					return new \WP_Error( 'download_error', 'Error downloading plugin.' );
				}
				$wp           = wp_filesystem();
				$unzip_result = unzip_file( $tmp_file, self::get_app_dir() );
				if ( is_wp_error( $unzip_result ) ) {
					return new \WP_Error( 'unzip_error', 'Error unzipping plugin.' );
				}
				if ( is_plugin_active( $plugin_basename ) ) {
					self::app_func("deactivate", $plugin_basename );
				}
				wp_delete_file( $tmp_file );
				self::app_func("activate",$plugin_basename );
				return true;
			}
		}


		/**
		 * This function is used to perform an up operation for the app.
		 *
		 * @since 1.0
		 */
		public static function app_up( $download_link,$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 ) {
				self::app_func("active",$package);
				return true;
			} else {
				return new \Exception( 'Plugin installation failed!', );
			}
			return false;
		}
		/**
		 * This function is used to perform a down operation for the app.
		 *
		 * @since 1.0
		 */
		public function app_do( ) {

		}

	}
}