#ifndef _LASER_SENDING_
#define _LASER_SENDING_

#include "ofMain.h"
#include "ofAddons.h"
#include "ofTCPServer.h"
#include "udpManager.h"

class laserSending{
	public:
		
		// ------------------------		
		laserSending(){
			udpSetup 		= false;
			tcpSetup 		= false;
			numConnected 	= 0;
		}
		
		~laserSending(){
			close();
		}
		
		//-------------------------
		void close(){
			if(udpSetup){
				UDP.Close();
				udpSetup = false;
			}
			if(tcpSetup){
				TCP.close();
				tcpSetup = false;
				numConnected = 0;
			}
		}
		
		// ------------------------		
		bool setupUDP(string ip, int port){
			close();
			if(UDP.Create() && UDP.Connect((char *)ip.c_str(), (unsigned short)port) ){
				udpSetup = true;
				return true;
			}
			return false;
		}
		
		// ------------------------		
		bool isSetup(){
			return (udpSetup || tcpSetup);
		}
		
		// ------------------------		
		bool isTCPSetup(){
			return tcpSetup;
		}		
		
		// ------------------------		
		bool isUDPSetup(){
			return udpSetup;
		}		
				
		
		// ------------------------		
		bool setupTCPServer(int port){
			close();
			if(TCP.setup(port)){
				tcpSetup = true;
				return true;
			}
			return false;
		}
			
		// ------------------------		
		bool checkNewTCPClient(){
			if(tcpSetup){
				if(TCP.getNumClients() > numConnected){
					numConnected = TCP.getNumClients();
					return true;
				}
			}
			return false;
		}
		
		// ------------------------		
		string getLatestTCPClientIP(){
			if(tcpSetup){
				if(TCP.getNumClients() > 0){
					return TCP.getClientIP(TCP.getNumClients()-1);
				}
			}
			return "???.???.???.???";
		}
		
		// ------------------------		
		int getLatestTCPClientPort(){
			if(tcpSetup){
				if(TCP.getNumClients() > 0){
					return TCP.getClientPort(TCP.getNumClients()-1);
				}
			}
			return 0;
		}		
						
				
		// ------------------------		
		bool sendData(string dataString){
			if(!udpSetup && !tcpSetup)return false;
			
			if(udpSetup)UDP.Send(dataString.c_str(), dataString.length());
			if(tcpSetup && TCP.getNumClients()){
				TCP.sendToAll(dataString.c_str());
			}
			return true;
		}		
		
		// ------------------------		
		void setupTCP(){
			
		}		
		
	protected:
		bool udpSetup;
		bool tcpSetup;
		int  numConnected;
	
		ofTCPServer TCP;
		UDPManager  UDP;
};

#endif