<?php
/**
 * Snal Elite Api
 *
 * @package Elite_Notification_Lite\Api
 */

namespace Elite_Notification_Lite\Api;

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

if ( ! class_exists( 'Sanl_Elite_Api' ) ) {
	/**
	 * Class Sanl_Elite_Api
	 *
	 * @package Elite_Notification_Lite\Api
	 */
	class Sanl_Elite_Api {
		/**
		 * Its property host
		 *
		 * @var string
		 */
		public $host = '';
		/**
		 * Its property api_key
		 *
		 * @var string
		 */
		public $api_key = '';

		/**
		 * Sanl_Elite_Api constructor.
		 *
		 * @param string $host it is host param.
		 * @param string $api_key it is api-key param.
		 */
		public function __construct( $host, $api_key ) {
			$this->host    = $host;
			$this->api_key = $api_key;
		}

		/**
		 * The  request is generated by appsbd
		 *
		 * @param mixed  $relatetive_path It is relatetive_path param.
		 * @param mixed  $data It is data param.
		 * @param string $error It is error param.
		 *
		 * @return mixed|null
		 */
		public function _request( $relatetive_path, $data, &$error = '' ) {
			$response         = new \stdClass();
			$response->status = false;
			$response->msg    = 'Empty Response';
			$final_data       = (array) ( $data );
			if ( ! isset( $final_data['api_key'] ) ) {
				$final_data['api_key'] = $this->api_key;
			}
			$full_url = rtrim( $this->host, '/' ) . '/' . ltrim( $relatetive_path, '/' );
			if ( function_exists( 'wp_remote_post' ) ) {
				$server_response = wp_remote_post(
					$full_url,
					array(
						'method'      => 'POST',
						'sslverify'   => false,
						'timeout'     => 120,
						'redirection' => 5,
						'httpversion' => '1.0',
						'blocking'    => true,
						'headers'     => array(),
						'body'        => $final_data,
						'cookies'     => array(),
					)
				);

				if ( is_wp_error( $server_response ) ) {
					$error = $server_response->get_error_message();

					return null;
				} elseif ( ! empty( $server_response['body'] ) && 'GET404' != $server_response['body'] ) {
						return $this->process_response( $server_response['body'], $error );
				}
			}

			return null;
		}

		/**
		 * The process response is generated by appsbd
		 *
		 * @param mixed  $server_response It is server_response param.
		 * @param string $error It is error param.
		 *
		 * @return mixed|null
		 */
		private function process_response( $server_response, &$error = '' ) {
			if ( ! empty( $server_response ) ) {
				$obj = json_decode( $server_response );
				if ( ! empty( $obj->code ) ) {
					$error = ! empty( $obj->message ) ? $obj->message : $error;

					return null;
				} else {
					return $obj;
				}
			}

			return null;
		}

		/**
		 * The has permission is generated by appsbd
		 *
		 * @param string $error It is error param.
		 *
		 * @return bool
		 */
		public function has_permission( &$error = '' ) {
			$error = 'Unknown';
			$obj   = $this->_request( 'hello', array(), $error );
			if ( ! empty( $obj ) ) {
				if ( $obj->status ) {
					if ( 'Y' == $obj->data->view_license && 'Y' == $obj->data->view_product && 'Y' == $obj->data->view_client ) {
						return true;
					} else {
						$error = "API doesn't have permission to view license, view product and view client, Pleaes allow these permision on Elite Licenser for this API Key";

						return false;
					}
				} else {
					$error = ! empty( $obj->msg ) ? $obj->msg : '';

					return false;
				}
			}

			return false;
		}

		/**
		 * The get license list is generated by appsbd
		 *
		 * @param int    $limit It is limit param.
		 * @param string $error It is error param.
		 *
		 * @return array
		 */
		public function get_license_list( $limit = 10, &$error = '' ) {
			$obj = $this->_request(
				'license/view-all',
				array(
					'limit'      => $limit,
					'lic_status' => 'A,W',
					'order_by'   => 'entry_time',
					'order'      => 'DESC',
				),
				$error
			);
			if ( ! empty( $obj->status ) && ! empty( $obj->data ) ) {
				return $obj->data;
			} else {
				$error = $obj->msg;
			}

			return array();
		}
	}
}
