package com.atlassian.bitbucket.internal.branch.automerge.rest;

import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.bitbucket.internal.branch.automerge.AutoMergeConfig;
import com.atlassian.bitbucket.internal.branch.automerge.AutoMergeService;
import com.atlassian.bitbucket.repository.*;
import com.atlassian.bitbucket.rest.RestResource;
import com.atlassian.bitbucket.rest.util.ResourcePatterns;
import com.atlassian.bitbucket.rest.util.ResponseFactory;
import com.atlassian.bitbucket.rest.util.RestUtils;
import com.atlassian.plugins.rest.common.security.AnonymousAllowed;
import com.sun.jersey.spi.resource.Singleton;
import org.apache.commons.lang3.StringUtils;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path(ResourcePatterns.REPOSITORY_URI + "/automerge")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({RestUtils.APPLICATION_JSON_UTF8})
@Singleton
@AnonymousAllowed
public class AutoMergeResource extends RestResource {

    private final AutoMergeService autoMergeService;
    private final AutoMergeConfig autoMergeConfig;
    private final RefService refService;

    public AutoMergeResource(I18nService i18nService,
                             AutoMergeService autoMergeService,
                             AutoMergeConfig autoMergeConfig,
                             RefService refService) {
        super(i18nService);
        this.autoMergeService = autoMergeService;
        this.autoMergeConfig = autoMergeConfig;
        this.refService = refService;
    }

    @GET
    @Path("path")
    public Response getMergePath(@Context Repository repository, @QueryParam("branchRefId") String branchRefId) {
        if (StringUtils.isBlank(branchRefId)) {
            return ResponseFactory.badRequest("branchRefId",
                    i18nService.getMessage("bitbucket.branch.automerge.rest.error.emptybranchrefid"))
                    .build();
        }
        Branch branch = resolveBranch(repository, branchRefId);
        if (branch == null) {
            return ResponseFactory.badRequest("branchRefId",
                    i18nService.getMessage("bitbucket.branch.automerge.rest.error.nosuchbranch", branchRefId))
                    .build();
        }

        return ResponseFactory.ok(new RestAutoMerge(autoMergeService.getAutoMerge(repository, branch))).build();
    }

    @GET
    @Path("enabled")
    public Response isAutoMergeEnabled(@Context Repository repository) {
        return autoMergeConfig.isEnabled(repository) ?
                ResponseFactory.noContent().build() : ResponseFactory.notFound().build();
    }

    @PUT
    @Path("enabled")
    public Response enableAutoMerge(@Context Repository repository) {
        return setAutoMergeEnabled(repository, true);
    }

    @DELETE
    @Path("enabled")
    public Response disableAutoMerge(@Context Repository repository) {
        return setAutoMergeEnabled(repository, false);
    }

    private Branch resolveBranch(Repository repository, String branchRefId) {
        return (Branch) refService.resolveRef(new ResolveRefRequest.Builder(repository)
                .refId(branchRefId)
                .type(StandardRefType.BRANCH)
                .build());
    }

    private Response setAutoMergeEnabled(Repository repository, boolean enabled) {
        autoMergeConfig.setEnabled(repository, enabled);
        return ResponseFactory.noContent().build();
    }
}
