package org.jenkinsci.plugins.p4.scm;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Item;
import hudson.model.Run;
import hudson.scm.SCM;
import jenkins.scm.api.SCMFile;
import jenkins.scm.api.SCMFileSystem;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSource;
import org.jenkinsci.plugins.p4.PerforceScm;
import org.jenkinsci.plugins.p4.client.TempClientHelper;
import org.jenkinsci.plugins.p4.workspace.Workspace;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;

import java.io.IOException;

public class P4SCMFileSystem extends SCMFileSystem {

	private TempClientHelper p4;

	protected P4SCMFileSystem(@NonNull Item owner, @NonNull PerforceScm scm, @CheckForNull P4SCMRevision rev) throws Exception {
		super(rev);
		String credential = scm.getCredential();
		Workspace ws = scm.getWorkspace().deepClone();

		// TODO Set environment (on SCM or Workspace)
		if (owner instanceof WorkflowJob) {
			WorkflowJob _job = (WorkflowJob) owner;
			Run<?,?> build = _job.getLastBuild();
			EnvVars env = build.getEnvironment(null);
			ws.setExpand(env);
		}

		this.p4 = new TempClientHelper(owner, credential, null, ws);
	}

	@Override
	public void close() throws IOException {
		p4.close();
	}

	@Override
	public long lastModified() throws IOException, InterruptedException {
		return 0;
	}

	@NonNull
	@Override
	public SCMFile getRoot() {
		return new P4SCMFile(this);
	}

	@Extension
	public static class BuilderImpl extends SCMFileSystem.Builder {

		@Override
		public boolean supports(SCM source) {
			if (source instanceof PerforceScm) {
				return true;
			}
			return false;
		}

		@Override
		public boolean supports(SCMSource source) {
			if (source instanceof AbstractP4ScmSource) {
				return true;
			}
			return false;
		}

		@Override
		public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull SCMRevision rev) throws IOException, InterruptedException {
			if (scm == null || !(scm instanceof PerforceScm)) {
				return null;
			}
			PerforceScm p4scm = (PerforceScm) scm;

			if (rev != null && !(rev instanceof P4SCMRevision)) {
				return null;
			}
			P4SCMRevision p4rev = (P4SCMRevision) rev;

			try {
				return new P4SCMFileSystem(owner, p4scm, p4rev);
			} catch (Exception e) {
				throw new IOException(e);
			}
		}
	}

	public TempClientHelper getConnection() {
		return p4;
	}
}

@@ -255,16 +255,52 @@
 cm.SCM;%0A
+import hudson.util.LogTaskListener;%0A
 import j
@@ -674,61 +674,210 @@
 on;%0A
-%0Apublic class P4SCMFileSystem extends SCMFileSystem %7B
+import java.util.logging.Level;%0Aimport java.util.logging.Logger;%0A%0Apublic class P4SCMFileSystem extends SCMFileSystem %7B%0A%0A%09private static Logger logger = Logger.getLogger(P4SCMFileSystem.class.getName());
 %0A%0A%09p
@@ -1146,13 +1146,8 @@
 %09%09//
- TODO
  Set
@@ -1163,18 +1163,10 @@
 ent 
-(on SCM or
+in
  Wor
@@ -1171,17 +1171,16 @@
 orkspace
-)
 %0A%09%09if (o
@@ -1333,19 +1333,54 @@
 onment(n
-ull
+ew LogTaskListener(logger, Level.INFO)
 );%0A%09%09%09ws
@@ -1641,18 +1641,8 @@
 %09%7D%0A%0A
-%09@NonNull%0A
 %09@Ov
@@ -2135,25 +2135,16 @@
 m owner,
- @NonNull
  SCM scm

Fix findbug errors, and add logger.