<?php

namespace KupShop\SKEETBundle\Tests;

use NineDigit\eKasa\Client\ApiRequest;
use NineDigit\eKasa\Client\ApiRequestBuilder;
use NineDigit\eKasa\Client\HttpClientInterface;
use NineDigit\eKasa\Client\Models\Registrations\Receipts\RegisterReceiptRequestContextDto;
use NineDigit\eKasa\Client\Models\Registrations\Receipts\RegisterReceiptResultDto;

final class HttpClientMock implements HttpClientInterface
{
    private $sendCallback;
    private $receiveCallback;

    public function __construct(
        ?callable $sendCallback = null,
        ?callable $receiveCallback = null)
    {
        $this->sendCallback = $sendCallback;
        $this->receiveCallback = $receiveCallback;
    }

    public function send(ApiRequest $request): void
    {
        if (is_callable($this->sendCallback)) {
            ($this->sendCallback)($request);
        }
    }

    public function receive(ApiRequest $request, $type)
    {
        if (is_callable($this->receiveCallback)) {
            return call_user_func($this->receiveCallback, $request, $type);
        }
    }

    public function registerReceipt(RegisterReceiptRequestContextDto $context): RegisterReceiptResultDto
    {
        $apiRequest = ApiRequestBuilder::createPost('/v1/requests/receipts')
          ->withPayload($context)
          ->build();

        return $this->receive($apiRequest, RegisterReceiptResultDto::class);
    }
}
