package net.beadsproject.touch.hardware;

import java.net.ConnectException;
import java.util.Vector;

import PQSDKMultiTouch.PQMTClient;
import PQSDKMultiTouch.PQMTClientConstant;
import PQSDKMultiTouch.TouchClientRequest;
import PQSDKMultiTouch.TouchPoint;

public class PQLabsTouchSurface extends TouchSurface {
	
	Client client;
	boolean started;
		
	public PQLabsTouchSurface()
	{
		client = new Client(this);
		started = false;
	}	
	
	public void start()
	{
		try {
			client.Init();
			started = true;
			setWidth(client.width);
			setHeight(client.height);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
		
	protected void pressed(int id, int x, int y){
		if (listener!=null)
			listener.pressed(id,x,y);
	}
	protected void released(int id)
	{
		if (listener!=null)
			listener.released(id);		
	}
	protected void moved(int id, int x, int y)
	{
		if (listener!=null)
			listener.moved(id,x,y);		
	}
	
	private static class Client extends PQMTClient
	{		
		PQLabsTouchSurface pqlts;
		public int width = -1;
		public int height = -1;
		
		public Client(PQLabsTouchSurface pqlts)
		{
			this.pqlts = pqlts;									
		}
		
		/**
		 * connect to server and setup client request type
		 * */
		public int Init()throws Exception{			
			int err_code = PQMTClientConstant.PQ_MT_SUCESS;
			try{
				if((err_code=ConnectServer())!=PQMTClientConstant.PQ_MT_SUCESS)
				{
					System.err.println("connect server fail");
					return err_code;
				}
			}catch(ConnectException ex){
				System.err.println("server not loaded");
				return err_code;
			}
			
			TouchClientRequest clientRequest = new TouchClientRequest();
			clientRequest.app_id=GetTrialAppID();
		
			try{
			clientRequest.type=PQMTClientConstant.RQST_RAWDATA_ALL|PQMTClientConstant.RQST_GESTURE_ALL;
			if((err_code=SendRequest(clientRequest))!=PQMTClientConstant.PQ_MT_SUCESS)
			{
				System.err.println("send request  fail,  error code:"+err_code);
				return err_code;
			}
			if((err_code=GetServerResolution())!=PQMTClientConstant.PQ_MT_SUCESS)
			{
				System.err.println("get server resolution fail,  error code:"+err_code);
				return err_code;
			}
			System.out.println("connected, start receive:"+err_code);
			}catch(Exception ex){
				System.err.println(ex.getMessage());
			}
			return err_code;
		}
		
		public int OnGetResolution(int x, int y)
		{
			width = x;
			height = y;
			return PQMTClientConstant.PQ_MT_SUCESS;			
		}
				
		public int OnTouchFrame(int frame_id, int time_stamp, Vector<TouchPoint> point_list)
		{
		    for(TouchPoint point: point_list)
			{
		    	int i = point.m_id;
		    	int x = point.m_x;
		    	int y = point.m_y;
		    	
		    	if (point.m_point_event==PQMTClientConstant.TP_DOWN)
		    	{
		    		pqlts.pressed(i,x,y);		    		
		    	}
		    	else if (point.m_point_event==PQMTClientConstant.TP_MOVE)
		    	{
		    		pqlts.moved(i,x,y);
		    	}
		    	else if (point.m_point_event==PQMTClientConstant.TP_UP)
		    	{
		    		pqlts.released(i);
		    	}
			}		    
			return PQMTClientConstant.PQ_MT_SUCESS;
		}
	}
}
