package com.atlassian.stash.internal.comment.like.rest;

import com.atlassian.bitbucket.rest.util.ResponseFactory;
import com.atlassian.bitbucket.comment.Comment;
import com.atlassian.stash.internal.comment.like.InternalCommentLikeService;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.rest.util.RestPage;
import com.atlassian.bitbucket.rest.user.RestApplicationUser;
import com.atlassian.bitbucket.user.ApplicationUser;
import com.atlassian.bitbucket.util.Page;
import com.atlassian.bitbucket.util.PageRequest;

import javax.annotation.Nonnull;
import javax.ws.rs.core.Response;

public abstract class AbstractCommentLikeResource {

    protected final InternalCommentLikeService commentLikeService;

    protected AbstractCommentLikeResource(InternalCommentLikeService commentLikeService) {
        this.commentLikeService = commentLikeService;
    }

    @Nonnull
    protected Response getLikers(@Nonnull Repository repository, @Nonnull Comment comment,
                                 @Nonnull PageRequest pageRequest) {
        Page<ApplicationUser> likers = commentLikeService.findByComment(repository, comment, pageRequest);

        return ResponseFactory.ok(new RestPage<>(likers, RestApplicationUser.REST_TRANSFORM)).build();
    }

    @Nonnull
    protected Response like(@Nonnull Repository repository, @Nonnull Comment comment) {
        commentLikeService.like(repository, comment);

        return ResponseFactory.noContent().build();
    }

    @Nonnull
    protected Response unlike(@Nonnull Repository repository, @Nonnull Comment comment) {
        commentLikeService.unlike(repository, comment);

        return ResponseFactory.noContent().build();
    }
}
