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

import com.atlassian.bitbucket.hook.BuiltinHookHandlerFactory;
import com.atlassian.bitbucket.hook.HookHandler;
import com.atlassian.bitbucket.hook.HookRequest;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.hook.PluginHookHandlerFactory;

import javax.annotation.Nonnull;

public class GitHookHandlerFactory implements PluginHookHandlerFactory {

    private final BuiltinHookHandlerFactory hookHandlerFactory;

    public GitHookHandlerFactory(BuiltinHookHandlerFactory hookHandlerFactory) {
        this.hookHandlerFactory = hookHandlerFactory;
    }

    @Override
    public HookHandler create(@Nonnull Repository repository, @Nonnull HookRequest request) {
        String hookName = request.getHookName();
        if ("git-pre-receive".equals(hookName)) {
            return hookHandlerFactory.preReceive(repository, GitHookUtils.parseRefChanges(request.getInput()));
        }
        if ("git-post-receive".equals(hookName)) {
            return hookHandlerFactory.postReceive(repository, GitHookUtils.parseRefChanges(request.getInput()));
        }
        return null;
    }
}
