/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.client;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.elasticsearch.client.ccr.PauseFollowRequest;
import org.elasticsearch.client.ccr.PutFollowRequest;
import org.elasticsearch.client.ccr.ResumeFollowRequest;
import org.elasticsearch.client.ccr.UnfollowRequest;

import java.io.IOException;

import static org.elasticsearch.client.RequestConverters.REQUEST_BODY_CONTENT_TYPE;
import static org.elasticsearch.client.RequestConverters.createEntity;

final class CcrRequestConverters {

    static Request putFollow(PutFollowRequest putFollowRequest) throws IOException {
        String endpoint = new RequestConverters.EndpointBuilder()
            .addPathPart(putFollowRequest.getFollowerIndex())
            .addPathPartAsIs("_ccr", "follow")
            .build();
        Request request = new Request(HttpPut.METHOD_NAME, endpoint);
        request.setEntity(createEntity(putFollowRequest, REQUEST_BODY_CONTENT_TYPE));
        return request;
    }

    static Request pauseFollow(PauseFollowRequest pauseFollowRequest) {
        String endpoint = new RequestConverters.EndpointBuilder()
            .addPathPart(pauseFollowRequest.getFollowerIndex())
            .addPathPartAsIs("_ccr", "pause_follow")
            .build();
        return new Request(HttpPost.METHOD_NAME, endpoint);
    }

    static Request resumeFollow(ResumeFollowRequest resumeFollowRequest) throws IOException {
        String endpoint = new RequestConverters.EndpointBuilder()
            .addPathPart(resumeFollowRequest.getFollowerIndex())
            .addPathPartAsIs("_ccr", "resume_follow")
            .build();
        Request request = new Request(HttpPost.METHOD_NAME, endpoint);
        request.setEntity(createEntity(resumeFollowRequest, REQUEST_BODY_CONTENT_TYPE));
        return request;
    }

    static Request unfollow(UnfollowRequest unfollowRequest) {
        String endpoint = new RequestConverters.EndpointBuilder()
            .addPathPart(unfollowRequest.getFollowerIndex())
            .addPathPartAsIs("_ccr", "unfollow")
            .build();
        return new Request(HttpPost.METHOD_NAME, endpoint);
    }

}
