package org.bench4q.recorder.httpcapture;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.bench4q.recorder.httpcapture.generator.GeneratorFactory;
import org.bench4q.recorder.httpcapture.generator.IScriptGenerator;

public class Test {
	private IScriptGenerator generator;
	private IScriptAdapter scriptAdapter;
	private boolean isRecording;
	private File testFile;
	ProxyServer proxyServer;

	public Test(ProxyServer proxy, IScriptAdapter adapter, String generatorClass) {
		this.proxyServer = proxy;
		adapter.setText("");
		this.generator = GeneratorFactory.newGenerator(generatorClass, adapter);
		this.generator.doNew();
		this.scriptAdapter = adapter;
	}

	public Test(ProxyServer proxy, IScriptAdapter adapter, File file)
			throws IOException, Utils.UserException {
		this.proxyServer = proxy;
		this.testFile = file;

		StringBuffer buffer = new StringBuffer();
		BufferedReader r = new BufferedReader(new FileReader(this.testFile));
		String line = r.readLine();
		while (line != null) {
			buffer.append(line);
			buffer.append(IScriptGenerator.EOL);
			line = r.readLine();
		}
		r.close();
		String script = buffer.toString();

		String generatorClass = null;
		String[] classes = GeneratorFactory.getClasses();

		for (int i = 0; i < classes.length; i++) {
			if (script.indexOf(classes[i]) != -1) {
				generatorClass = classes[i];
				break;
			}
		}
		if (generatorClass == null) {
			throw new Utils.UserException(
					"Your file does not appear to have been generated by MaxQ.\n\n(MaxQ expects to find the class name of the generator somewhere in the script.\nPerhaps you have mistakenly deleted it?)");
		}

		adapter.setText(script);
		this.generator = GeneratorFactory.newGenerator(generatorClass, adapter);
		this.generator.doLoad();
		this.generator.parseTestName();
	}

	public File getTestFile() {
		return this.testFile;
	}

	public void setTestFile(File file) {
		this.testFile = file;
	}

	public boolean isRecording() {
		return this.isRecording;
	}

	public void stopRecording() {
		this.proxyServer.removeObserver(this.generator);
		assert (this.isRecording);
		this.generator.doStopRecording();
		this.isRecording = false;
	}

	public void startRecording() {
		assert (!this.isRecording);
		this.proxyServer.addObserver(this.generator);
		this.isRecording = true;
		this.generator.doStartRecording();
	}

	public boolean save() throws IOException {
		return this.generator.doSave(
				stripFileName(this.testFile.getAbsolutePath()),
				this.testFile.getName());
	}

	private String stripFileName(String absolutePath) {
		int index = absolutePath.lastIndexOf(System
				.getProperty("file.separator"));
		if (index == -1) {
			return absolutePath;
		}
		return absolutePath.substring(0, index);
	}

	public void close() {
		if (this.isRecording)
			this.proxyServer.removeObserver(this.generator);
		this.generator.close();
	}

	public String getScriptContent() {
		return this.scriptAdapter.getText();
	}
}
