package leigh.tansu.sdk.v1;

import leigh.eo.EO;
import leigh.esp.sdk.ContentPart;
import leigh.esp.sdk.ESPClient;
import leigh.esp.sdk.link23.Link23Response;
import leigh.mecha.lang.FutureResult;
import leigh.tansu.sdk.v1.schema.BucketCreateRequestElementDAO;
import leigh.tansu.sdk.v1.schema.ObjectGetRequestElementDAO;
import leigh.tansu.sdk.v1.schema.ObjectPutRequestElementDAO;

import java.io.InputStream;
import java.util.ArrayList;

public class TansuClient {
    private final ESPClient ctx;
    public final static String API_BASE = "/tansu/v1";
    public final static String API_BUCKET_CREATE = "/bucket/create";
    public final static String API_OBJECT_GET = "/obj/get";
    public final static String API_OBJECT_PUT = "/obj/put";
    public final static String MULTIPART_BLOB = "leigh.tansu.blob";

    public TansuClient(ESPClient ctx) {
        this.ctx = ctx;
    }

    public FutureResult<Link23Response> bucketCreate(String bucketId) {
        EO request = BucketCreateRequestElementDAO.create();
        BucketCreateRequestElementDAO.setId(request, bucketId);
        return ctx.apiRequest(API_BASE + API_BUCKET_CREATE, request);
    }

    public FutureResult<Link23Response> objectPut(String bucketId, String objId, InputStream data, String contentType) {
        EO request = ObjectPutRequestElementDAO.create();
        ObjectPutRequestElementDAO.setBucketId(request, bucketId);
        ObjectPutRequestElementDAO.setId(request, objId);

        ArrayList<ContentPart> parts = new ArrayList<>();
        parts.add(new ContentPart(data, MULTIPART_BLOB, contentType, false));
        return ctx.apiRequest(API_BASE + API_OBJECT_PUT, request, parts);
    }

    public FutureResult<Link23Response> objectGet(String bucketId, String objId) {
        EO request = ObjectGetRequestElementDAO.create();
        ObjectGetRequestElementDAO.setBucketId(request, bucketId);
        ObjectGetRequestElementDAO.setId(request, objId);
        return ctx.apiRequest(API_BASE + API_OBJECT_GET, request);
    }
}
