<?php

namespace Tops\AmazonSellingPartnerAPI\Api;

class OrdersApi extends BaseApi
{
    /**
     * Get Orders Api
     *
     * @param array $params array of input params
     * @param string $params['CreatedAfter'] A date used for selecting orders created after (or at) a specified time. Only orders placed after the specified time are returned. Either the CreatedAfter parameter or the LastUpdatedAfter parameter is required. Both cannot be empty. The date must be in ISO 8601 format.
     * @param string $params['CreatedBefore'] A date used for selecting orders created before (or at) a specified time. Only orders placed before the specified time are returned. The date must be in ISO 8601 format.
     * @param string $params['LastUpdatedAfter'] A date used for selecting orders that were last updated after (or at) a specified time. An update is defined as any change in order status, including the creation of a new order. Includes updates made by Amazon and by the seller. The date must be in ISO 8601 format.
     * @param string $params['LastUpdatedBefore'] A date used for selecting orders that were last updated before (or at) a specified time. An update is defined as any change in order status, including the creation of a new order. Includes updates made by Amazon and by the seller. The date must be in ISO 8601 format.
     * @param string $params['OrderStatuses'] A list of OrderStatus values used to filter the results. (Possible values: PendingAvailability, Pending, Unshipped, PartiallyShipped, Shipped, InvoiceUnconfirmed, Canceled)    )
     * @param string $params['MarketplaceIds'] A list of MarketplaceId values.
     * @param string $params['FulfillmentChannels'] A list that indicates how an order was fulfilled. Filters the results by fulfillment channel. Possible values: FBA (Fulfillment by Amazon); SellerFulfilled (Fulfilled by the seller).
     * @param string $params['PaymentMethods'] A list of payment method values. Used to select orders paid using the specified payment methods. Possible values: COD (Cash on delivery); CVS (Convenience store payment); Other (Any payment method other than COD or CVS).
     * @param string $params['BuyerEmail'] The email address of a buyer. Used to select orders that contain the specified email address.
     * @param string $params['SellerOrderId'] An order identifier that is specified by the seller. Used to select only the orders that match the order identifier. If SellerOrderId is specified, then FulfillmentChannels, OrderStatuses, PaymentMethod, LastUpdatedAfter, LastUpdatedBefore, and BuyerEmail cannot be specified.
     * @param string $params['MaxResultsPerPage'] A number that indicates the maximum number of orders that can be returned per page. Value must be 1 - 100. Default 100.
     * @param string $params['EasyShipShipmentStatuses'] A list of EasyShipShipmentStatus values. Used to select Easy Ship orders with statuses that match the specified values.
     * @param string $params['NextToken']  	A string token returned in the response of your previous request.
     * @param string $params['AmazonOrderIds'] A list of AmazonOrderId values. An AmazonOrderId is an Amazon-defined order identifier, in 3-7-7 format.
     *
     * @return Json
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref:- https://github.com/amzn/selling-partner-api-docs/blob/main/references/orders-api/ordersV0.md#getorders
     */
    public function getOrders(array $params)
    {
        // check the required parameter 'MarketplaceIds' is set
        if (!array_key_exists('MarketplaceIds', $params) || null == $params['MarketplaceIds']) {
            throw new \InvalidArgumentException('Missing the required parameter $MarketplaceIds when calling getOrders');
        }
        $resource_path = '/orders/v0/orders';
       
        $url = $this->config->getHost() . $resource_path;
        $http_body = '';
        $query_params = [];
        $method = 'GET';

        if (array_key_exists('CreatedAfter', $params) && $params['CreatedAfter'] !== null) {
            $query_params['CreatedAfter'] = $params['CreatedAfter'];
        }
        if (array_key_exists('CreatedBefore', $params) && $params['CreatedBefore'] !== null) {
            $query_params['CreatedBefore'] = $params['CreatedBefore'];
        }
        if (array_key_exists('LastUpdatedAfter', $params) && $params['LastUpdatedAfter'] !== null) {
            $query_params['LastUpdatedAfter'] = $params['LastUpdatedAfter'];
        }
        if (array_key_exists('LastUpdatedBefore', $params) && $params['LastUpdatedBefore'] !== null) {
            $query_params['LastUpdatedBefore'] = $params['LastUpdatedBefore'];
        }

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

        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('FulfillmentChannels', $params) && $params['FulfillmentChannels'] !== null) {
            $query_params['FulfillmentChannels'] = is_array($params['FulfillmentChannels']) ? implode(',', $params['FulfillmentChannels']) : $params['FulfillmentChannels'];
        }

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

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

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

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

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

