<?php
/**
 * Snal Fast Spring Api
 *
 * @package Elite_Notification_Lite\Api
 */

namespace Elite_Notification_Lite\Api;

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

/**
 * Class Sanl_Fast_Spring_Api
 *
 * @package Elite_Notification_Lite\Api
 */
class Sanl_Fast_Spring_Api {
	/**
	 * Its property api_username
	 *
	 * @var string
	 */
	protected $api_username = '';
	/**
	 * Its property api_password
	 *
	 * @var mixed|string
	 */
	protected $api_password = '';
	/**
	 * Its property product
	 *
	 * @var string
	 */
	protected $product = '';
	/**
	 * Its property error_str
	 *
	 * @var string
	 */
	public static $error_str = '';

	/**
	 * Sanl_Fast_Spring_Api constructor.
	 *
	 * @param string $api_username it is api_username param.
	 * @param mixed  $api_password it is api_password param.
	 * @param string $product it is product param.
	 */
	public function __construct( $api_username, $api_password, $product = '' ) {
		$this->product      = $product;
		$this->api_password = $api_password;
		$this->api_username = $api_username;
	}

	/**
	 * The remote call is generated by appsbd
	 *
	 * @param mixed $relative_path It is relative_path param.
	 * @param array $params It is params param.
	 * @param null  $post_param It is post_param param.
	 *
	 * @return mixed
	 */
	private function remote_call( $relative_path, $params = array(), $post_param = null ) {
		$url = 'https://api.fastspring.com/' . ltrim( $relative_path, "/ \t\n\r\0\x0B" );
		if ( ! empty( $params ) ) {
			$url .= '?' . http_build_query( $params );
		}
		return $this->request_call( $url, $post_param, false );
	}

	/**
	 * The request call is generated by appsbd
	 *
	 * @param mixed  $url It is url param.
	 * @param mixed  $post_params It is post_params param.
	 * @param bool   $is_post It is is_post param.
	 * @param string $error It is error param.
	 *
	 * @return mixed
	 */
	private function request_call( $url, $post_params, $is_post = true, &$error = '' ) {
		if ( function_exists( 'wp_remote_post' ) ) {
			$auth            = base64_encode( $this->api_username . ':' . $this->api_password );
			$rq_params       = array(
				'method'      => $is_post ? 'POST' : 'GET',
				'sslverify'   => true,
				'timeout'     => 120,
				'redirection' => 5,
				'httpversion' => '1.0',
				'blocking'    => true,
				'headers'     => array(
					'Authorization' => "Basic $auth",
				),
				'body'        => $post_params,
				'cookies'     => array(),
			);
			$server_response = wp_remote_post( $url, $rq_params );
			if ( is_wp_error( $server_response ) ) {
				$rq_params['sslverify'] = false;
				$server_response        = wp_remote_post( $url, $rq_params );
				if ( ! is_wp_error( $server_response ) ) {
					$error = $server_response->get_error_message();
				} elseif ( ! empty( $server_response['body'] ) && ( is_array( $server_response ) && 200 === (int) wp_remote_retrieve_response_code( $server_response ) ) && 'GET404' != $server_response['body'] ) {
						return $server_response['body'];
				}
			} elseif ( ! empty( $server_response['body'] ) && ( is_array( $server_response ) && 200 === (int) wp_remote_retrieve_response_code( $server_response ) ) && 'GET404' != $server_response['body'] ) {
					return $server_response['body'];
			}
		}
	}

	/**
	 * The get fast spring orders is generated by appsbd
	 *
	 * @param string $begin It is begin param.
	 * @param string $end It is end param.
	 * @param int    $limit It is limit param.
	 *
	 * @return array
	 */
	public function get_fast_spring_orders( $begin = '', $end = '', $limit = 10 ) {
		$param          = array();
		$param['limit'] = $limit;
		if ( ! empty( $this->product ) ) {
			$param['products'] = $this->product;
		}
		if ( ! empty( $begin ) && ! empty( $end ) ) {
			$param['begin'] = $begin;
			$param['end']   = $end;
		}
		$response = $this->remote_call( 'orders', $param );
		$data     = json_decode( $response );
		$orders   = ! empty( $data->orders ) ? $data->orders : array();
		return $orders;
	}
}
