import sixapi.*;

import java.util.List;
import java.util.Iterator;
import java.util.Vector;
import java.util.ArrayList;
import java.util.Scanner;

public class Program {
	
	private static String OS = System.getProperty("os.name").toLowerCase();
	
	public static void main(String[] args) {
		run(args);
	}

	public static void run(String[] args) {
		
		DBWrapper dbWrapper;
		dbWrapper = new DBWrapper();
	
		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();
		
		try {
            SixLog logHandler = new SixLogStd(SixLog.INFO);

            System.out.println("Starting test program");
            System.out.println("Host:"+host+":"+port+" User:"+userid+" Password:"+pwd+" Request file:" + requestfile);

            SixEventHandler eventHandler = new SixEventHandler(host,port,logHandler,512,true);
			CookieHandler cookieHandler = new CookieHandler();
            SixResponse response = new SixResponse(eventHandler, cookieHandler, dbWrapper);
		
            System.out.println("Starting program");

            eventHandler.addSixResponseListener(response);
            eventHandler.start();
            SixRequest request = new SixLoginReq(userid,pwd,"apoon");
            eventHandler.send(request);

            for(int i=0;i<5&&!response.getIsLoggedIn();i++){ // wait for login Request
                Thread.sleep(1000);
            }
            if(!response.getIsLoggedIn()){
                System.out.println("Trouble Logging in ! ");
                System.exit(-1);
            }

            // Starting subscriptions
			
			List<SixId> sixids = new ArrayList<SixId>();
			
			while (iterator.hasNext()) {
				String s = iterator.next();
				sixids.add(new SixId(s));
			}

            // Requesting FieldSet and setting up subscriptions
            for (int i = 0; i < sixids.size(); i++) {
                SixId sixid = sixids.get(i);
				String cookie = cookieHandler.getNextCookie();
                SixGetFieldSetReq fieldsetReq = new SixGetFieldSetReq(sixid);
                fieldsetReq.setProperty(SixCmd.COOKIE_PROP,cookie);
                eventHandler.send(fieldsetReq);

                SixSubscribeReq subReq = new SixSubscribeReq(sixid,cookieHandler.currentCookieInteger()+1000);// Using a very simple Subscription ID
                subReq.setProperty(SixCmd.COOKIE_PROP,cookie);
				dbWrapper.CreateTable(cookieHandler.addBrackets(cookie), sixid.toString());
                eventHandler.send(subReq);
            }

            // Running program for a short while
            //Thread.sleep(1*1200*1000);// Wating one minute
			
			System.out.println("Press any key to stop client!");
			Scanner scanner = new java.util.Scanner(System.in);
			String s = scanner.nextLine();
			//int i = Integer.parseInt(s);

            // Unsuscribe all open subscriptions

            for (int i = 0; i < sixids.size(); i++) {
                SixId sixid = sixids.get(i);
                System.out.println("Sending unsubscribe for "+sixid);
                SixUnsubscribeReq unsubReq = new  SixUnsubscribeReq(i+1001);
                eventHandler.send(unsubReq);
            }
            // Logging out
            SixLogoutReq logoutReq = new SixLogoutReq();
            eventHandler.send(logoutReq);

            eventHandler.interrupt();
            eventHandler.dispose();
            System.out.println("Exit program");
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
}
