package it.com.atlassian.stash.hosting;

import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.bitbucket.hook.repository.AsyncPostReceiveRepositoryHook;
import com.atlassian.bitbucket.hook.repository.RepositoryHookContext;
import com.atlassian.bitbucket.repository.RefChange;

import javax.annotation.Nonnull;
import java.util.Collection;

public class TestAsyncPostReceiveRepositoryHook implements AsyncPostReceiveRepositoryHook {

    private final TestHookMessageProvider testHookMessageProvider;
    private final PluginSettingsFactory pluginSettingsFactory;

    public TestAsyncPostReceiveRepositoryHook(TestHookMessageProvider testHookMessageProvider, PluginSettingsFactory pluginSettingsFactory) {
        this.testHookMessageProvider = testHookMessageProvider;
        this.pluginSettingsFactory = pluginSettingsFactory;
    }

    @Override
    public void postReceive(@Nonnull RepositoryHookContext context, @Nonnull Collection<RefChange> refChanges) {
        testHookMessageProvider.putMessage(HostingTestConstants.POST_RECEIVE_CONFIGURED_HOOK_KEY, HostingTestConstants.POST_RECEIVE_CONFIGURED_HOOK_KEY);
        // Make sure we are testing write transactions!!!
        pluginSettingsFactory.createGlobalSettings()
                .put(HostingTestConstants.POST_RECEIVE_CONFIGURED_HOOK_KEY, refChanges.iterator().next().getRef().getId());
    }
}
