<?php
/**
 * The WordAi service functionality. The extended methods for PRO.
 *
 * @link       http://themeisle.com
 * @since      3.0.0
 *
 * @package    feedzy-rss-feeds-pro
 * @subpackage feedzy-rss-feeds-pro/includes/admin
 */

/**
 * Class Feedzy_Rss_Feeds_Pro_Wordai
 */
class Feedzy_Rss_Feeds_Pro_Wordai implements Feedzy_Rss_Feeds_Pro_Services_Interface {

	/**
	 * The API options.
	 *
	 * @since   1.3.1
	 * @access  private
	 * @var     array $options The API options.
	 */
	private $options = array();

	/**
	 * The API errors.
	 *
	 * @since   1.3.1
	 * @access  private
	 * @var     array $errors The API errors.
	 */
	private $errors = array();

	/**
	 * Init the API.
	 *
	 * @since   1.3.1
	 * @access  public
	 * @param   string $email The API email.
	 * @param   string $hash The API hash-ed pass.
	 */
	public function init( $email = '', $hash = '' ) {
		$this->set_api_option( 'email', $email );
		$this->set_api_option( 'hash', $hash );
	}

	/**
	 * Set an option key and value.
	 *
	 * @since   1.3.1
	 * @access  public
	 * @param   string $key The option key.
	 * @param   string $value The option value.
	 */
	public function set_api_option( $key, $value ) {
		$this->options[ $key ] = $value;
	}

	/**
	 * Get an option by key.
	 *
	 * @since   1.3.1
	 * @access  public
	 * @param   string $key The option key.
	 * @return bool|mixed
	 */
	public function get_api_option( $key ) {
		if ( isset( $this->options[ $key ] ) ) {
			return $this->options[ $key ];
		}
		return false;
	}

	/**
	 * Verify API status.
	 *
	 * @since   1.3.1
	 * @access  public
	 * @return bool|mixed
	 */
	public function check_api() {
		$response = wp_remote_post(
			'http://wordai.com/users/account-api.php', array(
				'method' => 'POST',
				'timeout' => 120,
				'redirection' => 5,
				'httpversion' => '1.0',
				'blocking' => true,
				'headers' => array(),
				'body' => array(
					'email' => $this->options['email'],
					'hash' => $this->options['hash'],
					'output' => 'json',
				),
				'cookies' => array(),
			)
		);

		if ( is_wp_error( $response ) ) {
			$error_message = $response->get_error_message();
			$this->errors[] = array(
				'type' => 'ERROR',
				'message' => 'Something went wrong: ' . $error_message,
			);
		} else {
			$decode_response = json_decode( $response['body'], true );
			if ( isset( $decode_response['error'] ) && $decode_response['error'] != '' ) {
				$this->errors[] = array(
					'type' => 'ERROR',
					'message' => $decode_response['error'],
				);
				return $decode_response;
			} else {
				return $decode_response;
			}
		}
		return false;
	}

	/**
	 * Call API.
	 *
	 * @since   1.3.1
	 * @access  public
	 * @param   array $args Args to pass to API call.
	 * @return bool|mixed
	 */
	public function call_api( $args ) {
		$quality = 'Regular';
		if ( isset( $args['quality'] ) && in_array( $args['quality'], array( 'Regular', 'Readable', 'Very Readable' ) ) ) {
			$quality = $args['quality'];
		}

		if ( isset( $args['text'] ) && isset( $this->options['email'] ) && isset( $this->options['hash'] ) ) {
			$response = wp_remote_post(
				'http://wordai.com/users/turing-api.php', array(
					'method' => 'POST',
					'timeout' => 120,
					'redirection' => 5,
					'httpversion' => '1.0',
					'blocking' => true,
					'headers' => array(),
					'body' => array(
						's' => $args['text'],
						'quality' => $quality,
						'sentence' => 'on',
						'returnspin' => 'true',
						'email' => $this->options['email'],
						'hash' => $this->options['hash'],
						'output' => 'json',
					),
					'cookies' => array(),
				)
			);

			if ( is_wp_error( $response ) ) {
				$error_message = $response->get_error_message();
				$this->errors[] = array(
					'type' => 'ERROR',
					'message' => 'Something went wrong: ' . $error_message,
				);
			} else {
				$decode_response = json_decode( $response['body'], true );
				if ( isset( $decode_response['error'] ) && $decode_response['error'] != '' ) {
					$this->errors[] = array(
						'type' => 'ERROR',
						'message' => $decode_response['error'],
					);
				} else {
					return $decode_response;
				}
			}
		} else {
			$this->errors[] = array(
				'type' => 'FATAL',
				'message' => 'Not all variables set!',
			);
		}// End if().
		return false;
	}

	/**
	 * Return erros.
	 *
	 * @since   1.3.1
	 * @access  public
	 * @return array|bool
	 */
	public function get_api_errors() {
		if ( count( $this->errors ) > 0 ) {
			return $this->errors;
		}
		return false;
	}
}
