package us.kbase.jgiintegration.perftest;

import static us.kbase.jgiintegration.common.JGIUtils.wipeRemoteServer;

import java.io.File;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import us.kbase.jgiintegration.common.JGIFileLocation;
import us.kbase.jgiintegration.common.JGIOrganismPage;

import com.gargoylesoftware.htmlunit.WebClient;

public class MassPushFiles {
	
	private static final boolean SKIP_WIPE = true;
	private static final String JGI_PUSHABLE_FILES = 
			"/home/crusherofheads/localgit/jgi_kbase_integration_tests/test_data/putative_pushable_files";
	
	private static final String WIPE_URL = 
			"http://dev03.berkeley.kbase.us:9000";
	
	private static final int WORKERS = 20;

	private static String JGI_USER;
	private static String JGI_PWD;
	private static String KB_USER;
	private static String KB_PWD;
	
	
	public static void main(String[] args) throws Exception {
		Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
		JGI_USER = System.getProperty("test.jgi.user");
		JGI_PWD = System.getProperty("test.jgi.pwd");
		KB_USER = System.getProperty("test.kbase.user1");
		KB_PWD = System.getProperty("test.kbase.pwd1");
		
		String wipeUser = System.getProperty("test.kbase.wipe_user");
		String wipePwd = System.getProperty("test.kbase.wipe_pwd");
		if (!SKIP_WIPE) {
			wipeRemoteServer(new URL(WIPE_URL), wipeUser, wipePwd);
		}
		List<String> lines = Files.readAllLines(
				new File(JGI_PUSHABLE_FILES).toPath(),
					Charset.forName("UTF-8"));
		
		List<List<PushableFile>> filesets =
				new LinkedList<List<PushableFile>>();
		for (int i = 0; i < WORKERS; i++) {
			filesets.add(new LinkedList<PushableFile>());
		}
		int index = 0;
		for (String line: lines) {
			String[] temp = line.split("\t");
			PushableFile pf = new PushableFile(
					temp[0], temp[1], temp[2], temp[3]);
			if (index >= filesets.size()) {
				index = 0;
			}
			filesets.get(index).add(pf);
			index++;
		}

		List<PushFilesToKBaseRunner> theruns =
				new LinkedList<PushFilesToKBaseRunner>();
		for (List<PushableFile> list: filesets) {
			theruns.add(new PushFilesToKBaseRunner(list));
		}
		List<Thread> threads = new LinkedList<Thread>();
		for (PushFilesToKBaseRunner r: theruns) {
			Thread t = new Thread(r);
			t.start();
			threads.add(t);
		}
		
		for (Thread t: threads) {
			t.join();
		}
		
		index = 1;
		for (PushFilesToKBaseRunner r: theruns) {
			System.out.println(String.format("Worker %s exceptions:", index));
			for (Throwable t: r.getExceptions()) {
				t.printStackTrace();
			}
			index++;
		}
		
	}
	
	private static class PushFilesToKBaseRunner implements Runnable {
		
		private final List<PushableFile> files;
		private final List<Throwable> exceptions = new LinkedList<Throwable>();

		public PushFilesToKBaseRunner(List<PushableFile> files) {
			this.files = files;
		}
		
		@Override
		public void run() {
			WebClient wc = new WebClient();
			try {
				//perform known good login
				new JGIOrganismPage(
						wc, "BlaspURHD0036", JGI_USER, JGI_PWD);
			} catch (Exception e) {
				exceptions.add(e);
			}
			for (PushableFile f: files) {
				try {
					JGIOrganismPage p = new JGIOrganismPage(
							wc, f.getOrganism(), null, null);
					p.selectFile(new JGIFileLocation(
							f.getFileGroup(), f.getFile()));
					p.pushToKBase(KB_USER, KB_PWD);
				} catch (Exception e) {
					exceptions.add(e);
				}
			}
		}
		
		public List<Throwable> getExceptions() {
			return exceptions;
		}
		
	}
}

@@ -1941,17 +1941,17 @@
 emp%5B
-0
+1
 %5D, temp%5B
 1%5D, 
@@ -1942,25 +1942,25 @@
 mp%5B1%5D, temp%5B
-1
+0
 %5D, temp%5B2%5D, 

Correct input for PushableFile class