<?php

/**
 * Fontis Australia Post API client library for PHP
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/osl-3.0.php
 *
 * @category   Fontis
 * @package    Fontis_Auspost
 * @copyright  Copyright (c) 2019 Fontis Pty. Ltd. (https://www.fontis.com.au)
 * @license    https://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */
declare (strict_types=1);
namespace OctolizeShippingAustraliaPostVendor\Fontis\Auspost\Api\Postage\International\Letter\Services;

use Exception;
use OctolizeShippingAustraliaPostVendor\Fontis\Auspost\Api\Postage\Domestic\Letter\Cost\ParseJsonError;
use OctolizeShippingAustraliaPostVendor\Fontis\Auspost\Exception\EmptyServiceResponse;
use OctolizeShippingAustraliaPostVendor\Fontis\Auspost\HttpClient\HttpApi;
use OctolizeShippingAustraliaPostVendor\Fontis\Auspost\Model\Postage\Option;
use OctolizeShippingAustraliaPostVendor\Fontis\Auspost\Model\Postage\OptionCollection;
use OctolizeShippingAustraliaPostVendor\Fontis\Auspost\Model\Postage\Service;
use OctolizeShippingAustraliaPostVendor\Fontis\Auspost\Model\Postage\ServiceCollection;
use OctolizeShippingAustraliaPostVendor\Psr\Http\Message\ResponseInterface;
class ServiceRequest
{
    /**
     * @var HttpApi
     */
    private $httpClient;
    /**
     * @param HttpApi $httpClient
     */
    public function __construct(HttpApi $httpClient)
    {
        $this->httpClient = $httpClient;
    }
    /**
     * @param GetServiceParams $params
     * @return ServiceCollection
     */
    public function listServices(GetServiceParams $params): ServiceCollection
    {
        $apiParams = $this->generateApiParam($params);
        $response = $this->httpClient->get('postage/letter/international/service', $apiParams);
        $responseBody = $this->parseResponse($response);
        if (!isset($responseBody["services"]) || !isset($responseBody["services"]["service"]) || !count($responseBody["services"]["service"])) {
            return new ServiceCollection([]);
        }
        $result = [];
        foreach ($responseBody["services"]["service"] as $serviceItem) {
            $options = [];
            $optionCollection = null;
            if (!empty($serviceItem["options"])) {
                if (count($serviceItem["options"]) > 0) {
                    $optionList = $serviceItem["options"]["option"];
                } else {
                    $optionList = $serviceItem["options"];
                }
                foreach ($optionList as $optionListItem) {
                    if (!isset($optionListItem["code"]) || !isset($optionListItem["name"])) {
                        continue;
                    }
                    $optionItem = new Option($optionListItem["code"], $optionListItem["name"]);
                    array_push($options, $optionItem);
                }
                $optionCollection = new OptionCollection($options);
            }
            $serviceName = $serviceItem["name"] ?? '';
            $serviceCode = $serviceItem["code"] ?? '';
            $servicePrice = isset($serviceItem["price"]) ? (float) $serviceItem["price"] : 0;
            $serviceExtraCover = isset($serviceItem["max_extra_cover"]) ? (float) $serviceItem["max_extra_cover"] : 0;
            $serviceOption = $optionCollection ?? null;
            $service = new Service($serviceCode, $serviceName, $servicePrice, $serviceExtraCover, $serviceOption);
            array_push($result, $service);
        }
        $serviceCollection = new ServiceCollection($result);
        return $serviceCollection;
    }
    /**
     * @param ResponseInterface $response
     * @return array
     * @throws Exception If invalid response.
     */
    private function parseResponse(ResponseInterface $response): array
    {
        $responseBody = (string) $response->getBody();
        if (empty($responseBody)) {
            throw new EmptyServiceResponse("The response body is empty.");
        }
        $result = json_decode($responseBody, \true);
        if (json_last_error() !== \JSON_ERROR_NONE) {
            throw new ParseJsonError('Error parsing JSON: ' . json_last_error_msg());
        }
        return $result;
    }
    /**
     * @param GetServiceParams $params
     * @return array
     */
    private function generateApiParam(GetServiceParams $params): array
    {
        $urlParams = ['country_code' => $params->getCountryCode()];
        if (!empty($params->getWeight())) {
            $urlParams['weight'] = $params->getWeight();
        }
        return $urlParams;
    }
}
