import java.net.*;
import java.io.*;
import java.util.*;
/**
 *  This class allows the checker game to be played over
 *  a network.  This class extends Player class
 *  @vesion 4.0
 *  
 */
class NetPlayer extends Player 
{
   private Scanner in;
   private PrintWriter out;
   private static int port = 4444;
   private ServerSocket serverSocket;
   private Socket clientSocket;

   /**
     *  Constructor for the server socket  This method throws
     *  an IOException
     *  @param pNum      int for the player number
     *  @param dir       int direction of play
     *  @param n         String string for name
     *  @param boardRef  Board this is a reference to the Board Class
     *  
    */
   // Server Socket
   public NetPlayer (int pNum, int dir, String n, Board boardRef) throws IOException
   {
      super(pNum, dir, n, boardRef);
      serverSocket = new ServerSocket (port);
      clientSocket = serverSocket.accept();

      // use the InputStream object to create a Scanner.
      in = new Scanner (clientSocket.getInputStream());

      // use the OutputStream object to create a PrintWriter
      out = new PrintWriter (clientSocket.getOutputStream(), true);
   }

   /**
     *  Constructor for the Client socket.  This method throws an
     *  IOException
     *  @param  pNum         int for the player number
     *  @param  dir          int direction
     *  @param  n            String for name
     *  @param  server       String for server
     *  @param  boardRef     Board this is a reference to the Board Class
    */ 
   // Client Socket
   public NetPlayer (int pNum, int dir, String n, String server, Board boardRef) throws 
                     UnknownHostException, IOException
   {
      super(pNum, dir, n, boardRef);
      clientSocket = new Socket(server, port);

      // use the InputStream object to create a Scanner.
      in = new Scanner (clientSocket.getInputStream());

      // use the OutputStream object to create a PrintWriter
      out = new PrintWriter (clientSocket.getOutputStream(), true);
   }
   
   /**
     *  This methods sends the name of a player
     *  @param n      String  that represents a name
    */
   public void sendMyName(String n)
   {
      out.println(n);
   }
   
   /**
     *  This method is an accessor of the opponents name
     *  @return  String
    */
   public String getOpponentName()
   {
      return in.nextLine().intern();
   }
   /**
     *  This method returns the interger 1 for NetPlayer
     *  @return  int    the interger 1 represents NetPlayer
    */
   public int playerType()
   {
      return 1;
   }

   /**
     *  This method ends the turn this player and sets the 
     *  opponents turn
     *  @return  int 
     *
    */
   public int endTurn()
   {
      this.isMyTurn = false;
      return this.opponent.setTurn();
   }
   /**
     *  This method sets the turn of the appropriate player
     *  @return  int    this is for the player number
    */
   public int setTurn()
   {
      this.isMyTurn = true;
      out.println("end");
      return this.getPlayerNumber();
   }
   
   /**
     *  This method sends the coordinates for the current move
     *  @param  col1   column
     *  @param  row1   row
     *  @param  col2   column
     *  @param  row2   row
    */
   public void sendMove(int col1, int row1, int col2, int row2)
   {
      out.println((7 - col1) + " " + (7 - row1) + " " + (7 - col2) + " " + (7 - row2));
   }
   /**
     *  This method parses coordinates that are sent.
     *  @param b   reference to the board 
    */
   public synchronized void waitForTurn(Board b)
   {
      String commandGetter = "";
      StringTokenizer st;
      int commandsGotten = 0, col1, row1, col2, row2;
      
      while (commandGetter != "end")
      {
         b.repaint();
         if (in.hasNextLine())
         {
            commandGetter = (in.nextLine()).intern();
            commandsGotten++;
            
            if (commandGetter == "end" && commandsGotten == 1)
            {
               hasWon = true;
               b.gameOver = true;
            }
            
             if (commandGetter != "end")
            {
               st = new StringTokenizer(commandGetter);
               
               col1 = Integer.parseInt(st.nextToken());
               row1 = Integer.parseInt(st.nextToken());
               col2 = Integer.parseInt(st.nextToken());
               row2 = Integer.parseInt(st.nextToken());
               
               b.draggedPiece = b.sq[col1][row1].contents;
               b.draggedPiece.executeMove(col2, row2, b);
               //b.crownCheck(col2, row2);
               
               b.repaint();
            }
            
            if (commandGetter == "end" && commandsGotten > 1)
            {
               b.currentPlayer = b.players[b.thisPlayer].setTurn();
               b.setLabel();
               b.repaint();
            }
            
            if (!b.draggedPiece.isNull())
            {
               b.draggedPiece = new Pawn();
            }
         }
      }
   }
   
   /**
     *  This method is a synchronized method used with threads
     *  
    */
   public synchronized void run()
   {
      sendMyName(this.opponent.name);
      while (!(in.hasNextLine()));
      this.name = getOpponentName();
      this.b.setLabel();
      
      while (!b.gameOver)
      {
         while (isMyTurn)
         {
            waitForTurn(b);
         }
      }
   }
}