package org.bench4q.recorder.httpcapture;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.swing.JTextArea;

import org.apache.log4j.Logger;
import org.bench4q.share.models.agent.RunScenarioModel;

public class HttpCapture {
	private int localport;
	private Config config;
	private Test currentTest;
	private ProxyServer proxy;
	private File resultFile;
	private ExecutorService executorService;
	private String scriptContent;
	private static Logger logger = Logger.getLogger(HttpCapture.class);

	public int getLocalport() {
		return localport;
	}

	public void setLocalport(int localport) {
		this.localport = localport;
	}

	public Config getConfig() {
		return config;
	}

	public void setConfig(Config config) {
		this.config = config;
	}

	public Test getCurrentTest() {
		return currentTest;
	}

	public void setCurrentTest(Test currentTest) {
		this.currentTest = currentTest;
	}

	public ProxyServer getProxy() {
		return proxy;
	}

	public void setProxy(ProxyServer proxy) {
		this.proxy = proxy;
	}

	public File getResultFile() {
		return resultFile;
	}

	public void setResultFile(File resultFile) {
		this.resultFile = resultFile;
	}

	public String getScriptContent() {
		return scriptContent;
	}

	private void setScriptContent(String scriptContent) {
		this.scriptContent = scriptContent;
	}

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

	public HttpCapture(String dirPath, String fileName, int localport,
			String generator, JTextArea textArea) {
		this.localport = localport;
		System.out.println(dirPath + System.getProperty("file.separator")
				+ fileName + ".xml");

		File file = new File(dirPath);
		if (!file.exists()) {
			file.mkdirs();
		}

		this.resultFile = new File(dirPath
				+ System.getProperty("file.separator") + fileName + ".xml");
		this.config = Config.getConfig();
		try {
			this.config.completeInit();
			this.proxy = new ProxyServer(this.localport);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.currentTest = new Test(this.proxy, new Bench4qTestScriptAdapter(
				new RunScenarioModel()), generator);
	}

	public void startProxyServer() throws IOException, Utils.UserException {
		logger.info("enter startProxyServer!");
		this.executorService = Executors.newCachedThreadPool();
		this.executorService.execute(this.proxy);
	}

	public void startRecording() throws IOException, Utils.UserException {
		this.currentTest.startRecording();
		this.resultFile.createNewFile();
		this.currentTest.setTestFile(this.resultFile);
	}

	public void stopRecording() throws IOException, Utils.UserException {
		this.currentTest.stopRecording();
		this.currentTest.save();
	}

	public void shutProxyServer() throws IOException, Utils.UserException {
		if (this.proxy.getServerSocket() != null) {
			this.proxy.getServerSocket().close();
		}
		this.executorService.shutdownNow();
		this.currentTest.close();
		this.retrieveScriptContent();
	}

	private void retrieveScriptContent() {
		this.setScriptContent(this.currentTest.getScriptContent());
	}
}
