package it.com.atlassian.stash.hosting;

import com.atlassian.bitbucket.rest.util.ResponseFactory;
import com.atlassian.bitbucket.rest.util.RestUtils;
import com.atlassian.plugin.PluginController;
import com.atlassian.plugins.rest.common.security.AnonymousAllowed;
import com.atlassian.sal.api.pluginsettings.PluginSettingsFactory;
import com.atlassian.bitbucket.server.ApplicationPropertiesService;
import com.atlassian.bitbucket.ssh.SshConfiguration;
import com.atlassian.bitbucket.ssh.SimpleSshConfiguration;
import com.atlassian.bitbucket.ssh.SshConfigurationService;
import com.sun.jersey.spi.resource.Singleton;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("helper")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({RestUtils.APPLICATION_JSON_UTF8})
@Singleton
@AnonymousAllowed
public class TestHelperResource {

    private final ApplicationPropertiesService applicationPropertiesService;
    private final PluginController pluginController;
    private final PluginSettingsFactory pluginSettingsFactory;
    private final SshConfigurationService sshConfigurationService;
    private final TestHookMessageProvider testHookMessageProvider;

    public TestHelperResource(ApplicationPropertiesService applicationPropertiesService,
                              PluginController pluginController,
                              PluginSettingsFactory pluginSettingsFactory,
                              SshConfigurationService sshConfigurationService,
                              TestHookMessageProvider testHookMessageProvider) {
        this.applicationPropertiesService = applicationPropertiesService;
        this.pluginController = pluginController;
        this.pluginSettingsFactory = pluginSettingsFactory;
        this.sshConfigurationService = sshConfigurationService;
        this.testHookMessageProvider = testHookMessageProvider;
    }

    @PUT
    @Path("hook/{key}")
    public Response putFailureMessage(@PathParam("key") String key, String message) {
        pluginController.enablePluginModule("com.atlassian.bitbucket.server.bitbucket-it-hosting:" + key);
        testHookMessageProvider.putMessage(key, message);
        return ResponseFactory.noContent().build();
    }

    @DELETE
    @Path("hook/{key}")
    public Response deleteFailureMessage(@PathParam("key") String key) {
        testHookMessageProvider.deleteMessage(key);
        pluginController.disablePluginModule("com.atlassian.bitbucket.server.bitbucket-it-hosting:" + key);
        return ResponseFactory.noContent().build();
    }

    @GET
    @Path("hook/{key}")
    public Response getMessage(@PathParam("key") String key) {
        String message = testHookMessageProvider.getMessage(key);
        return ResponseFactory.ok(message).build();
    }

    @GET
    @Path("setting/{key}")
    @Produces({MediaType.TEXT_PLAIN})
    public Response getPluginSetting(@PathParam("key") String key) {
        return ResponseFactory.ok(pluginSettingsFactory.createGlobalSettings().get(key)).build();
    }

    @POST
    @Path("httpAccess")
    public Response toggleHttpAccess(@QueryParam("enabled") boolean enabled) {
        applicationPropertiesService.setHttpScmHostingEnabled(enabled);
        return ResponseFactory.noContent().build();
    }

    @POST
    @Path("sshAccess")
    public Response toggleSshAccess(@QueryParam("enabled") boolean enabled) {
        SshConfiguration configuration = sshConfigurationService.getConfiguration();
        if (configuration.isEnabled() != enabled) {
            SshConfiguration updated = new SimpleSshConfiguration(
                    enabled,
                    configuration.isAccessKeysEnabled(),
                    configuration.getPort(),
                    configuration.getBaseUrl());
            sshConfigurationService.setConfiguration(updated);
        }
        return ResponseFactory.noContent().build();
    }

}
