package net.beadsproject.touch.hardware.tests;

import java.awt.Color;
import java.awt.geom.Point2D;
import java.net.ConnectException;
import java.util.Vector;

import processing.core.PApplet;
import processing.core.PGraphics;
import PQSDKMultiTouch.PQMTClient;
import PQSDKMultiTouch.PQMTClientConstant;
import PQSDKMultiTouch.TouchClientRequest;
import PQSDKMultiTouch.TouchPoint;

public class PQDrawingFun extends PApplet {

	public static class Client extends PQMTClient
	{
		PGraphics gfx;
		Point2D[] oldPositions;
		Color[] colors;
		/**
		 * connect to server and setup client request type
		 * */
		public int Init(PGraphics gfx)throws Exception{
			this.gfx = gfx;
			oldPositions = new Point2D[100];
			colors = new Color[100];
			for(int i=0;i<100;i++)
			{
				oldPositions[i] = null;
				colors[i] = new Color(Color.HSBtoRGB((float)Math.random(),0.5f,0.5f));
			}
			
			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 OnTouchFrame(int frame_id, int time_stamp, Vector<TouchPoint> point_list){
			gfx.beginDraw();
		    for(TouchPoint point:point_list )
			{
		    	/*
		    	String type = "unknown";
		    	switch (point.m_point_event)
		    	{
		    		case PQMTClientConstant.TP_DOWN:
		    			type = "down";
		    			break;
		    		case PQMTClientConstant.TP_UP:
		    			type = "up";
		    			break;
		    		case PQMTClientConstant.TP_MOVE:
		    			type = "move";
		    			break;
		    		default: break;		    			
		    	}		    	
				String message = type + " " + point.m_id + " at " + 
					point.m_x + "," + point.m_y + " (size " +
					point.m_dx + "*" + point.m_dy + ")";
				System.out.println(message);
				*/
		    	
		    	if (point.m_point_event==PQMTClientConstant.TP_MOVE)
		    	{
		    		Point2D p = new Point2D.Double(point.m_x, point.m_y);
		    		Point2D o = oldPositions[point.m_id]; 
		    		if (o != null)
		    		{
		    			// draw a line from old to new position
		    			Color c = colors[point.m_id];
		    			gfx.stroke(c.getRed(),c.getGreen(),c.getBlue());
		    			gfx.strokeWeight(Math.min(point.m_dx*.2f,point.m_dy*.2f));
		    			gfx.line((float)o.getX(),(float)o.getY(),(float)p.getX(),(float)p.getY());
		    		}		    		
		    		oldPositions[point.m_id] = p;
		    	}
		    	else if (point.m_point_event==PQMTClientConstant.TP_UP)
		    	{
		    		oldPositions[point.m_id] = null;
		    	}
			}
		    gfx.endDraw();
			return PQMTClientConstant.PQ_MT_SUCESS;
		}
	}
	
	Client client;
	PGraphics gfx;
	
	public void setup()
	{
		size(screen.width,screen.height);
		noCursor();
		gfx = createGraphics(width,height,P2D);
		
		gfx.beginDraw();
		gfx.background(255);
		gfx.noFill();
		gfx.stroke(0);
		gfx.strokeWeight(2);
		gfx.endDraw();	
		
		client = new Client();
		try {
			client.Init(gfx);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void draw()
	{
		image(gfx,0,0);
		
	}
}
