<?php

namespace Tops\AmazonSellingPartnerAPI\Api;

use Tops\AmazonSellingPartnerAPI\Helpers\FeedHelper;

/**
 * Version 2021-06-30
 */
class FeedsApi20210630 extends BaseApi
{
    use FeedHelper;
    // Rules for create Feed
    // Step-1 : CreateFeed Doument
    // Step-2: Upload document
    // Step-3: Create feed
    /**
     * Operation Get Feeds
     *
     * @param array $params
     * @param string|array $params['feedTypes'] A list of feed types used to filter feeds. When feedTypes is provided, the other filter parameters (processingStatuses, marketplaceIds, createdSince, createdUntil) and pageSize may also be provided. Either feedTypes or nextToken is required. < Min count : 1 and Max count : 10 >
     * @param string|array $params['marketplaceIds'] A list of marketplace identifiers used to filter feeds. The feeds returned will match at least one of the marketplaces that you specify. < Min count : 1 and Max count : 10 >
     * @param integer $params['pageSize'] The maximum number of feeds to return in a single call. < Minimum  : 1 and Maximum  : 100 >
     * @param array $params['processingStatuses'] A list of processing statuses used to filter feeds.
     * @param string (date-time) $params['createdSince'] The earliest feed creation date and time for feeds included in the response, in ISO 8601 format. The default is 90 days ago. Feeds are retained for a maximum of 90 days.
     * @param string (date-time) $params['createdUntil'] The latest feed creation date and time for feeds included in the response, in ISO 8601 format. The default is now.
     * @param string $params['NextToken'] A string token returned in the response to your previous request. nextToken is returned when the number of results exceeds the specified pageSize value. To get the next page of results, call the getFeeds operation and include this token as the only parameter. Specifying nextToken with any other parameters will cause the request to fail.
     *
     * @return json\
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * about more Ref: https://github.com/amzn/selling-partner-api-docs/blob/main/references/feeds-api/feeds_2021-06-30.md#getfeeds
     */
    public function getFeeds($params)
    {
        $resource_path = '/feeds/2021-06-30/feeds';
        $url = $this->config->getHost() . $resource_path;
        $http_body = '';
        $query_params = [];
        $method = 'GET';

        if (array_key_exists('feedTypes', $params) && $params['feedTypes'] !== null) {
            $query_params['feedTypes'] = $params['feedTypes'];
        }

        if (array_key_exists('marketplaceIds', $params) && $params['marketplaceIds'] !== null) {
            $query_params['marketplaceIds'] = is_array($params['marketplaceIds']) ? implode(',', $params['marketplaceIds']) :  $params['marketplaceIds'];
        }

        if (array_key_exists('pageSize', $params) && $params['pageSize'] !== null) {
            $query_params['pageSize'] = $params['pageSize'];
        }

        if (array_key_exists('processingStatuses', $params) && $params['processingStatuses'] !== null) {
            $query_params['processingStatuses'] = $params['processingStatuses'];
        }

        if (array_key_exists('createdSince', $params) && $params['createdSince'] !== null) {
            $query_params['createdSince'] = $params['createdSince'];
        }

        if (array_key_exists('createdUntil', $params) && $params['createdUntil'] !== null) {
            $query_params['createdUntil'] = $params['createdUntil'];
        }

        if (array_key_exists('NextToken', $params) && $params['NextToken'] !== null) {
            $query_params['NextToken'] = $params['NextToken'];
        }

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }
    
