package com.atlassian.stash.internal.scm;

import com.atlassian.stash.internal.AbstractService;
import com.atlassian.stash.internal.annotation.Throttled;
import com.atlassian.stash.internal.page.PageConstants;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.ScmCommandFactory;
import com.atlassian.bitbucket.scm.ScmService;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Nonnull;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * A simple base class for building service implementations which rely on the {@link ScmCommandFactory} to execute
 * different commands in order to perform their work.
 */
@Throttled(Throttled.SCM_COMMAND)
public abstract class AbstractScmService extends AbstractService {

    @Value(PageConstants.COMMIT_DIFF_CONTEXT)
    protected int diffContext;
    @Value(PageConstants.MAX_CHANGES)
    protected int maxChanges;
    @Value(PageConstants.MAX_COMMITS)
    protected int maxCommits;
    @Value(PageConstants.MAX_DIFF_LINES)
    protected int maxDiffLines;
    @Value(PageConstants.MAX_DIRECTORY_CHILDREN)
    protected int maxDirectoryChildren;
    @Value(PageConstants.MAX_DIRECTORY_RECURSIVE_CHILDREN)
    protected int maxDirectoryRecursiveChildren;
    @Value(PageConstants.MAX_SOURCE_LINE_LENGTH)
    protected int maxLineLength;
    @Value(PageConstants.MAX_SOURCE_LINES)
    protected int maxSourceLines;

    protected final ScmService scmService;

    protected AbstractScmService(ScmService scmService) {
        this.scmService = checkNotNull(scmService, "scmService");
    }

    protected ScmCommandFactory getCommandFactory(@Nonnull Repository repository) {
        return scmService.getCommandFactory(checkNotNull(repository, "repository"));
    }
}
