package org.jenkinsci.plugins.p4.swarmAPI;

import com.google.gson.Gson;
import hudson.Extension;
import hudson.model.RootAction;
import jenkins.branch.BranchSource;
import jenkins.model.Jenkins;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.p4.client.ConnectionHelper;
import org.jenkinsci.plugins.p4.populate.AutoCleanImpl;
import org.jenkinsci.plugins.p4.scm.SwarmScmSource;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Symbol("swarm_projects")
@Extension
public class SwarmQueryAction implements RootAction {

	@Override
	public String getIconFileName() {
		return null;
	}

	@Override
	public String getDisplayName() {
		return null;
	}

	@Override
	public String getUrlName() {
		return "swarm";
	}

	public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {

		String path = req.getRestOfPath();

		if (path != null && path.startsWith("/project")) {
			String credentialID = req.getParameter("credential");

			try (ConnectionHelper p4 = new ConnectionHelper(credentialID, null)) {
				SwarmHelper swarm = new SwarmHelper(p4, "4");
				List<String> list = swarm.getProjects();

				Gson gson = new Gson();
				String json = gson.toJson(list);

				PrintWriter out = rsp.getWriter();
				out.write(json);
			} catch (Exception e) {
				throw new IOException(e);
			}
		}

		if (path != null && path.startsWith("/create")) {
			String credentialID = req.getParameter("credential");
			String project = req.getParameter("project");
			String name = req.getParameter("name");

			if (name == null || name.isEmpty()) {
				name = project;
			}

			try (ConnectionHelper p4 = new ConnectionHelper(credentialID, null)) {
				SwarmHelper swarm = new SwarmHelper(p4, "4");

				String format = "jenkins-${NODE_NAME}-${JOB_NAME}";
				SwarmScmSource source = new SwarmScmSource(credentialID, null, format);
				source.setProject(project);
				source.setSwarm(swarm);
				source.setPopulate(new AutoCleanImpl());

				WorkflowMultiBranchProject multi = Jenkins.getInstance().createProject(WorkflowMultiBranchProject.class, name);
				multi.getSourcesList().add(new BranchSource(source));

				multi.scheduleBuild2(0);

				PrintWriter out = rsp.getWriter();
				out.write(name);
			} catch (Exception e) {
				throw new IOException(e);
			}
		}
	}
}

@@ -62,24 +62,93 @@
 .gson.Gson;%0A
+import com.google.gson.JsonArray;%0Aimport com.google.gson.JsonObject;%0A
 import hudso
@@ -737,16 +737,151 @@
 .List;%0A%0A
+import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;%0Aimport static javax.servlet.http.HttpServletResponse.SC_CREATED;%0A%0A
 @Symbol(
@@ -2617,63 +2617,756 @@
 %09%09%09%09
-PrintWriter out = rsp.getWriter();%0A%09%09%09%09out.write(name);
+rsp.setStatus(SC_CREATED);%0A%09%09%09%09rsp.setContentType(%22text/plain%22);%0A%0A%09%09%09%09PrintWriter out = rsp.getWriter();%0A%09%09%09%09out.write(name);%0A%0A%09%09%09%7D catch (IllegalArgumentException e) %7B%0A%09%09%09%09rsp.setStatus(SC_BAD_REQUEST);%0A%09%09%09%09rsp.setContentType(%22text/plain%22);%0A%0A%09%09%09%09JsonObject json = new JsonObject();%0A%09%09%09%09json.addProperty(%22message%22, %22Failed to create pipeline%22);%0A%09%09%09%09json.addProperty(%22code%22, SC_BAD_REQUEST);%0A%09%09%09%09JsonArray errors = new JsonArray();%0A%09%09%09%09JsonObject error = new JsonObject();%0A%09%09%09%09error.addProperty(%22message%22, name + %22 already exists%22);%0A%09%09%09%09error.addProperty(%22code%22, %22ALREADY_EXISTS%22);%0A%09%09%09%09error.addProperty(%22field%22, %22name%22);%0A%09%09%09%09errors.add(error);%0A%09%09%09%09json.add(%22errors%22, errors);%0A%0A%09%09%09%09PrintWriter out = rsp.getWriter();%0A%09%09%09%09out.write(json.toString());%0A
 %0A%09%09%09

return error 400 on Create if Pipeline exists.