<?php
/**
 * Mk_Live_Support class.
 *
 * @author  Ross Marandi & Ugur Mirza Zeyrek <mirza@artbees.net>
 * @author  Ayub Adiputra (Modified) <ayub@artbees.net>
 * @package ken/framework/includes
 * @since   4.1.1
 */

if ( ! defined( 'THEME_FRAMEWORK' ) ) {
	exit( 'No direct script access allowed' );
}

/**
 * This class is responsible for live support of VIP users.
 * It communicates with Artbees Portal and checks if user eligible for VIP Live Support.
 *
 * @since 4.1.1
 */
class Mk_Live_Support {
	/**
	 * Logger for errors.
	 *
	 * @var object.
	 */
	private $logger;

	/**
	 * Time interval for checking vip user status from Artbees Portal.
	 *
	 * @var string.
	 */
	private $check_time_interval = '1';
	/*====================== MAIN SECTION ============================*/

	/**
	 * Mk_Live_Support constructor.
	 *
	 * @since 4.1.1
	 */
	public function __construct() {
		add_action( 'admin_footer', array( &$this, 'enqueue_live_support_script' ), 100 );
		$this->logger = new Devbees\BeesLog\logger();
	}

	/**
	 * Returns true if current logged-in user to the admin section eligible for live
	 * support verification.
	 *
	 * @since  4.1.1
	 * @return boolean
	 */
	private function eligible_for_live_support_check() {
		if ( defined( 'ENVATO_HOSTED_SITE' ) ) {
			return false;
		}

		if ( ! $this->get_api_key() ) {
			return false;
		}

		if ( ! $this->user_level_check() ) {
			return false;
		}

		if ( ! $this->is_live_support_active() ) {
			return false;
		}

		return true;
	}

	/**
	 * This method is responsible to check active api-key on jupiter and return it
	 *
	 * @since 4.1.1
	 *
	 * @return mixed Return false if api-key is not exists and return api-key itself if its exist
	 */
	private function get_api_key() {
		$api_key = get_option( 'artbees_api_key' );
		if ( empty( $api_key ) ) {
			return false;
		}
		return $api_key;
	}

	/**
	 * This method is responsible to create http body and post request to ATP and evaluate response
	 *
	 * @return mixed Will return JSON if okay and false if there is a problem on connection
	 *
	 * @since 4.1.1
	 */
	private function request_user_info() {
		$transient_name = 'mk_request_user_info_' . $this->get_api_key();
		$return_result = get_transient( $transient_name );
		if ( $return_result ) {
			return $return_result;
		}
		$url  = 'https://themes.artbees.net/wp-admin/admin-ajax.php';

		$args = array(
			'method'   => 'POST',
			'blocking' => true,
			'timeout'  => 45,
			'headers'  => array(
				'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
			),
			'body'     => array(
				'action'      => 'abb_get_vip_user_by_api_key',
				'api_key'     => $this->get_api_key(),
				'ken_version' => THEME_VERSION,
			),
		);

		$response = wp_remote_post( $url, $args );
		// Check if Artbees Portal Response has Error.
		if ( is_wp_error( $response ) ) {
			$this->logger->debug( 'Live Support - Error in response', $response->get_error_message() );
			return false;
		}

		$result = wp_remote_retrieve_body( $response );
		try {
			$return_result = json_decode( $result, true );
		} catch ( Exception $exception ) {
			// Check if response is not encoded in JSON.
			$this->logger->debug( 'Live Support - Error in decoding', $exception->getMessage() );
			return false;
		}

		$seconds = 3600;
		if ( defined( 'MK_DEV' ) && true === MK_DEV ) {
			$seconds = 1;
		}

		set_transient( $transient_name, $return_result, $seconds * $this->check_time_interval );
		return $return_result;
	}

	/**
	 * Returns true if user not disabled live support and sets live support status if get
	 * parameter provided.
	 *
	 * @since  4.1.1
	 * @return boolean false if disabled.
	 */
	private function is_live_support_active() {
		wp_verify_nonce( 'set_mk_live_support', 'nonce' );
		if ( isset( $_GET['set_mk_live_support'] ) ) {
			if ( '1' === $_GET['set_mk_live_support'] ) {
				update_option( 'mk_ken_live_support', true );
			}
			if ( '0' === $_GET['set_mk_live_support'] ) {
				update_option( 'mk_ken_live_support', false );
			}
		}
		return get_option( 'mk_ken_live_support', true );
	}

	/**
	 * Checks if current user has administrator privileges or not. Multi-site compatible.
	 *
	 * @since  4.1.1
	 *
	 * @return boolean true if current user is admin and false if not.
	 */
	private function user_level_check() {
		if ( current_user_can( 'administrator' ) || current_user_can( 'super_admin' ) ) {
			return true;
		}
		$user = wp_get_current_user();
		if ( $user->user_level > 9 ) {
			return true;
		}
		return false;
	}

	/**
	 * Enqueue live support script and inject variables in it. (it call by admin_footer hook )
	 *
	 * @since 4.1.1
	 */
	public function enqueue_live_support_script() {
		if ( $this->eligible_for_live_support_check() ) {
			$user_info = $this->user_info();

			// Avoid cache isse with updated Jupiter version.
			wp_enqueue_script( 'mk-live-support', THEME_ADMIN_ASSETS_URI . '/js/intercom.js', array( 'jquery' ), THEME_VERSION );

			$support_active = isset( $user_info['support_active'] ) ? $user_info['support_active'] : false;
			$user_status    = isset( $user_info['status'] ) ? $user_info['status'] : false;

			// If user VIP status is true or INTERCOM_SUPPORT_ACTIVE is enabled.
			if ( true === $user_status || true === $support_active ) {
				wp_localize_script( 'mk-live-support', 'mk_atp_user', $user_info );
				return true;
			}

			if ( ! isset( $user_info['status'] ) ) {
				$script = 'console.log( "Live Support not available : An unknown error occurred." );';
				wp_add_inline_script( 'mk-live-support', $script );
			}
		}

		return false;
	}

	/**
	 * Returns necessary variables for customer.
	 *
	 * @since  4.1.1
	 * @return array
	 */
	private function user_info() {
		$user_info = $this->request_user_info();
		if ( $user_info && isset( $user_info['status'] ) ) {
			update_option( 'mk_atp_user', true );
		}
		return $user_info;
	}

}

global $abb_phpunit;
if ( empty( $abb_phpunit ) || false === $abb_phpunit ) {
	new Mk_Live_Support();
}
