import Six.Sixp.Client.*;

import java.util.concurrent.*;
import java.util.List;
import java.util.Iterator;
import java.util.Vector;

public class SIXPClient implements IClientLogging, Runnable {

	private String[] args;
	private ConcurrentLinkedQueue<ICommand> queue;
	private static String OS = System.getProperty("os.name").toLowerCase();
	private StreamingClientDemo sd;
	
	public SIXPClient(String[] argument, ConcurrentLinkedQueue<ICommand> q)
	{
		args = argument;
		queue = q;
	}

	public void run()
	{
		if (args.length != 4) {
			System.out.println("Syntax: Six.Sixp.Client.Demo server:port userid pwd requestfile");
			return;
		}

		String server = args[0];
		String userid = args[1];
		String pwd = args[2];
		String requestfile = args[3];

		String workdir = System.getProperty("user.dir");

		String host = server.split(":")[0];
		int port = Integer.parseInt(server.split(":")[1]);
		
		List<String> prls;
		ParseUtil p = new ParseUtil();

		if (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 ) {
			prls = p.parseFile(workdir + "/" + requestfile);
		} else {
			prls = p.parseFile(workdir + "\\" + requestfile);
		}

		Iterator<String> iterator = prls.iterator();

		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
		
		Vector<SixpServerSettings> l = new Vector<SixpServerSettings>();
		try
		{
			SixpServerSettings ss = new SixpServerSettings(host, port);
			
			l.add(ss);
		
			sd = new StreamingClientDemo(l, null, ReconnectStrategy.ReconnectResubscribe, 15,prls, workdir, userid, pwd, queue);
		
			sd.RunAsync();
		}
		catch (SixpExceptionInvalidParameter e)
		{
			e.printStackTrace();
			System.out.println("Invalid parameter!");
		}
	}

	public void Stop()
	{
		sd.Stop();
	}
	
	@Override
	public void OnLogMessage(LogLevel level, String message) {
		System.out.println("Level: " + level.toString() + " Message: " + message);
	}

	@Override
	public void OnLogMessage(LogLevel level, String message, Exception exception) {
		System.out.println("Level: " + level.toString() + " Message: " + message + " Exception: " + exception.getMessage());
	}

	@Override
	public boolean getIsDebugLoggingEnabled() {
		return false;
	}
		
}
