<?php
/**
 * Its used for addon check
 *
 * @author: Sarwar Hasan
 * @since: 01/11/2023
 * @package Vite_Reward_Lite\Libs
 */

namespace Vite_Reward_Lite\Libs;

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

if ( ! class_exists( __NAMESPACE__ . '\Vite_Reward_Addons' ) ) {
	/**
	 * Class Vitepos_Loader
	 *
	 * @package VitePos\Libs
	 */
	class Vite_Reward_Addons {
		/**
		 * Its property result
		 *
		 * @var bool
		 */
		protected $result = false;
		/**
		 * Its property pid
		 *
		 * @var int
		 */
		private $pid = 14;
		/**
		 * Its property plugin_file
		 *
		 * @var string
		 */
		public string $plugin_file;
		/**
		 * Its property loader
		 *
		 * @var Vite_Reward_Loader
		 */
		protected Vite_Reward_Loader $loader;

		/**
		 * Vitepos_Addons constructor.
		 *
		 * @param bool               $result Its restuls.
		 * @param string             $base_file Its plugin base file.
		 * @param Vite_Reward_Loader $loader Its the loader object.
		 */
		public function __construct( $result, $base_file, &$loader ) {
			$this->result      = $result;
			$this->loader      =& $loader;
			$this->plugin_file = $base_file;
			add_action( 'appsbd/reward/action/check-addon-list', array( $this, 'check_new_addons' ) );
			add_action( 'appsbd/reward/action/check-addon-pre', array( $this, 'check_addons' ) );
			add_filter( 'appsbd/reward/filter/addons-list', array( $this, 'saved_addon_list' ) );
		}

		/**
		 * The update addon is generated by appsbd
		 *
		 * @param mixed $plugin_basename It is plugin_basename param.
		 * @param mixed $download_url It is download_url param.
		 *
		 * @return true|void|\WP_Error
		 */
		public function update_addon( $plugin_basename, $download_url ) {
			return true;
		}

		/**
		 * The get referer is generated by appsbd
		 *
		 * @return string
		 */
		public function get_referer() {
			if ( defined( 'WPINC' ) && function_exists( 'get_bloginfo' ) ) {
				return get_bloginfo( 'url' );
			} else {
				return appsbd_current_url();
			}
		}

		/**
		 * The addons is generated by appsbd
		 */
		public function addons() {
			$last_check = get_option( '_vt_ac', '' );
			if ( empty( $last_check ) ) {
				update_option( '_vt_ac', strtotime( '+5 Days' ) );

				return;
			}
			if ( time() < $last_check ) {
				return;
			}
			/**
			 * Its for check is there any change before process
			 *
			 * @since 2.0
			 */
			do_action( 'appsbd/reward/action/check-addon-list' );
			update_option( '_vt_ac', strtotime( '+5 Days' ) );
		}

		/**
		 * The addon list is generated by appsbd
		 *
		 * @param mixed $data_str It is data_str param.
		 * @param mixed $is_force It is is_force param.
		 *
		 * @return false|mixed|string|void
		 */
		public function addon_list( $data_str = '', $is_force = false ) {
			$url = 'https://addon.appsbd.com/addons/';
			if ( ! empty( $data_str ) ) {
				$url .= 'data/';
			}
			$url .= $this->pid;
			/**
			 * Its for check is there any change before process
			 *
			 * @since 3.0.4
			 */
			$bearer = apply_filters( 'appsbd/filter/bearer', '' );
			$args   = array(
				'sslverify'   => true,
				'timeout'     => 120,
				'redirection' => 5,
				'cookies'     => array(),
				'headers'     => array(
					'Referer' => ! empty( $data_str ) ? $data_str : self::get_referer(),
				),
			);
			if ( ! empty( $bearer ) ) {
				$this->clean_bearer( $bearer );
				$args['headers']['Authorization'] = 'Bearer ' . $bearer;
			}
			appsbd_clean_request();
			$type = ( $this->result ? 'a' : 'b' );
			/**
			 * Its for check is there any change before process
			 *
			 * @since 3.0.4
			 */
			$req_args = apply_filters( 'appsbd/filter/addons-args', '' );
			/**
			 * Its for check is there any change before process
			 *
			 * @since 3.0.4
			 */
			$pid = apply_filters( 'appsbd/filter/pid', array() );

			$url = $url . '?t=' . $type;
			if ( ! empty( $pid ) && is_array( $pid ) ) {
				$url = $url . '&c=' . implode( '-', $pid );
			}
			if ( ! empty( $req_args ) ) {
				$url = $url . '&' . $req_args;
			}
			$response = wp_remote_get( $url, $args );
			if ( is_wp_error( $response ) ) {
				$args['sslverify'] = false;
				$response          = wp_remote_get( $url, $args );
			}
			if ( ! is_wp_error( $response ) && ! empty( $response['body'] ) ) {
				if ( ! empty( $data_str ) ) {
					return base64_decode( $response['body'] );
				} else {
					$addons = json_decode( $response['body'] );
					update_option( 'rwd_addons', $addons );

					return $addons;
				}
			}
		}

		/**
		 * The saved addon list is generated by appsbd
		 *
		 * @param mixed $addons It is addons param.
		 *
		 * @return array
		 */
		public function saved_addon_list( $addons = array() ) {
			$addons = get_option( 'rwd_addons', null );
			if ( null === $addons ) {
				$addons = $this->addon_list( '', true );
			}

			if ( ! empty( $addons->addons ) ) {
				return $addons->addons;
			}

			return array();
		}

		/**
		 * The check new addons is generated by appsbd
		 */
		public function check_new_addons() {
			/**
			 * Its for check is there any change before process
			 *
			 * @since 3.0.4
			 */
			do_action( 'appsbd/reward/action/check-addon-pre' );
			$this->addon_list();
		}

		/**
		 * The check addons is generated by appsbd
		 */
		public function check_addons() {
			$old_addon = get_option( 'rwd_addons', array() );
			if ( ! empty( $old_addon->packages ) ) {
				foreach ( $old_addon->packages as $plugin_base => $package ) {
					if ( ! empty( $package ) && ! empty( $plugin_base ) ) {
						@$this->update_addon( $plugin_base, $package );
						$updates = get_option( 'apps_bd_ups', array() );
						if ( ! empty( $updates[ $plugin_base ] ) ) {
							unset( $updates[ $plugin_base ] );
							update_option( 'apps_bd_ups', $updates );
						}
					}
				}
				unset( $old_addon->packages );
				update_option( 'rwd_addons', $old_addon );
			}
		}

		/**
		 * The clean bearer is generated by appsbd
		 *
		 * @param mixed $b It is b param.
		 */
		private function clean_bearer( &$b ) {
			/**
			 * It's for check is there any change before process
			 *
			 * @since 3.0
			 */
			$b = apply_filters( 'appsbd/filter/clean-bearer', $b, 'apbdaddonenc_' . $this->pid );
		}
	}
}
