package ee.cybernetica.uxp.securityserverloadtesting.service.test;

import ee.cybernetica.uxp.securityserverloadtesting.service.types.GetBinaryRequest;
import ee.cybernetica.uxp.securityserverloadtesting.service.types.GetBinaryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import org.springframework.ws.soap.SoapHeaderElement;
import org.springframework.ws.soap.server.endpoint.annotation.SoapHeader;


@Endpoint
public class GetBinaryEndpoint {
    private static final String NAMESPACE_URI = "http://cybernetica.ee/uxp/securityserverloadtesting/service/types";

    private BinaryDataRepository binaryDataRepository;



    @Autowired
    public GetBinaryEndpoint(BinaryDataRepository binaryDataRepository) {
        this.binaryDataRepository = binaryDataRepository;
    }

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getBinaryRequest")
    @ResponsePayload
    public GetBinaryResponse getBinary(
            @RequestPayload GetBinaryRequest request,
            @SoapHeader("{http://x-road.ee/xsd/x-road.xsd}hdrstd")SoapHeaderElement soapHeaderElement
            ) {

        GetBinaryResponse response = new GetBinaryResponse();

        try {
            int size = request.getSize();
            response.setBinaryData(binaryDataRepository.getBinaryData((int) (size*0.75)));
        } catch (Exception ignored) {

        }


        return response;
    }
}
