package org.bench4q.recorder;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JTextArea;

import org.bench4q.recorder.httpcapture.HttpCapture;
import org.bench4q.recorder.httpcapture.Utils.UserException;
import org.springframework.stereotype.Component;

@Component
public class ScriptCapturer {
	private String ipHttpCaptureServerAdress;
	private static final String HTTPCATUREGENERATOR_STRING = "org.bench4q.recorder.httpcapture.generator.Bench4qCodeGenerator";
	private Map<Integer, HttpCapture> httpCaptureMap;

	public String getIpHttpCaptureServerAdress() {
		return ipHttpCaptureServerAdress;
	}

	private void setIpHttpCaptureServerAdress(String ipHttpCaptureServerAdress) {
		this.ipHttpCaptureServerAdress = ipHttpCaptureServerAdress;
	}

	private Map<Integer, HttpCapture> getHttpCaptureMap() {
		return httpCaptureMap;
	}

	private void setHttpCaptureMap(Map<Integer, HttpCapture> httpCaptureMap) {
		this.httpCaptureMap = httpCaptureMap;
	}

	public ScriptCapturer() {
		this.setIpHttpCaptureServerAdress(this.getLocalHostIp());
		this.setHttpCaptureMap(new HashMap<Integer, HttpCapture>());
	}

	private String getLocalHostIp() {
		InetAddress addr;
		try {
			addr = InetAddress.getLocalHost();
			return addr.getHostAddress().toString();
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return null;
		}

	}

	public void startRecord(int port, String fileDir, String fileName) {
		try {
			HttpCapture httpCapture = buildACapture(port, fileDir, fileName);
			httpCapture.startProxyServer();
			httpCapture.startRecording();
		} catch (IOException e1) {
			System.out.println("Error When start recording!");
			e1.printStackTrace();
		} catch (UserException e1) {
			System.out.println("Error When start recording!");
			e1.printStackTrace();
		}
	}
	public void startRecordServer(int port, String fileDir, String fileName){
		try {
			HttpCapture httpCapture = buildACapture(port, fileDir, fileName);
			httpCapture.startProxyServer();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void startRecording(int port){
		try {
			HttpCapture httpCapture = this.getHttpCaptureMap().get(new Integer(port));
			httpCapture.startRecording();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private HttpCapture buildACapture(int port, String filePath, String fileName) {
		HttpCapture httpCapture = new HttpCapture(filePath, fileName, port,
				HTTPCATUREGENERATOR_STRING, new JTextArea());
		this.getHttpCaptureMap().put(new Integer(port), httpCapture);
		return httpCapture;
	}

	public void stopRecording(int port){
		try {
			HttpCapture httpCapture = this.getHttpCaptureMap().get(
					new Integer(port));
			if (httpCapture == null) {
				return;
			}
			assert (httpCapture.isRecording());
			httpCapture.stopRecording();
		} catch (Exception e1) {
			System.out.println("Error When stop recording!");
			e1.printStackTrace();
		}
	}
	public void stopProxyServer(int port) {
		try {
			HttpCapture httpCapture = this.getHttpCaptureMap().get(
					new Integer(port));
			if (httpCapture == null) {
				return;
			}
			httpCapture.shutProxyServer();
			this.getHttpCaptureMap().remove(new Integer(port));
		} catch (Exception e1) {
			System.out.println("Error When stop recording!");
			e1.printStackTrace();
		}
	}
//	public void stopCurrentRecord(int port) {
//		try {
//			HttpCapture httpCapture = this.getHttpCaptureMap().get(
//					new Integer(port));
//			if (httpCapture == null) {
//				return;
//			}
//			assert (httpCapture.isRecording());
//			httpCapture.stopRecording();
//			httpCapture.shutProxyServer();
//			this.getHttpCaptureMap().remove(new Integer(port));
//		} catch (Exception e1) {
//			System.out.println("Error When stop recording!");
//			e1.printStackTrace();
//		}
//	}
}
