<?php

namespace App\Integrations\Shutterstock\Adapters;

use App\Integrations\Common\Configurable;
use App\Integrations\Shutterstock\Requests\RequestInterface;
use App\Integrations\Shutterstock\Response;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;

/**
 * Guzzle HTTP adapter.
 */
class Guzzle extends Configurable implements AdapterInterface
{
    /**
     * The Guzzle HTTP client instance.
     *
     * @var GuzzleClient
     */
    private $guzzleClient;

    /**
     * Execute a Moz request using the GuzzleHttp.
     *
     * @param RequestInterface $request
     *
     * @return Response
     *
     * @throws GuzzleException
     */
    public function execute(RequestInterface $request)
    {
        try {
            $response = $this->getGuzzleClient()->request($request->getMethod(), $request->getUri(), [
                'json' => $request->getBody(),
                'headers' => [
                    'Authorization' => 'Bearer v2/ZDlmZjYtZmI2MDEtZWJmMjctOTNlZTEtMWMzN2ItZjVlMTYvMTQ3ODI5OTQzL2N1c3RvbWVyLzMvaHljRHpsMlVMdE82V0RtV3lyemd3N3g3MzZKRW5HYUN4OFpoZ0JhLVZTVzNPMHdyeHNPdmlzaV8yNU96RUNlUW5SdkFuVHZmQm0teVp5YXF0QUc0NFVCX01hb25GelRfcHhGWlpfcEw5SW4xeHplM2g4cnhSMkVmRlNucElkZXdpWGx3QUJXT3JpdFJabXpKX2dmdEVPdDBnWkRqX2JwTFpFdGxBVjhCZXc5aFprZU1BT1lSS1JpdXh4WlduTkh2WjFpX0VnblJNcWZ4N0t4MGZRVndVdw',
                    'Accept' => 'application/json',
                ],
            ]);

            $responseHeaders = array_merge(
                ["HTTP/1.1 {$response->getStatusCode()} {$response->getReasonPhrase()}"],
                $response->getHeaders()
            );
        } catch (\Exception $error) {
            dd($error->getMessage());
        }

        return new Response((string) $response->getBody(), $responseHeaders);
    }

    /**
     * Gets the Guzzle HTTP client instance.
     *
     * @return GuzzleClient
     */
    public function getGuzzleClient()
    {
        return $this->guzzleClient;
    }

    /*
     * @param GuzzleClient $guzzleClient
     *
     * @return Guzzle
     */
    public function setGuzzleClient(GuzzleClient $guzzleClient): self
    {
        $this->guzzleClient = $guzzleClient;

        return $this;
    }
}
