/* @(#)McLaughlin_Encode.java   06 October 2005 */
/* Erin McLaughlin */


/* Imports */


import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;
import edu.neu.ccs.codec.*;
import edu.neu.ccs.console.*;
import edu.neu.ccs.filter.*;
import edu.neu.ccs.jpf.*;
import edu.neu.ccs.parser.*;
import edu.neu.ccs.pedagogy.*;
import edu.neu.ccs.quick.*;
import edu.neu.ccs.util.*;


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.beans.*;
import java.lang.reflect.*;
import java.net.URL;
import java.util.regex.*;
import java.text.ParseException;


public class McLaughlin_Encode extends TablePanel {
    
    private static int width =
        TextFieldView.getSampleWidth(20, '0');
    
    private TextFieldView view1 =
        new TextFieldView("0", width);
    
    private TextFieldView view2 =
        new TextFieldView("0", width);
    
    private TextFieldView view3 =
        new TextFieldView("", width);
    
    private TextFieldView encoded =
        new TextFieldView("", width);
    
    private SimpleAction encodeAction =
        new SimpleAction("McLaughlin_Encode Sentence") {
            public void perform() { encode(); }
        
    };
    
    private Object[][] dataStuff =
        new Object[][] {
            { "code prefix", new Halo(view1) },
            { "mod prefix", new Halo(view2) },
            { "phrase", new Halo(view3) },            
            { "encoded message", new Halo(encoded) }
    };
    
    private TablePanel dataPanel =
        new TablePanel(
            dataStuff, 10, 10, WEST);
    
    private Object[] mainStuff =
        new Object[] { dataPanel, encodeAction };
    
    private TablePanel mainPanel =
        new TablePanel(
            mainStuff, VERTICAL, 10, 10, CENTER);
    
    
    public McLaughlin_Encode() {
        add(new Halo(mainPanel, 10, 10));
        addListeners();
        
        frame("Message Encoder");
    }
    
    
    private void encode() {
        double x1 = view1.demandDouble();
        double x2 = view2.demandDouble();
        String x3 = view3.demandString();
        
        String x = "";
        
        x3 = x3.toUpperCase();
        int ln = x3.length();
        int loc = 0;
        char[] str = new char[ln];
        
        while (loc < ln) {
                
                //converts char at that location to ASCII
                int temp = (int) x3.charAt(loc);
                
                //converts to new CODE 0-25 represent letters, 26 is a space,
                //  27 is a period, and 28 is a comma
                if (temp >= 65){
                        temp = temp - 65;       //changes temp to number between 0 and 25
                                                                //corresponds with letter of the alphabet
                temp = (int) ((x1 * temp) + x2) % 29;   //(x1*code + x2)mod29
                                                                                            //gives new letter
                } else if (temp == 32){
                        temp = 26;      //code 26 represents a space
                        temp = (int) ((x1 * temp) + x2) % 29;
                } else if (temp == 46){
                        temp = 27; //represents a period
                        temp = (int) ((x1 * temp) + x2) % 29;
                } else if (temp == 44){
                        temp = 28; //represents a comma
                        temp = (int) ((x1 * temp) + x2) % 29;
                }
                
                //converts back to ASCII
                if (temp <= 25){
                        temp = temp + 65;
                } else if (temp == 26){
                        temp = 32;
                } else if (temp == 27){
                        temp = 46;
                } else if (temp == 28){
                        temp = 44;
                }
                
                //converts ASCII to char
                str[loc] = (char) temp;
                loc = loc + 1;
        }
        
        //converts char array to new encoded string
        
        x = new String(str);
        
        encoded.setViewState(x);
    }
    
    private void addListeners() {
        view1.addActionListener(encodeAction);
        view2.addActionListener(encodeAction);
        view3.addActionListener(encodeAction);
    }
}
