package com.aparicio.tcp;

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;



public class SocketServer
{
	public static String spotyfolder = System.getenv("AppData")+ "\\Spotify\\spotify.exe";
	public static Robot robot;
	public static Runtime runtime = Runtime.getRuntime();
	public static Process proc;
	 private static ServerSocket serverSocket;
	 private static Socket clientSocket;
	 private static InputStreamReader inputStreamReader;
	 private static BufferedReader bufferedReader;
	 private static String message; 
	 public static int port = 0;
	 public static String strIP;
    	
	public static void main(String args[]) {
	    try {
	    	
	    	
	    	 try {
	    		 serverSocket = new ServerSocket(0); //Server socket
	    		 port = serverSocket.getLocalPort();
	    	 		} catch (IOException e) {
	    		 System.out.println("Could not listen on port: " + port);
	    		 } 
	    	 
	      robot = new Robot();
	      InetAddress IP=InetAddress.getLocalHost();
	      
	      	      
	     		
	      
	      // Create a socket to listen on the port.
	      //DatagramSocket dsocket = new DatagramSocket(port);

	      // Create a buffer to read datagrams into. If a
	      // packet is larger than this buffer, the
	      // excess will simply be discarded!
	      
	      // Create a packet to receive data into the buffer
	      //DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

	      strIP = IP.toString();
	      
	      if (strIP.contains("/")) {
	    	  String[] parts = IP.toString().split("/");
	    	  String part1 = parts[0]; 
	    	  strIP = parts[1];
	    	}
	      
	      // Now loop forever, waiting to receive packets and printing them.
	      System.out.println("IP: " + strIP);
	      System.out.println("PORT: " + port);
	      //System.out.println("SPOTY: " + spotyfolder);
	      
	      
	      
	      Thread thread = new Thread(){
	    	    public void run(){
	    	      JOptionPane.showMessageDialog(null, "IP:\n" + strIP + "\n\nPORT:\n" + port);
	    	    }
	    	  };

	      thread.start();	  
	      
	      	      	     
	      System.out.println("Waiting for messages:\n");
	      while (true) {
	    	  
	    	  clientSocket = serverSocket.accept(); //accept the client connection
	    	  inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
	    	  bufferedReader = new BufferedReader(inputStreamReader); //get the client message
	    	  try{
	    	  message = bufferedReader.readLine();
	    	  processCommand(message); 
	    	  System.out.println(clientSocket.getRemoteSocketAddress() + " " + message);
		      } catch (Exception e) {
		    	  System.out.println("timeout");			      
			  }
	    	  inputStreamReader.close();
	    	  clientSocket.close();   
//	        dsocket.receive(packet);
//
//	        String msg = new String(buffer, 0, packet.getLength());
//	        System.out.println(packet.getAddress().getHostName() + " " + packet.getPort() + ": "
//	            + msg);
//	        
//	        processCommand(msg);
//	        
//	        packet.setLength(buffer.length);
	      }
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	  }

	
	private static void processCommand(String command) {
		try {			
    		int i;
    		
    		String[] parts = command.split(",");
    		
    		for(i= 0; i < parts.length; i++)
    		{
    			pressKey(parts[i]);
    		}
    		
    		for(i= parts.length-1; i >= 0; i--)
    		{
    			releaseKey(parts[i]);
    		}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void pressKey(String key)
	{
		if(key.equals("CTRL"))
		{
			robot.keyPress(KeyEvent.VK_CONTROL);
		}
		else if(key.equals("ALT"))
		{
			robot.keyPress(KeyEvent.VK_ALT);
		}
		else if(key.equals("SHIFT"))
		{
			robot.keyPress(KeyEvent.VK_SHIFT);
		}
		else if(key.equals("TAB"))
		{
			robot.keyPress(KeyEvent.VK_TAB);
		}
		else if(key.equals("ENTER"))
		{
			robot.keyPress(KeyEvent.VK_ENTER);
		}
		else if(key.equals("ESC"))
		{
			robot.keyPress(KeyEvent.VK_ESCAPE);
		}
		else if(key.equals("SUPR") || key.equals("DEL"))
		{
			robot.keyPress(KeyEvent.VK_DELETE);
		}
		else if(key.equals("BACK SPACE"))
		{
			robot.keyPress(KeyEvent.VK_BACK_SPACE);
		}
		else if(key.equals("SPACE"))
		{
			robot.keyPress(KeyEvent.VK_SPACE);
		}
		else if(key.equals("UP"))
		{
			robot.keyPress(KeyEvent.VK_UP);
		}
		else if(key.equals("DOWN"))
		{
			robot.keyPress(KeyEvent.VK_DOWN);
		}
		else if(key.equals("RIGHT"))
		{
			robot.keyPress(KeyEvent.VK_RIGHT);
		}
		else if(key.equals("LEFT"))
		{
			robot.keyPress(KeyEvent.VK_LEFT);
		}
		else if(key.equals("A"))
		{
			robot.keyPress(KeyEvent.VK_A);
		}
		else if(key.equals("B"))
		{
			robot.keyPress(KeyEvent.VK_B);
		}
		else if(key.equals("C"))
		{
			robot.keyPress(KeyEvent.VK_C);
		}
		else if(key.equals("D"))
		{
			robot.keyPress(KeyEvent.VK_D);
		}
		else if(key.equals("E"))
		{
			robot.keyPress(KeyEvent.VK_E);
		}
		else if(key.equals("F"))
		{
			robot.keyPress(KeyEvent.VK_F);
		}
		else if(key.equals("G"))
		{
			robot.keyPress(KeyEvent.VK_G);
		}
		else if(key.equals("H"))
		{
			robot.keyPress(KeyEvent.VK_H);
		}
		else if(key.equals("I"))
		{
			robot.keyPress(KeyEvent.VK_I);
		}
		else if(key.equals("J"))
		{
			robot.keyPress(KeyEvent.VK_J);
		}
		else if(key.equals("K"))
		{
			robot.keyPress(KeyEvent.VK_K);
		}
		else if(key.equals("L"))
		{
			robot.keyPress(KeyEvent.VK_L);
		}
		else if(key.equals("M"))
		{
			robot.keyPress(KeyEvent.VK_M);
		}
		else if(key.equals("N"))
		{
			robot.keyPress(KeyEvent.VK_N);
		}
		else if(key.equals("O"))
		{
			robot.keyPress(KeyEvent.VK_O);
		}
		else if(key.equals("P"))
		{
			robot.keyPress(KeyEvent.VK_P);
		}
		else if(key.equals("Q"))
		{
			robot.keyPress(KeyEvent.VK_Q);
		}
		else if(key.equals("R"))
		{
			robot.keyPress(KeyEvent.VK_R);
		}
		else if(key.equals("S"))
		{
			robot.keyPress(KeyEvent.VK_S);
		}
		else if(key.equals("T"))
		{
			robot.keyPress(KeyEvent.VK_T);
		}
		else if(key.equals("U"))
		{
			robot.keyPress(KeyEvent.VK_U);
		}
		else if(key.equals("V"))
		{
			robot.keyPress(KeyEvent.VK_V);
		}
		else if(key.equals("W"))
		{
			robot.keyPress(KeyEvent.VK_W);
		}
		else if(key.equals("X"))
		{
			robot.keyPress(KeyEvent.VK_X);
		}
		else if(key.equals("Y"))
		{
			robot.keyPress(KeyEvent.VK_Y);
		}
		else if(key.equals("Z"))
		{
			robot.keyPress(KeyEvent.VK_Z);
		}
		else if(key.equals("1"))
		{
			robot.keyPress(KeyEvent.VK_1);
		}
		else if(key.equals("2"))
		{
			robot.keyPress(KeyEvent.VK_2);
		}
		else if(key.equals("3"))
		{
			robot.keyPress(KeyEvent.VK_3);
		}
		else if(key.equals("4"))
		{
			robot.keyPress(KeyEvent.VK_4);
		}
		else if(key.equals("5"))
		{
			robot.keyPress(KeyEvent.VK_5);
		}
		else if(key.equals("6"))
		{
			robot.keyPress(KeyEvent.VK_6);
		}
		else if(key.equals("7"))
		{
			robot.keyPress(KeyEvent.VK_7);
		}
		else if(key.equals("8"))
		{
			robot.keyPress(KeyEvent.VK_8);
		}
		else if(key.equals("9"))
		{
			robot.keyPress(KeyEvent.VK_9);
		}
		else if(key.equals("0"))
		{
			robot.keyPress(KeyEvent.VK_0);
		}
		else if(key.equals("F1"))
		{
			robot.keyPress(KeyEvent.VK_F1);
		}
		else if(key.equals("F2"))
		{
			robot.keyPress(KeyEvent.VK_F2);
		}
		else if(key.equals("F3"))
		{
			robot.keyPress(KeyEvent.VK_F3);
		}
		else if(key.equals("F4"))
		{
			robot.keyPress(KeyEvent.VK_F4);
		}
		else if(key.equals("F5"))
		{
			robot.keyPress(KeyEvent.VK_F5);
		}
		else if(key.equals("F6"))
		{
			robot.keyPress(KeyEvent.VK_F6);
		}
		else if(key.equals("F7"))
		{
			robot.keyPress(KeyEvent.VK_F7);
		}
		else if(key.equals("F8"))
		{
			robot.keyPress(KeyEvent.VK_F8);
		}
		else if(key.equals("F9"))
		{
			robot.keyPress(KeyEvent.VK_F9);
		}
		else if(key.equals("F10"))
		{
			robot.keyPress(KeyEvent.VK_F10);
		}
		else if(key.equals("F11"))
		{
			robot.keyPress(KeyEvent.VK_F11);
		}
		else if(key.equals("F12"))
		{
			robot.keyPress(KeyEvent.VK_F12);
		}
		
	}
	
	private static void releaseKey(String key)
	{
		if(key.equals("CTRL"))
		{
			robot.keyRelease(KeyEvent.VK_CONTROL);
		}
		else if(key.equals("ALT"))
		{
			robot.keyRelease(KeyEvent.VK_ALT);
		}
		else if(key.equals("SHIFT"))
		{
			robot.keyRelease(KeyEvent.VK_SHIFT);
		}
		else if(key.equals("TAB"))
		{
			robot.keyRelease(KeyEvent.VK_TAB);
		}
		else if(key.equals("ENTER"))
		{
			robot.keyRelease(KeyEvent.VK_ENTER);
		}
		else if(key.equals("ESC"))
		{
			robot.keyRelease(KeyEvent.VK_ESCAPE);
		}
		else if(key.equals("SUPR") || key.equals("DEL"))
		{
			robot.keyRelease(KeyEvent.VK_DELETE);
		}
		else if(key.equals("BACK SPACE"))
		{
			robot.keyRelease(KeyEvent.VK_BACK_SPACE);
		}
		else if(key.equals("SPACE"))
		{
			robot.keyRelease(KeyEvent.VK_SPACE);
		}
		else if(key.equals("UP"))
		{
			robot.keyRelease(KeyEvent.VK_UP);
		}
		else if(key.equals("DOWN"))
		{
			robot.keyRelease(KeyEvent.VK_DOWN);
		}
		else if(key.equals("RIGHT"))
		{
			robot.keyRelease(KeyEvent.VK_RIGHT);
		}
		else if(key.equals("LEFT"))
		{
			robot.keyRelease(KeyEvent.VK_LEFT);
		}
		else if(key.equals("A"))
		{
			robot.keyRelease(KeyEvent.VK_A);
		}
		else if(key.equals("B"))
		{
			robot.keyRelease(KeyEvent.VK_B);
		}
		else if(key.equals("C"))
		{
			robot.keyRelease(KeyEvent.VK_C);
		}
		else if(key.equals("D"))
		{
			robot.keyRelease(KeyEvent.VK_D);
		}
		else if(key.equals("E"))
		{
			robot.keyRelease(KeyEvent.VK_E);
		}
		else if(key.equals("F"))
		{
			robot.keyRelease(KeyEvent.VK_F);
		}
		else if(key.equals("G"))
		{
			robot.keyRelease(KeyEvent.VK_G);
		}
		else if(key.equals("H"))
		{
			robot.keyRelease(KeyEvent.VK_H);
		}
		else if(key.equals("I"))
		{
			robot.keyRelease(KeyEvent.VK_I);
		}
		else if(key.equals("J"))
		{
			robot.keyRelease(KeyEvent.VK_J);
		}
		else if(key.equals("K"))
		{
			robot.keyRelease(KeyEvent.VK_K);
		}
		else if(key.equals("L"))
		{
			robot.keyRelease(KeyEvent.VK_L);
		}
		else if(key.equals("M"))
		{
			robot.keyRelease(KeyEvent.VK_M);
		}
		else if(key.equals("N"))
		{
			robot.keyRelease(KeyEvent.VK_N);
		}
		else if(key.equals("O"))
		{
			robot.keyRelease(KeyEvent.VK_O);
		}
		else if(key.equals("P"))
		{
			robot.keyRelease(KeyEvent.VK_P);
		}
		else if(key.equals("Q"))
		{
			robot.keyRelease(KeyEvent.VK_Q);
		}
		else if(key.equals("R"))
		{
			robot.keyRelease(KeyEvent.VK_R);
		}
		else if(key.equals("S"))
		{
			robot.keyRelease(KeyEvent.VK_S);
		}
		else if(key.equals("T"))
		{
			robot.keyRelease(KeyEvent.VK_T);
		}
		else if(key.equals("U"))
		{
			robot.keyRelease(KeyEvent.VK_U);
		}
		else if(key.equals("V"))
		{
			robot.keyRelease(KeyEvent.VK_V);
		}
		else if(key.equals("W"))
		{
			robot.keyRelease(KeyEvent.VK_W);
		}
		else if(key.equals("X"))
		{
			robot.keyRelease(KeyEvent.VK_X);
		}
		else if(key.equals("Y"))
		{
			robot.keyRelease(KeyEvent.VK_Y);
		}
		else if(key.equals("Z"))
		{
			robot.keyRelease(KeyEvent.VK_Z);
		}
		else if(key.equals("1"))
		{
			robot.keyRelease(KeyEvent.VK_1);
		}
		else if(key.equals("2"))
		{
			robot.keyRelease(KeyEvent.VK_2);
		}
		else if(key.equals("3"))
		{
			robot.keyRelease(KeyEvent.VK_3);
		}
		else if(key.equals("4"))
		{
			robot.keyRelease(KeyEvent.VK_4);
		}
		else if(key.equals("5"))
		{
			robot.keyRelease(KeyEvent.VK_5);
		}
		else if(key.equals("6"))
		{
			robot.keyRelease(KeyEvent.VK_6);
		}
		else if(key.equals("7"))
		{
			robot.keyRelease(KeyEvent.VK_7);
		}
		else if(key.equals("8"))
		{
			robot.keyRelease(KeyEvent.VK_8);
		}
		else if(key.equals("9"))
		{
			robot.keyRelease(KeyEvent.VK_9);
		}
		else if(key.equals("0"))
		{
			robot.keyRelease(KeyEvent.VK_0);
		}
		else if(key.equals("F1"))
		{
			robot.keyRelease(KeyEvent.VK_F1);
		}
		else if(key.equals("F2"))
		{
			robot.keyRelease(KeyEvent.VK_F2);
		}
		else if(key.equals("F3"))
		{
			robot.keyRelease(KeyEvent.VK_F3);
		}
		else if(key.equals("F4"))
		{
			robot.keyRelease(KeyEvent.VK_F4);
		}
		else if(key.equals("F5"))
		{
			robot.keyRelease(KeyEvent.VK_F5);
		}
		else if(key.equals("F6"))
		{
			robot.keyRelease(KeyEvent.VK_F6);
		}
		else if(key.equals("F7"))
		{
			robot.keyRelease(KeyEvent.VK_F7);
		}
		else if(key.equals("F8"))
		{
			robot.keyRelease(KeyEvent.VK_F8);
		}
		else if(key.equals("F9"))
		{
			robot.keyRelease(KeyEvent.VK_F9);
		}
		else if(key.equals("F10"))
		{
			robot.keyRelease(KeyEvent.VK_F10);
		}
		else if(key.equals("F11"))
		{
			robot.keyRelease(KeyEvent.VK_F11);
		}
		else if(key.equals("F12"))
		{
			robot.keyRelease(KeyEvent.VK_F12);
		}
		else if(key.equals("SHUTDOWN"))
		{
			try {
				proc = runtime.exec("shutdown -s -t 0");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		else if(key.equals("SPOTIFY"))
		{
			try {
				proc = runtime.exec(spotyfolder);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
//	private static void processCommand(int command) {
//		try {
//			Robot robot = new Robot();
//			Runtime runtime = Runtime.getRuntime();
//    		Process proc;
//			switch (command) {
//	    	
//			
//			case 1:
//				robot.keyPress(KeyEvent.VK_ENTER);
//	    		System.out.println("Enter pressed");
//	    		// release the key after it is pressed. Otherwise the event just keeps getting trigged	    		
//	    		robot.keyRelease(KeyEvent.VK_ENTER);
//	    		break;
//			case 2:
//				robot.keyPress(KeyEvent.VK_UP);
//	    		System.out.println("UP pressed");
//	    		// release the key after it is pressed. Otherwise the event just keeps getting trigged	    		
//	    		robot.keyRelease(KeyEvent.VK_UP);
//	    		break;
//			case 3:
//				robot.keyPress(KeyEvent.VK_DOWN);
//	    		System.out.println("DOWN pressed");
//	    		// release the key after it is pressed. Otherwise the event just keeps getting trigged	    		
//	    		robot.keyRelease(KeyEvent.VK_DOWN);
//	    		break;
//			case 4:
//				robot.keyPress(KeyEvent.VK_CONTROL);
//				robot.keyPress(KeyEvent.VK_UP);
//	    		System.out.println("VOL+ pressed");
//	    		// release the key after it is pressed. Otherwise the event just keeps getting trigged	    		
//	    		robot.keyRelease(KeyEvent.VK_UP);
//	    		robot.keyRelease(KeyEvent.VK_CONTROL);
//	    		break;
//			case 5:
//				robot.keyPress(KeyEvent.VK_CONTROL);
//				robot.keyPress(KeyEvent.VK_DOWN);
//	    		System.out.println("VOL- pressed");
//	    		// release the key after it is pressed. Otherwise the event just keeps getting trigged	    		
//	    		robot.keyRelease(KeyEvent.VK_DOWN);
//	    		robot.keyRelease(KeyEvent.VK_CONTROL);
//	    		break;
//			case 6:
//	    		proc = runtime.exec("shutdown -s -t 0");
//	    		System.out.println("ON/OFF pressed");
//	    		break;
//			case 7:
//	    		proc = runtime.exec(spotyfolder);
//	    		System.out.println("openspoty pressed");
//	    		break;
//			case 8:
//				robot.keyPress(KeyEvent.VK_CONTROL);
//				robot.keyPress(KeyEvent.VK_RIGHT);
//	    		System.out.println("next pressed");
//	    		// release the key after it is pressed. Otherwise the event just keeps getting trigged	    		
//	    		robot.keyRelease(KeyEvent.VK_RIGHT);
//	    		robot.keyRelease(KeyEvent.VK_CONTROL);
//	    		break;
//			case 9:
//				robot.keyPress(KeyEvent.VK_CONTROL);
//				robot.keyPress(KeyEvent.VK_L);
//	    		System.out.println("List_select pressed");
//	    		// release the key after it is pressed. Otherwise the event just keeps getting trigged	    		
//	    		robot.keyRelease(KeyEvent.VK_L);
//	    		robot.keyRelease(KeyEvent.VK_CONTROL);
//	    		robot.keyPress(KeyEvent.VK_TAB);
//	    		robot.keyRelease(KeyEvent.VK_TAB);
//	    		break;		
//			case 10:
//	    		robot.keyPress(KeyEvent.VK_SPACE);
//	    		System.out.println("Space pressed");
//	    		// release the key after it is pressed. Otherwise the event just keeps getting trigged	    		
//	    		robot.keyRelease(KeyEvent.VK_SPACE);
//	    		break;
//			case 11:
//				robot.keyPress(KeyEvent.VK_CONTROL);
//				robot.keyPress(KeyEvent.VK_LEFT);
//	    		System.out.println("previous pressed");
//	    		// release the key after it is pressed. Otherwise the event just keeps getting trigged	    		
//	    		robot.keyRelease(KeyEvent.VK_LEFT);
//	    		robot.keyRelease(KeyEvent.VK_CONTROL);
//	    		break;
//	    	
//	    		
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}

}