/**
 * @(#)ScrabbleProtocol.java	11/03/05
 * 
 * Copyright 2005 3GP01, KCL.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * The GNU General Public Licence should be contained within licence.txt;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA  02110-1301, USA.
 */

/**
 * Class to handle message transaction in the remote game.
 * Allows for simple encryption and coding of messages.
 *
 * @author 3GP01 - Hardev Bhamra, Deepak Chandarana, James Tompkin
 * @version 2.0
 * @see Client
 * @see ServerThread
 */

public class ScrabbleProtocol 
{  	
	/*
	 * The MESSAGETYPES:
	 * 			1  Client requests UID
	 * 			2  Client receives join confirmation
	 * 			3  Dictionary check request
	 * 			4  Dictionary send request
	 *			5  Game start
	 *			6  Seed request
	 *			7  Received letter move
	 *			8  Swap letters request
	 *			9  End game request
	 *			10 Message received
	 * 
	 */
	
	
	/**
	 * Method to encypt a message based on a simple rotation.
	 * 
	 * @param plaintext The text to encrypt.
	 * @return String
	 */
	public String encryptMessage(String plaintext)
	{
		int rotate = plaintext.length()/2;
		String ciphertext = "";
		
		//for each charater in the plaintext rotate
		for(int i = 0; i < plaintext.length(); i++)
		{
			char symbol = plaintext.charAt((i+rotate)%plaintext.length());
			ciphertext = ciphertext + symbol;
		}
		
		return ciphertext;
	}
	
	/**
	 * Rotation method to decrypt the encoded message.
	 * 
	 * @param ciphertext The ciphertext.
	 * @return String The plaintext.
	 */
	public String decryptMessage(String ciphertext)
	{	
		int rotate;
		
		//if method length is even
		if(ciphertext.length()%2 == 0)
		{
			rotate = ciphertext.length()/2;
		}
		else
		{
			rotate = ciphertext.length()/2 + 1;
		}
		
		String plaintext = "";
		
		//for each charater in the ciphertext roate
		for(int i = 0; i < ciphertext.length(); i++)
		{
			char symbol = ciphertext.charAt((i+rotate)%ciphertext.length());
			plaintext = plaintext + symbol;
		}
		
		return plaintext;
	}
	
	
    /**
     * Processes an encrypted, coded message and returns a String array containing
     * only the useful information.
     * 
     * @param theInput The ciphertext message.
     * @return String[]
     */
    public String[] processInput(String theInput) 
    {	
    	decryptMessage(theInput);
    	
        String[] theOutput = new String[3];
        int index = theInput.indexOf("!");
        int lastindex = theInput.lastIndexOf("!");
        theOutput[0] = theInput.substring(0, index);
        theOutput[1] = theInput.substring(index+1, lastindex);
        theOutput[2] = theInput.substring(lastindex+1);
        
        return theOutput;
    }
    
    /**
     * Constructs, codes and encrypts an outgoing message.
     * 
     * @param uid The unique identifier of the client.
     * @param index The message type.
     * @param message The message body.
     * @return String The ciphertext.
     */
    public String processOutput(int uid, int index, String message) 
    {	
    	String theOutput = uid + "!" + index + "!" + message;
    	
    	encryptMessage(theOutput);
    	
        return theOutput;
    }
}
