package com.atlassian.bitbucket.mesh.git.gc;

import com.atlassian.bitbucket.mesh.config.GcConfig;
import com.atlassian.bitbucket.mesh.git.GitRepositoryLayout;
import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilderFactory;
import com.atlassian.bitbucket.mesh.repository.Repository;

import javax.annotation.Nonnull;

class ManagedGc implements GcOperation {

    private final GitProcessBuilderFactory builderFactory;
    private final GcConfig config;
    private final PackRefsOperation packRefsOperation;
    private final Repository repository;
    private final GitRepositoryLayout repositoryLayout;

    ManagedGc(GitProcessBuilderFactory builderFactory, GcConfig config,
              Repository repository, GitRepositoryLayout repositoryLayout) {
        this.builderFactory = builderFactory;
        this.config = config;
        this.repository = repository;
        this.repositoryLayout = repositoryLayout;

        packRefsOperation = new PackRefsOperation(builderFactory, config, repository, repositoryLayout);
    }

    @Override
    public void run(@Nonnull GcContext context) {
        packRefsOperation.run(context);

        if (context.isSuccess(GcActionType.PACK_REFS)) {
            new WriteCommitGraphOperation(builderFactory, config, repository, repositoryLayout).run(context);
            new PruneStaleObjectsOperation(repository, config, repositoryLayout).run(context);
        }
    }

    @Override
    public boolean shouldRun() {
        return packRefsOperation.shouldRun();
    }
}
