package ru.zetit.bridgeandjoins.crypto;

import io.grpc.Status;
import io.grpc.stub.StreamObserver;
import ru.zetit.bridgeandjoins.crypto.v1.CryptoServiceGrpc;
import ru.zetit.bridgeandjoins.crypto.v1.HealthRequest;
import ru.zetit.bridgeandjoins.crypto.v1.HealthResponse;
import ru.zetit.bridgeandjoins.crypto.v1.SignRequest;
import ru.zetit.bridgeandjoins.crypto.v1.SignResponse;
import ru.zetit.bridgeandjoins.crypto.v1.VerifyRequest;
import ru.zetit.bridgeandjoins.crypto.v1.VerifyResponse;

/**
 * gRPC-биндинг: делегирует на хендлеры. Сам по себе не реализует
 * криптографию.
 */
public final class CryptoServiceImpl extends CryptoServiceGrpc.CryptoServiceImplBase {

    private final VerifyHandler verify;
    private final SignHandler sign;
    private final HealthHandler health;

    public CryptoServiceImpl(VerifyHandler verify, SignHandler sign, HealthHandler health) {
        this.verify = verify;
        this.sign = sign;
        this.health = health;
    }

    @Override
    public void verifyXMLDSig(VerifyRequest request, StreamObserver<VerifyResponse> obs) {
        try {
            obs.onNext(verify.handle(request));
            obs.onCompleted();
        } catch (Exception e) {
            obs.onError(Status.INTERNAL.withDescription(e.getMessage()).asException());
        }
    }

    @Override
    public void signXMLDSig(SignRequest request, StreamObserver<SignResponse> obs) {
        try {
            obs.onNext(sign.handle(request));
            obs.onCompleted();
        } catch (Exception e) {
            obs.onError(Status.INTERNAL.withDescription(e.getMessage()).asException());
        }
    }

    @Override
    public void health(HealthRequest request, StreamObserver<HealthResponse> obs) {
        obs.onNext(health.handle(request));
        obs.onCompleted();
    }
}
