// Protocol.java
// protocol used for the client/server to communicate

import java.net.*;
import java.io.*;
import java.awt.Color;
import java.text.*;

public class Protocol  {
  protected int counter = 0;

  // convert a string to an int
  private int atoi (String str) {
    Integer temp = new Integer (str);

    return temp.intValue();
  }

  public String processInput (String theInput, Space space) {
    String theOutput = null;
    String playerID = null;
    int x;
    int y;
    int colorIdx;
    Color color = null;
    Token tokenObject = new Token();

    String token = tokenObject.getNextToken (theInput);
    
    if (token.equals ("ADD")) {
      playerID = tokenObject.getNextToken (theInput);
      x = atoi (tokenObject.getNextToken (theInput));  // x coordinate
      y = atoi (tokenObject.getNextToken (theInput));  // y coordinate
      colorIdx = atoi (tokenObject.getNextToken (theInput)); // color is 0..9

      switch (colorIdx) {
      case 0: color = new Color (0.0f, 0.0f, 1.0f); break;
      case 1: color = new Color (0.0f, 1.0f, 0.0f); break;
      case 2: color = new Color (0.0f, 1.0f, 1.0f); break;
      case 3: color = new Color (1.0f, 0.0f, 0.0f); break;
      case 4: color = new Color (1.0f, 0.0f, 1.0f); break;
      case 5: color = new Color (1.0f, 1.0f, 0.0f); break;
      case 6: color = new Color (1.0f, 1.0f, 1.0f); break;
      case 7: color = new Color (0.7f, 0.3f, 0.5f); break;
      case 8: color = new Color (0.8f, 0.8f, 0.1f); break;
      case 9: color = new Color (0.3f, 0.4f, 0.5f); break;
      }
      
      space.addPlayer (playerID, x, y,
                       color); 
      theOutput = "+ ACK ADD " + playerID + " " + x + " " + y + " " + colorIdx;
    }
    else if (token.equals ("DEL")) {
      playerID = tokenObject.getNextToken (theInput);
      space.deletePlayer (playerID);    // delete player along w/ their bullet
      theOutput = "+ ACK DEL " + playerID + "\n";
    }
    else if (token.equals ("GET")) {
      double[][] p = new double[3][3];   // for temp storage of orientation
      theOutput = "";
      for (int i = 0; i < space.ships(); i++) {
        Ship ship = space.getShip (i);
        p = ship.dimensions();
        NumberFormat numberFormatter = NumberFormat.getNumberInstance();
        numberFormatter.setMaximumFractionDigits (2);

        String x0 = numberFormatter.format (p[0][0]);
        String y0 = numberFormatter.format (p[0][1]);
        String x1 = numberFormatter.format (p[1][0]);
        String y1 = numberFormatter.format (p[1][1]);
        String x2 = numberFormatter.format (p[2][0]);
        String y2 = numberFormatter.format (p[2][1]);
       
        theOutput += ship.shipOwner() + " " +
          ship.x() + " " + ship.y() + " " +
          x0 + " " + y0 + " " +
          x1 + " " + y1 + " " +
          x2 + " " + y2 + " " +
          ship.xMotion() + " " + ship.yMotion() + " " +    // dx, dy
          ship.shipTilt() + " " +                 // the tilt of the ship
          ship.shipColor().getRed() + " " +
          ship.shipColor().getGreen() + " " +
          ship.shipColor().getBlue() + " " +
          ship.getScore() + ";\n";
      }
      
      for (int i = 0; i < space.bullets(); i++) {
        Bullet bullet = space.getBullet (i);
        theOutput += bullet.bulletOwner() + " " +
          bullet.x() + " " + bullet.y() + " " +
          bullet.xMotion() + " " + bullet.yMotion() + ";\n";
      }
      theOutput += "EOT";
      return theOutput;
    }
    else if (token.equals ("UPD")) {
      boolean isNewBullet;
      int dx, dy, theta;
      String newBullet = null;
      
      playerID = tokenObject.getNextToken (theInput);
      dx = atoi (tokenObject.getNextToken (theInput));   
      dy = atoi (tokenObject.getNextToken (theInput));    
      theta = atoi (tokenObject.getNextToken (theInput));  
      newBullet = tokenObject.getNextToken (theInput);  // y | n
      if (newBullet.equals ("y")) 
        isNewBullet = true;
      else
        isNewBullet = false;

      space.updatePlayer (playerID, dx, dy, theta, isNewBullet);
      
      theOutput = "+ ACK UPD " +
        playerID + " " +
        dx + " " +
        dy + " " +
        theta + " " +
        newBullet;
    }
    else
      theOutput = "- NACK\n";

    return theOutput;
  }
}