        $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 Get Order
     *
     * @param string $order_id An Amazon-defined order identifier, in 3-7-7 format.
     *
     * @return json
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref :- https://github.com/amzn/selling-partner-api-docs/blob/main/references/orders-api/ordersV0.md#getorder
     */
    public function getOrder($order_id)
    {
        // check the required parameter 'order_id' is set
        if (null === $order_id) {
            throw new \InvalidArgumentException('Missing the required parameter $order_id when calling getOrderItemsBuyerInfo');
        }
        $resource_path = '/orders/v0/orders/{orderId}';
        $query_params = [];
        $http_body = '';
        $method = 'GET';

        // path params
        if (null !== $order_id) {
            $resource_path = str_replace(
                '{' . 'orderId' . '}',
                $order_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 Get Order Buyer Info
     *
     * @param string $order_id An orderId is an Amazon-defined order identifier, in 3-7-7 format.
     *
     * @return json
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref :- https://github.com/amzn/selling-partner-api-docs/blob/main/references/orders-api/ordersV0.md#getorderbuyerinfo
     */
    public function getOrderBuyerInfo($order_id)
    {
        // check the required parameter 'order_id' is set
        if (null === $order_id) {
            throw new \InvalidArgumentException('Missing the required parameter $order_id when calling getOrderItemsBuyerInfo');
        }
        $resource_path = '/orders/v0/orders/{orderId}/buyerInfo';
        $query_params = [];
        $http_body = '';
        $method = 'GET';

        // path params
        if (null !== $order_id) {
            $resource_path = str_replace(
                '{' . 'orderId' . '}',
                rawurlencode($order_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 Get Order Address
     *
     * @param string $order_id An order_id is an Amazon-defined order identifier, in 3-7-7 format.
     *
     * @return json
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref :- https://github.com/amzn/selling-partner-api-docs/blob/main/references/orders-api/ordersV0.md#getorderaddress
     */
    public function getOrderAddress($order_id)
    {
        // check the required parameter 'order_id' is set
        if (null === $order_id) {
            throw new \InvalidArgumentException('Missing the required parameter $order_id when calling getOrderItemsBuyerInfo');
        }

        $resource_path = '/orders/v0/orders/{orderId}/address';
        $query_params = [];
        $http_body = '';
        $method = 'GET';

        // path params
        if (null !== $order_id) {
            $resource_path = str_replace(
                '{' . 'orderId' . '}',
                rawurlencode($order_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 Get Order Items
     *
     * @param string $order_id An Amazon-defined order_id identifier, in 3-7-7 format.
     *
     * @param string next_token A string token returned in the response of your previous request.
     *
     * @return json
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref :- https://github.com/amzn/selling-partner-api-docs/blob/main/references/orders-api/ordersV0.md#getorderitems
     */
    public function getOrderItems($order_id, $next_token = null)
    {
        // check the required parameter 'order_id' is set
        if (null === $order_id) {
            throw new \InvalidArgumentException('Missing the required parameter $order_id when calling getOrderItemsBuyerInfo');
        }
        $resource_path = '/orders/v0/orders/{orderId}/orderItems';
        $query_params = [];
        $http_body = '';
        $method = 'GET';

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

        // if next token available then add in query params
        if ($next_token) {
            $query_params['NextToken'] = $next_token;
        }


        $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 Order Items BuyerInfo
     *
     * @param string $order_id An Amazon-defined order identifier, in 3-7-7 format
     *
     * @param string $next_token A string token returned in the response of your previous request.
     *
     * @return json
     *
     * @author Chetan Meena <chetan@topsinfosolutions.com>
     *
     * About more Ref :- https://github.com/amzn/selling-partner-api-docs/blob/main/references/orders-api/ordersV0.md#getorderitemsbuyerinfo
     */
    public function getOrderItemsBuyerInfo($order_id, $next_token = null)
    {
        // check the required parameter 'order_id' is set
        if (null === $order_id) {
            throw new \InvalidArgumentException('Missing the required parameter $order_id when calling getOrderItemsBuyerInfo');
        }

        $resource_path = '/orders/v0/orders/{orderId}/orderItems/buyerInfo';
        $query_params = [];
        $http_body = '';
        $method = 'GET';

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

        if ($next_token) {
            $query_params['NextToken'] = $next_token;
        }

        $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);
    }
}
