<?php
/**
 * PhotoShelter OAuth DataSource.
 *
 * @package photoshelter-importer
 */

namespace PhotoShelter\Importer\API\OAuth;

use PhotoShelter\Importer\API\RemoteSource;

/**
 * Class DataSource
 */
class DataSource extends RemoteSource {

	/**
	 * Register OAuth application.
	 *
	 * @see https://engineering.photoshelter.com/psapi-v4-doc/#operation/oAuthRegister
	 *
	 * @param array $args Array of arguments.
	 *
	 * @return array|\WP_Error Array on success, \WP_Error on failure.
	 * @since 1.0.0
	 */
	public function register( $args = array() ) {
		$default_args = array(
			'application_name'   => '',
			'company_name'       => '',
			'description'        => '',
			'email'              => '',
			'homepage_uri'       => '',
			'logo_uri'           => '',
			'name'               => '',
			'privacy_policy_uri' => '',
			'redirect_uri'       => '',
			'scopes'             => '',
		);

		$args = array_filter( wp_parse_args( $args, $default_args ) );

		$required_args = array(
			'application_name' => 1,
			'description'      => 1,
			'email'            => 1,
			'name'             => 1,
			'redirect_uri'     => 1,
		);

		$missing_required_fields = array_diff_key( $required_args, $args );

		if ( count( $missing_required_fields ) ) {
			return new \WP_Error(
				'photoshelter-importer-api-oauth-register-missing-fields',
				sprintf(
					/* translators: %1$s: list of required fields */
					__( 'OAuth\register() is missing the following required fields: %1$s', 'photoshelter-importer' ),
					implode( ', ', array_keys( $missing_required_fields ) )
				)
			);
		}

		$body = $args;

		return $this->api_request_post(
			'oauth/register',
			compact( 'body' )
		);
	}

	/**
	 * Gets OAuth token.
	 *
	 * @see https://engineering.photoshelter.com/psapi-v4-doc/#operation/oAuthToken
	 *
	 * @param array $args Array of arguments.
	 *
	 * @return array|\WP_Error Array on success, \WP_Error on failure.
	 * @since 1.0.0
	 */
	public function token( $args = array() ) {
		$default_args = array(
			'code'          => '',
			'client_id'     => '',
			'client_secret' => '',
			'grant_type'    => 'authorization_code',
			'redirect_uri'  => '',
			'refresh_token' => '',
		);

		$args = array_filter( wp_parse_args( $args, $default_args ) );

		// Validate grant_type.
		$grant_type = true === isset( $args['grant_type'] ) ? $args['grant_type'] : 'authorization_code';

		if ( 'authorization_code' !== $grant_type && 'refresh_token' !== $grant_type ) {
			return new \WP_Error(
				'photoshelter-importer-api-oauth-token-invalid-grant-type',
				/* translators: %s: grant type */
				sprintf( __( 'Invalid grant_type: %s', 'photoshelter-importer' ), $grant_type )
			);
		}

		// Validate required fields based on grant type.
		if ( 'authorization_code' === $grant_type ) {
			$required_args = array(
				'code'         => 1,
				'grant_type'   => 1,
				'redirect_uri' => 1,
			);
		} else {
			// refresh_token grant type.
			$required_args = array(
				'refresh_token' => 1,
				'grant_type'    => 1,
				'client_id'     => 1,
				'client_secret' => 1,
				'redirect_uri'  => 1,
			);
		}

		$missing_required_fields = array_diff_key( $required_args, $args );

		if ( 0 < count( $missing_required_fields ) ) {
			return new \WP_Error(
				'photoshelter-importer-api-oauth-token-missing-fields',
				sprintf(
					/* translators: %1$s: list of required fields */
					__( 'OAuth\token() is missing the following required fields: %1$s', 'photoshelter-importer' ),
					implode( ', ', array_keys( $missing_required_fields ) )
				)
			);
		}

		$body = $args;

		return $this->api_request_post(
			'oauth/token',
			compact( 'body' )
		);
	}

	/**
	 * Revoke OAuth token.
	 *
	 * @see https://engineering.photoshelter.com/psapi-v4-doc/#operation/oAuthRevoke
	 *
	 * @param array $args Array of arguments.
	 *
	 * @return array|\WP_Error Array on success, \WP_Error on failure.
	 * @since 1.3.0
	 */
	public function revoke( $args = array() ) {
		$default_args = array(
			'client_id'     => '',
			'client_secret' => '',
			'token'         => '',
		);

		$args = array_filter( wp_parse_args( $args, $default_args ) );

		$required_args = array(
			'client_id'     => 1,
			'client_secret' => 1,
			'token'         => 1,
		);

		$missing_required_fields = array_diff_key( $required_args, $args );

		if ( 0 < count( $missing_required_fields ) ) {
			return new \WP_Error(
				'photoshelter-importer-api-oauth-revoke-missing-fields',
				sprintf(
					/* translators: %1$s: list of required fields */
					__( 'OAuth\revoke() is missing the following required fields: %1$s', 'photoshelter-importer' ),
					implode( ', ', array_keys( $missing_required_fields ) )
				)
			);
		}

		$body = $args;

		return $this->api_request_post(
			'oauth/revoke',
			compact( 'body' )
		);
	}
}
