<?php

namespace Yeepay\Yop\Sdk\Service\Mer\Model;

use Yeepay\Yop\Sdk\Http\Headers;
use Yeepay\Yop\Sdk\Internal\DefaultRequest;
use Yeepay\Yop\Sdk\Internal\Request;
use Yeepay\Yop\Sdk\Model\Transform\RequestMarshaller;
use Yeepay\Yop\Sdk\Utils\ObjectSerializer;
use Yeepay\Yop\Sdk\Utils\UUIDUtils;

class AuthorizeSignRequestMarshaller implements RequestMarshaller
{

    /**
     * @var AuthorizeSignRequestMarshaller
     */
    private static $instance;

    public static function __init()
    {
        self::$instance = new AuthorizeSignRequestMarshaller();
    }

    /**
     * @return AuthorizeSignRequestMarshaller
     */
    public static function getInstance()
    {
        return self::$instance;
    }

    /**
     * @var string
     */
    private $serviceName = 'Mer';

    /**
     * @var string
     */
    private $httpMethod = 'POST';

    /**
     * @var string
     */
    private $resourcePath = '/rest/v2.0/mer/authorize/sign';

    /**
     * @var string
     */
    private $contentType = 'application/x-www-form-urlencoded';

    /**
     * @param  AuthorizeSignRequest  $request
     * @return Request
     */
    public function marshal($request)
    {
        $internalRequest = new DefaultRequest($this->serviceName);
        $internalRequest->setResourcePath($this->resourcePath);
        $internalRequest->setHttpMethod($this->httpMethod);
        if (!empty($request->getRequestConfig()) && !empty($request->getRequestConfig()->getCustomRequestHeaders())) {
            foreach ($request->getRequestConfig()->getCustomRequestHeaders() as $name => $value) {
                $internalRequest->addHeader($name, $value);
            }
        }
        if (!isset($internalRequest->getHeaders()[Headers::YOP_REQUEST_ID])) {
            $internalRequest->addHeader(Headers::YOP_REQUEST_ID, UUIDUtils::uuid());
        }
        if ($request->getRequestNo() != null) {
            $internalRequest->addParameter('requestNo',
                ObjectSerializer::sanitizeForSerialization($request->getRequestNo(), 'string'));
        }
        if ($request->getAuthorizeMerchantNo() != null) {
            $internalRequest->addParameter('authorizeMerchantNo',
                ObjectSerializer::sanitizeForSerialization($request->getAuthorizeMerchantNo(), 'string'));
        }
        if ($request->getBizType() != null) {
            $internalRequest->addParameter('bizType',
                ObjectSerializer::sanitizeForSerialization($request->getBizType(), 'string'));
        }
        if ($request->getNotifyUrl() != null) {
            $internalRequest->addParameter('notifyUrl',
                ObjectSerializer::sanitizeForSerialization($request->getNotifyUrl(), 'string'));
        }
        $internalRequest->addHeader(Headers::CONTENT_TYPE, $this->contentType);

        return $internalRequest;
    }

}

AuthorizeSignRequestMarshaller::__init();
