/**
 * File: KeyGroups.java
 * @author A.Kurd <alan.kurd@trincoll.edu>
 * 
 * Description: Implements a Java 1.1 version of a GUI tool for creating "KeyGroups" and displaying them.  KeyGroups are
*  the letters of the cipher alphabet split into 5 groups based on their frequency.  Letters in the same group have similar
*  frequencies as the others in their group.
 * 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 analyzers;             // Analyzer plugins belong to this package

import hcrypto.analyzer.*;     // This is where Analyzer is
import java.awt.TextArea;
import hcrypto.cipher.*;
import java.awt.*;
import java.awt.event.*;   

public class KeyGroups extends Frame implements WindowListener, Analyzer{

   public  Frame  frame[] = new Frame[10];     //the frames that the top 10 KeyGroups are displayed
   public  KeyBuilder kb= new KeyBuilder();    //to count letter frequency
   public Calculate ct = new Calculate();          //to score the different KeyGroups
   public  BigramCount bc= new BigramCount();	//to collect Bigram data
   public  TrigramCount tc= new TrigramCount();	//to collect Trigram data
   public  String output [] = new String [10];		//The 10 strings were the output is collected
   public int result [] = new int[128];	//array to collect the order of letters for the KeyGroup
   private TextArea text1;     // Pointers to the text to be analyzed
   private TextArea display1;  // And the window where results are displayed
   public TextArea display [] = new TextArea[10]; 	//TextAreas where the KeyGroups will be displayed
   public char  freqI [] = {'e'};		//5 KeyGroups
   public char freqII [] = {'t', 'a', 'o', 'i', 'n', 's', 'h', 'r'};
   public char freqIII[] = { 'd', 'l'};
   public char freqIV [] = { 'c', 'u', 'm', 'w', 'f', 'g', 'y', 'p', 'b'};
   public char freqV[] = {'v', 'k', 'j', 'x', 'q', 'z'};
   public int total [] = new int [10];	//int array to collect the scores of the different KeyGroups

   public  void KeyGroups(String text){ }//BigramDisplay

//necessary as part of the Analysis Package and displays this class has been called

   public void setup(TextArea text1, TextArea display1) { 
      display1.append("Key Groups Called" + "\n");
      this.text1 = text1;
      this.display1 = display1;
   }//setup

/*first method called by this class. It calculates the letter frequency, bigram frequency, and trigram frequency.
   It the creates possible KeyGroups, in arrays, scores the different KeyGroups, and displays each one in a frame.*/

   public void run() {
      kb.count(text1.getText());
      bc.count(text1.getText());
      tc.count(text1.getText());
//this loop begins at 0 and only loops through 3 times creating 3 possible KeyGroups
      for( int i = 0; i<3; i++){
         create(i);
         score(i,result);
         frame(i); }//for
   }//run

/*This method creates the possible KeyGroups in an array called result.  It shifts the first 9 letters of the array creating the
   optimal possible output.  The input is the KeyGroup created, between 1 and 3.  It orders the whole array of letter frequency 
    collected in KeyBuilder's array tabulate and stores the new array in result[].
     Input: int representing the iteration of the KeyGroup
     Output: and int array of the reordered letter frequency.*/
   public int[] create(int i){
      if(i == 0){
         for(int k = 0; k<kb.tabulate.length; k++)
            result [k] = kb.tabulate[k][1];}//if
      else{
         result[0] = kb.tabulate[i][1];
         result[i] = kb.tabulate[0][1];
         for(int k = 1; k<kb.tabulate.length; k++)
            if (k == i) result[k] = result[k];
            else result[k] = kb.tabulate[k][1]; }//else
      return result;
   }//create

/*This method scores each result array created by calling ct.triFreq() for how often Trigram hits (matches) occur and
    then ct.accumulate to accumulate the total number of bigram and trigram hits.  The calculate method accepts the 
    Trigram frequency and Bigram frequency which is already calculated.*/

   public void score (int j, int [] res){
     ct.triFreq(tc.frequencies);
     total[j] = ct.accumulate(res, bc.tabulate, 0);
   }//score

//This method outputs the different groups seperated into 5 seperate groups.

   public  void frame(int l){   
      
      frame[l] = new Frame ("Key Groups " + (l+1));
      frame[l].addWindowListener(this);
      frame[l].setResizable(true);
      frame[l].setSize(400, 200);
      if(l<5) frame[l].setLocation(5, 150*l);
      else frame[l].setLocation(500, 150*(l-5));
      frame[l].setVisible(true);

      output[l] = "";

      output[l] = output[l]  + "plaintext" + "\t" + "\t"+ "\t" + "encrypted text" + "\n";
      output[l] = output[l] + "I)  " + freqI[0] + ": " + "\t" + "\t" + "\t" + (char)result[0] + "" + "\n";
      output[l] = output[l] + "II)  ";
      for (int k = 0; k<8; k++) output[l] = output[l] + freqII[k] + "  ";
      output[l] = output[l] + ":" + "\t";
      for (int k=1; k < 9; k++){
         if (kb.tabulate [k] [0] > 0)
            output[l] = output[l] + ((char)result[k] + "" + "  ");}//for 
         output[l] = output[l] + "\n" + "III)  ";
      for (int k = 0; k<2; k++) output[l] = output[l] + freqIII[k] + "  ";
      output[l] = output[l] + ":" + "\t"+ "\t"+ "\t";
      for (int k=9; k < 11; k++){
         if (kb.tabulate [k] [0] > 0)
            output[l] = output[l] + ((char)result[k] + "" + "  ");}//for 
         output[l] = output[l] + "\n" + "IV)  ";
      for (int k = 0; k<9; k++) output[l] = output[l] + freqIV[k] + "  ";
      output[l] = output[l] + ":" + "\t";
      for (int k=11; k < 20; k++){
         if (kb.tabulate [k] [0] > 0)
            output[l] = output[l] + ((char)result [k] + "" + "  ");}//for 
         output[l] = output[l] + "\n" + "V)  ";
      for (int k = 0; k<6; k++) output[l] = output[l] + freqV[k] + "  ";
      output[l] = output[l] + ":" + "\t"+ "\t";
      for (int k=20; k < 26; k++){
         if (kb.tabulate [k] [0] > 0)
            output[l] = output[l] + ((char)result[k] + "" + "  ");}//for 
     output[l] = output[l] + "\n" + "\n" + "SCORE:  " + total[l];

      display[l] = new TextArea (output[l]);
      frame[l].add(display[l]);
      frame[l].show();
   }//frame
    public void windowClosing(WindowEvent e) {
        for(int l = 0; l<10; l++)
        frame[l].dispose();
    }
    public void windowActivated(WindowEvent e) {}	
    public void windowDeactivated(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}   
}//KeyGroups Class