package com.atlassian.bitbucket.internal.scm.git.lfs.rest;

import com.atlassian.bitbucket.internal.scm.git.lfs.GitLfsService;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.rest.util.ResourcePatterns;
import com.atlassian.bitbucket.rest.util.ResponseFactory;

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

@Path("/admin")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class GitLfsAdminResource {

    private final GitLfsService gitLfsService;

    public GitLfsAdminResource(GitLfsService gitLfsService) {
        this.gitLfsService = gitLfsService;
    }

    @PUT
    @Path("/enabled")
    public Response enableLfs() {
        gitLfsService.enable();
        return ResponseFactory.noContent().build();
    }

    @DELETE
    @Path("/enabled")
    public Response disableLfs() {
        gitLfsService.disable();
        return ResponseFactory.noContent().build();
    }

    @GET
    @Path("/enabled")
    public Response getEnabled() {
        if (gitLfsService.isEnabled()) {
            return ResponseFactory.ok().build();
        } else {
            return ResponseFactory.notFound().build();
        }
    }

    @PUT
    @Path(ResourcePatterns.REPOSITORY_URI + "/enabled")
    public Response enableLfs(@Context Repository repository) {
        gitLfsService.enable(repository);
        return ResponseFactory.noContent().build();
    }

    @DELETE
    @Path(ResourcePatterns.REPOSITORY_URI + "/enabled")
    public Response disableLfs(@Context Repository repository) {
        gitLfsService.disable(repository);
        return ResponseFactory.noContent().build();
    }

    @GET
    @Path(ResourcePatterns.REPOSITORY_URI + "/enabled")
    public Response getEnabled(@Context Repository repository) {
        if (gitLfsService.isEnabled(repository)) {
            return ResponseFactory.ok().build();
        } else {
            return ResponseFactory.notFound().build();
        }
    }
}
