/**
 * File: KeyBuilder.java
 * @author A.Kurd <alan.kurd@trincoll.edu>
 * 
 * Description: Implements a Java 1.1 version of a GUI tool for finding the letter frequency of each encrypted letter of the cipher
*  alphabet.  It then orders the letters based on frequency.
 * Credits: This program is modelled after the frequency test of the CryptoToolJ program by Ralph Morelli.
 *
 * Copyright: This program is in the public domain. You can do whatever you want with
 *  it as long as I get credit for my work and as long as you offer your changes to me
 *  so I can possible add them to the "official" version.
 */

package hcrypto.analyzer;

import hcrypto.cipher.*;
import java.awt.*;

public class KeyBuilder {

    public  final int MAX_CHARS = 128;      // Possibly all ASCII characters
    private  int alphabetRange = Alphabet.RANGE_ascii;    
    public  String message;
    private  Alphabet alphabet = null;
    public  int tabulate [][] = new int [MAX_CHARS][2];

    public  void KeyBuilder(String text) {
        message = text;
    } //KeyBuilder

/*This method cycles through the entire method collecting how often each letter appears in the encrypted text and incrementing 
  their frequency accordingly. The output is then ordered into an array called tabulate with the characters represented by their 
   ASCII integer representation.*/

    public  void count(String text) {    
        int size = 128;
        FrequencyRecord frequencies[] = new FrequencyRecord[size];  
        for (int k = 0; k < frequencies.length; k++)
            frequencies[k]= new FrequencyRecord((char)k, 0);

        try {  
            alphabet = new Alphabet(alphabetRange);
        } catch (Exception e) {
            e.printStackTrace();
        }//catch
   
        int charCount = 0;
        int alphabeticsCount = 0;
        int highestFreq = 0;
        
        for (int k = 0; k < text.length(); k++) {
            char ch = text.charAt(k);
            if (Character.isLetter(ch))
                 ++alphabeticsCount;
            if (alphabet.isInAlphabet(ch)) {                                    // Ignore chars > 127
                 charCount++;
                 frequencies[ch].count++; 
             } //if
        } //for

        int j = 0;
        for (int k = 0; k < frequencies.length; k++)
            {   if (frequencies [k].count > 0)
                {   highestFreq = frequencies[k].count;         // Keep track of highest f   
                    tabulate [j] [0] = highestFreq;
                    tabulate [j] [1] = k;
                    j++;
                }//if
            }//for

      int temp, temp1, temp2;
      for (int pass = 1; pass < tabulate.length; pass++)
         for (int pair = 1; pair < tabulate.length; pair++)
            if (tabulate[pair -1][0] < tabulate[pair] [0]){
               temp = tabulate[pair-1][0];
               temp1 = tabulate[pair-1][1];
               tabulate[pair-1][0] = tabulate[pair][0];
               tabulate[pair-1][1] = tabulate[pair][1];
               tabulate[pair][0] = temp;
               tabulate[pair][1] = temp1;
            }//if
    }  //count
}//class


 
   