    /**
     * Operation Create Feed
     *
     * @param array $body
     * @param string $body['feedType'] The feed type.
     * @param string|array $body['marketplaceIds '] A list of identifiers for marketplaces that you want the feed to be applied to.
     * @param string $body['inputFeedDocumentId'] The document identifier returned by the createFeedDocument operation. Encrypt and upload the feed document contents before calling the createFeed operation.
     * @param string $body['feedOptions'] Additional options to control the feed. These vary by feed type.
     *
     * @return json
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * about more Ref: https://github.com/amzn/selling-partner-api-docs/blob/main/references/feeds-api/feeds_2021-06-30.md#createfeed
     */
    public function createFeed($body)
    {
        if (null === $body) {
            throw new \InvalidArgumentException('Missing the required parameter $body when calling createFeed');
        }
        
        $resource_path = '/feeds/2021-06-30/feeds';
        $query_params = [];
        $http_body = $body;
        $method = 'POST';

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        $url = $this->config->getHost() . $resource_path;
        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
     * Operation Get Feed
     *
     * @param string $feed_id The identifier for the feed. This identifier is unique only in combination with a seller ID.
     *
     * @return json Returns feed details (including the resultDocumentId, if available) for the feed that you specify.
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * about more ref: https://github.com/amzn/selling-partner-api-docs/blob/main/references/feeds-api/feeds_2021-06-30.md#getfeed
     */
    public function getFeed($feed_id)
    {
        if (null === $feed_id) {
            throw new \InvalidArgumentException('Missing the required parameter $asin when calling getOrderItemsBuyerInfo');
        }
        $resource_path = '/feeds/2021-06-30/feeds/{feedId}';
        $query_params = [];
        $http_body = '';
        $method = 'GET';

        // path params
        if (null !== $feed_id) {
            $resource_path = str_replace(
                '{' . 'feedId' . '}',
                rawurlencode($feed_id),
                $resource_path
            );
        }

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        $url = $this->config->getHost() . $resource_path;
        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }


    /**
     * Operation Cancel Feed
     *
     * @param string $feed_id The identifier for the feed. This identifier is unique only in combination with a seller ID.
     *
     * @return json  Cancelled feeds are returned in subsequent calls to the getFeed and getFeeds operations.
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * about more ref: https://github.com/amzn/selling-partner-api-docs/blob/main/references/feeds-api/feeds_2021-06-30.md#cancelfeed
     */
    public function cancelFeed($feed_id)
    {
        if (null === $feed_id) {
            throw new \InvalidArgumentException('Missing the required parameter $granularity when calling getOrderItemsBuyerInfo');
        }

        $resource_path = '/feeds/2021-06-30/feeds/{feedId}';
        $query_params = [];
        $http_body = '';
        $method = 'DELETE';

        // path params
        if (null !== $feed_id) {
            $resource_path = str_replace(
                '{' . 'feedId' . '}',
                rawurlencode($feed_id),
                $resource_path
            );
        }

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        $url = $this->config->getHost() . $resource_path;
        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
     * Operation Create Feed Document
     *
     * @param array $body
     * @param string $body['contentType'] The content type of the feed. (required)
     * @return json  This operation returns a presigned URL for uploading the feed document contents. It also  returns a feedDocumentId value that you can pass in with a subsequent call to the createFeed operation.
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * about more ref: https://github.com/amzn/selling-partner-api-docs/blob/main/references/feeds-api/feeds_2021-06-30.md#createfeeddocument
     *
     */
    public function createFeedDocument($body)
    {
        if (null === $body) {
            throw new \InvalidArgumentException('Missing the required parameter $body when calling createFeedDocument');
        }

        $resource_path = '/feeds/2021-06-30/documents';
        $query_params = [];
        $http_body = $body;
        $method = 'POST';

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        $url = $this->config->getHost() . $resource_path;
        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }

    /**
     * Operation Get Feed Document
     *
     * @param string $feed_document_id the identifier of the feed document
     *
     * @return json  Returns the information required for retrieving a feed document's contents
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * about more ref: https://github.com/amzn/selling-partner-api-docs/blob/main/references/feeds-api/feeds_2021-06-30.md#getfeeddocument
     */

    public function getFeedDocument($feed_document_id)
    {
        if (null === $feed_document_id) {
            throw new \InvalidArgumentException('Missing the required parameter $feed_document_id when calling etOrderItemsBuyerInfo');
        }

        $resource_path = '/feeds/2021-06-30/documents/{feedDocumentId}';
        $query_params = [];
        $http_body = '';
        $method = 'GET';
        
        // path params
        if (null !== $feed_document_id) {
            $resource_path = str_replace(
                '{' . 'feedDocumentId' . '}',
                rawurlencode($feed_document_id),
                $resource_path
            );
        }

        $curl_headers = $this->generateRequest($resource_path, $query_params, $http_body, $this->config, $method);

        $url = $this->config->getHost() . $resource_path;
        return $this->sendRequest($url, $query_params, $http_body, $curl_headers, $method);
    }
}
