package org.cloudcoder.app.loadtester;

import java.util.Arrays;

import org.cloudcoder.app.shared.model.Change;
import org.cloudcoder.app.shared.model.ChangeType;
import org.cloudcoder.app.shared.model.CompilationOutcome;
import org.cloudcoder.app.shared.model.ICallback;
import org.cloudcoder.app.shared.model.SubmissionResult;

public class LoadTesterTask implements Runnable {
	private HostConfig hostConfig;
	private String userName;
	private String password;
	private EditSequence editSequence;
	private int repeatCount;

	public void setHostConfig(HostConfig hostConfig) {
		this.hostConfig = hostConfig;
	}
	
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public void setEditSequence(EditSequence editSequence) {
		this.editSequence = editSequence;
	}
	
	public void setRepeatCount(int repeatCount) {
		this.repeatCount = repeatCount;
	}
	
	@Override
	public void run() {
		try {
			doRun();
		} catch (Exception e) {
			System.err.println("LoadTesterTask caught exception: " + e.toString());
		}
	}

	private void doRun() throws Exception {
		Client client = new Client(hostConfig);
		
		client.login(userName, password);
		
		PlayEditSequence player = new PlayEditSequence();
		player.setClient(client);
		player.setEditSequence(editSequence);
		player.setSubmitOnFullTextChange(true);
		
		player.setOnSend(new ICallback<Change[]>() {
			@Override
			public void call(Change[] value) {
				if (value.length == 1 && value[0].getType() == ChangeType.FULL_TEXT) {
					System.out.print("\u2191");
				} else {
					char[] a = new char[value.length];
					Arrays.fill(a, '.');
					System.out.print(new String(a));
				}
				System.out.flush();
			}
		});
		player.setOnSubmissionResult(new ICallback<SubmissionResult>() {
			public void call(SubmissionResult value) {
				char c;
				if (value.getCompilationResult().getOutcome() != CompilationOutcome.SUCCESS
						|| value.getNumTestsPassed() < value.getNumTestsAttempted()) {
					c = '\u2639';
				} else {
					c = '\u263A';
				}
				System.out.print(String.valueOf(c));
				System.out.flush();
			}
		});
		
		player.setup();
		
		for (int i = 0; i < repeatCount; i++) {
			player.play();
		}
	}

}

@@ -157,16 +157,90 @@
 geType;%0A
+import org.cloudcoder.app.shared.model.CloudCoderAuthenticationException;%0A
 import o
@@ -1176,20 +1176,54 @@
 ing());%0A
+%09%09%09e.printStackTrace(System.err);%0A
 %09%09%7D%0A
-
 %09%7D%0A%0A%09pri
@@ -1306,16 +1306,21 @@
 );%0A%09%09%0A%09%09
+if (!
 client.l
@@ -1343,17 +1343,101 @@
 assword)
-;
+) %7B%0A%09%09%09throw new RuntimeException(%22Could not log into %22 + userName + %22 account%22);%0A%09%09%7D
 %0A%09%09%0A%09%09Pl

throw exception if login fails