package com.atlassian.stash.internal.scm.git.command.foreachref;

import com.atlassian.bitbucket.repository.Branch;
import com.atlassian.bitbucket.repository.BranchCallback;
import com.atlassian.bitbucket.repository.BranchContext;
import com.atlassian.bitbucket.repository.BranchSummary;
import com.atlassian.bitbucket.scm.CommandSummary;

import javax.annotation.Nonnull;
import java.io.IOException;

/**
 * @since 4.6
 */
public class ForEachBranchCallback extends AbstractForEachRefCallback<Branch> {

    private static final BranchContext CONTEXT = new BranchContext.Builder().build();

    private final BranchCallback delegate;

    public ForEachBranchCallback(BranchCallback delegate, String filterText, String head) {
        super(new ForEachBranchParser(head), filterText);

        this.delegate = delegate;
    }

    @Override
    protected void onEnd(@Nonnull CommandSummary summary) throws IOException {
        delegate.onEnd(new BranchSummary.Builder(summary).build());
    }

    @Override
    protected boolean onRef(@Nonnull Branch ref) throws IOException {
        return delegate.onBranch(ref);
    }

    @Override
    protected void onStart() throws IOException {
        delegate.onStart(CONTEXT);
    }
}
