package org.jenkinsci.plugins.p4.workspace;

import com.perforce.p4java.client.IClientSummary;
import com.perforce.p4java.client.IClientSummary.ClientLineEnd;
import com.perforce.p4java.core.IStreamSummary;
import com.perforce.p4java.option.server.GetStreamsOptions;
import com.perforce.p4java.server.IOptionsServer;
import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.AutoCompletionCandidates;
import hudson.model.Descriptor;
import hudson.util.ListBoxModel;
import org.jenkinsci.plugins.p4.client.ConnectionFactory;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class WorkspaceSpec extends AbstractDescribableImpl<WorkspaceSpec> implements Serializable {

	private static final long serialVersionUID = 1L;

	public final boolean allwrite;
	public final boolean clobber;
	public final boolean compress;
	public final boolean locked;
	public final boolean modtime;
	public final boolean rmdir;

	private final String streamName;
	private final String line;
	private final String view;
	private final String changeView;
	private final String type;

	private final String serverID;
	private final boolean backup;

	public String getStreamName() {
		return streamName;
	}

	public String getLine() {
		return line;
	}

	public String getView() {
		return view;
	}

	public String getChangeView() {
		return changeView;
	}

	public String getType() {
		return type;
	}

	public String getServerID() {
		return serverID;
	}

	public boolean isBackup() {
		return backup;
	}

	@DataBoundConstructor
	public WorkspaceSpec(boolean allwrite, boolean clobber, boolean compress,
	                     boolean locked, boolean modtime, boolean rmdir, String streamName,
	                     String line, String view, String changeView, String type,
	                     String serverID, boolean backup) {
		this.allwrite = allwrite;
		this.clobber = clobber;
		this.compress = compress;
		this.locked = locked;
		this.modtime = modtime;
		this.rmdir = rmdir;
		this.streamName = streamName;
		this.line = line;
		this.view = view;
		this.changeView = changeView;
		this.type = type;
		this.serverID = serverID;
		this.backup = backup;
	}

	@Deprecated
	public WorkspaceSpec(boolean allwrite, boolean clobber, boolean compress,
	                     boolean locked, boolean modtime, boolean rmdir, String streamName,
	                     String line, String view) {
		this(allwrite, clobber, compress, locked, modtime, rmdir, streamName, line, view, null, null, null, true);
	}

	@Extension
	public static class DescriptorImpl extends Descriptor<WorkspaceSpec> {

		@Override
		public String getDisplayName() {
			return "Perforce Client Spec";
		}

		public ListBoxModel doFillLineItems() {
			ListBoxModel list = new ListBoxModel();
			for (ClientLineEnd end : IClientSummary.ClientLineEnd.values()) {
				list.add(end.name());
			}
			return list;
		}

		public ListBoxModel doFillTypeItems() {
			ListBoxModel list = new ListBoxModel();
			for (WorkspaceSpecType type : WorkspaceSpecType.values()) {
				list.add(type.name());
			}
			return list;
		}

		/**
		 * Provides auto-completion for workspace names. Stapler finds this
		 * method via the naming convention.
		 *
		 * @param value The text that the user entered.
		 * @return suggestion
		 */
		public AutoCompletionCandidates doAutoCompleteStreamName(
				@QueryParameter String value) {

			AutoCompletionCandidates c = new AutoCompletionCandidates();
			try {
				IOptionsServer iserver = ConnectionFactory.getConnection();
				if (iserver != null && value.length() > 1) {
					List<String> streamPaths = new ArrayList<String>();
					streamPaths.add(value + "...");
					GetStreamsOptions opts = new GetStreamsOptions();
					opts.setMaxResults(10);
					List<IStreamSummary> list = iserver.getStreams(streamPaths,
							opts);
					for (IStreamSummary l : list) {
						c.add(l.getStream());
					}
				}
			} catch (Exception e) {
			}

			return c;
		}
	}
}

@@ -156,166 +156,8 @@
 nd;%0A
-import com.perforce.p4java.core.IStreamSummary;%0Aimport com.perforce.p4java.option.server.GetStreamsOptions;%0Aimport com.perforce.p4java.server.IOptionsServer;%0A
 impo
@@ -177,16 +177,16 @@
 ension;%0A
+
 import h
@@ -337,66 +337,8 @@
 el;%0A
-import org.jenkinsci.plugins.p4.client.ConnectionFactory;%0A
 impo
@@ -426,16 +426,16 @@
 meter;%0A%0A
+
 import j
@@ -458,59 +458,8 @@
 ble;
-%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0Apu
@@ -3249,570 +3249,61 @@
 %0A%09%09%09
-AutoCompletionCandidates c = new AutoCompletionCandidates();%0A%09%09%09try %7B%0A%09%09%09%09IOptionsServer iserver = ConnectionFactory.getConnection();%0A%09%09%09%09if (iserver != null && value.length() %3E 1) %7B%0A%09%09%09%09%09List%3CString%3E streamPaths = new ArrayList%3CString%3E();%0A%09%09%09%09%09streamPaths.add(value + %22...%22);%0A%09%09%09%09%09GetStreamsOptions opts = new GetStreamsOptions();%0A%09%09%09%09%09opts.setMaxResults(10);%0A%09%09%09%09%09List%3CIStreamSummary%3E list = iserver.getStreams(streamPaths,%0A%09%09%09%09%09%09%09opts);%0A%09%09%09%09%09for (IStreamSummary l : list) %7B%0A%09%09%09%09%09%09c.add(l.getStream());%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%7D%0A%0A%09%09%09return c
+return StreamDescImpl.doAutoCompleteStreamName(value)
 ;%0A%09%09

Remove duplicate code for Stream name auto completion.