import java.awt.*;
import java.awt.event.*;
import java.math.*;
import java.io.*;


// Erbe von Frame -> Application
public class MyRSAGUI extends Frame
    implements ActionListener, WindowListener {

    Label privSchl, oeffSchl, modulus, feld1Label, feld2Label, feld3Label, feld4Label,
    feld5Label, feld6Label, feld8Label,feld7Label,leeresLabel, feld2, verschluesselt, entschluesselt;
    Button b1;
    Panel hauptPanel, westPanel, unteresPanel;
    TextField feld1, feld3 ;
    MenuBar  mb;
    Menu     rsaMenu;
    MenuItem gen, chif, ende, speicher, sauber;
    public static String eingabe;
    BigInteger zwischenschritt, zwischenschritt2, zwischenschritt3;

    public MyRSAGUI () {
        super();
    }

    public void init () {

        //Label
        feld1Label = new Label ("Klartext");
        feld2Label = new Label ("Eingegebener Text                   ");
        feld3Label = new Label ("erzeugter Privater Schlüssel        ");
        feld4Label = new Label ("erzeugter Öffentlicher Schlüssel    ");
        feld5Label = new Label ("erzeugte Moduluszahl                ");
        feld6Label = new Label ("erzeugtes  Chiffre                  ");

        privSchl        = new Label ("");
        oeffSchl        = new Label ("");
        modulus         = new Label ("");
        verschluesselt  = new Label ("");

        //Panel
        hauptPanel = new Panel();
        hauptPanel.setLayout (new FlowLayout() );

        //westliches Panel für Label
        westPanel = new Panel();
        westPanel.setLayout(new GridLayout(9, 3));

        // oestliches Panel für Text
        unteresPanel = new Panel();
        unteresPanel.setLayout (new GridLayout(9, 3));

        add(hauptPanel);

        // westliches Panel (Labels)
        hauptPanel.add(westPanel, "West");
        westPanel.add("center",feld1Label);
        westPanel.add("center",feld2Label);
        westPanel.add("center",feld3Label);
        westPanel.add("center",feld4Label);
        westPanel.add("center",feld5Label);
        westPanel.add("center",feld6Label);


        // unteres Panel (Osten:  EIngabe)
        hauptPanel.add (unteresPanel, "East");
        feld1 = new TextField("Texteingabe", 60);
        feld1.addActionListener(this);
        feld2 = new Label("");
        unteresPanel.add("center", feld1);
        unteresPanel.add("center", feld2);
        unteresPanel.add("center", privSchl);
        unteresPanel.add("center", oeffSchl);
        unteresPanel.add("center", modulus);
        unteresPanel.add("center", verschluesselt);
        setVisible(true);


        // Menuezeile erzeugen
        mb = new MenuBar();
        setMenuBar(mb);
        // Eintraege für Menü
        rsaMenu = new Menu("MyRSA");
        mb.add(rsaMenu);
        gen = new MenuItem("Generiere Schlüssel");
        rsaMenu.add(gen);
        chif = new MenuItem("Chiffirere");
        rsaMenu.add(chif);
        speicher = new MenuItem(":w");
        rsaMenu.add(speicher);
        sauber = new MenuItem("saeubern");
        rsaMenu.add(sauber);
        rsaMenu.addSeparator();
        ende = new MenuItem("Ende");
        rsaMenu.add(ende);
        // ActionListener für Menü
        rsaMenu.addActionListener(this);
        // Menübefehlaktionen
        gen.setActionCommand("generiere");
        chif.setActionCommand("chiffriere");
        speicher.setActionCommand("speicher");
        sauber.setActionCommand("wischen");
        ende.setActionCommand("beende");


    }// init()

    // Aufbau der neuen Oberfläche
    public static void main (String[] args) {
        MyRSAGUI Oberflaeche = new MyRSAGUI();
        Oberflaeche.init();
        Oberflaeche.pack();
    }

    //
    // ACTION PERFORMED
    //
    public void actionPerformed (ActionEvent e) {
        String which = e.getActionCommand();


        //  __  __         _  _         _   _
        // |  \/  |___ _ _(_)(_)__ _ __| |_(_)___ _ _
        // | |\/| / -_) ' \ || / _` / _|  _| / _ \ ' \
        // |_|  |_\___|_||_\_,_\__,_\__|\__|_\___/_||_|

        //
        // Schlüssel generieren via Menü
        //
        if (which.equals("generiere")){

            // privater Schlüssel
            zwischenschritt2 = MyRSA.privatSchluessel();
            privSchl.setText(zwischenschritt2.toString());

            // oeffentlicher Schlüssel
            zwischenschritt = MyRSA.pubKey;
            oeffSchl.setText(zwischenschritt.toString());

            // modulus
            zwischenschritt3 = MyRSA.modulus;
            modulus.setText(zwischenschritt3.toString());


        }
        //
        // Eingabe verschlüsseln
        //
        else if (which.equals("chiffriere")){
            eingabe = feld1.getText().toString();
            verschluesselt.setText(MyRSA.verschluessele(eingabe).toString());

        }

        //
        // Speichern
        //
        else if (which.equals("speicher")){
            //                        öffentlicher, privater,         modulus
            try{MyRSA.speichereDatei(zwischenschritt, zwischenschritt2, zwischenschritt3);}
            catch (IOException f){
                return;
            }

            //
            // ende
            //
        }
        else if (which.equals("beende")){
            //töten
            dispose();
            // Null returnen
            System.exit(0);
        }


        // Textfeld löschen 
        else if (which.equals("wischen")) {
            entschluesselt.setText("");
            verschluesselt.setText("");
        }


    }// Action Performed

    public void windowClosing (WindowEvent e) {
        dispose();
        System.exit(0);
    }
    public void windowClosed (WindowEvent e) { }
    public void windowOpened (WindowEvent e) { }
    public void windowIconified (WindowEvent e) { }
    public void windowDeiconified (WindowEvent e) { }
    public void windowActivated (WindowEvent e) { }
    public void windowDeactivated (WindowEvent e) { }
}
