/**
 * decodage.java - 01/12/01 - 25/01/11 - 20/09/11 - 8/05/16 - 29/08/22
 * decodage par permutation de lettres
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.lang.Math;

public class decodage extends JFrame implements ActionListener {
	static final long serialVersionUID = 220829L;
	TextArea ttexte, tstat;
	Font f, fc;
	JLabel lalpha, lettres, lfleche, Mlettres;
	JTextField tl, tln;
	JButton remplace, stat, init, annule;
	String alphabet = "abcdefghijklmnopqrstuvwxyz";
	String sblettres = "abcdefghijklmnopqrstuvwxyz";
	char[] lcar = new char [52];
	int index = -1;

	public decodage(String titre) {
		super (titre);
		setFont (new Font ("Arial", Font.PLAIN, 12));
		setLayout (new BorderLayout ());
		JPanel p = new JPanel ();
		p.setBackground (Color.lightGray);
		p.setLayout (new GridLayout (2,2));
		p.add (lalpha = new JLabel (alphabet));
		JPanel p1 = new JPanel ();
		p1.setBackground (Color.lightGray);
		p1.add (tl = new JTextField (1));
		p1.add (lfleche = new JLabel ("-->"));
		p1.add (tln = new JTextField (1));
		p1.add (remplace = new JButton ("remplace"));
		remplace.addActionListener (this);
		p1.add (annule = new JButton ("annule"));
		annule.addActionListener (this);
		p1.add (stat = new JButton ("stat"));
		stat.addActionListener (this);
		p1.add (init = new JButton ("initialise"));
		init.addActionListener (this);
		p.add (p1);
		p.add (lettres = new JLabel (sblettres));
		p.add (Mlettres = new JLabel ("--------------------------"));
		add (p, BorderLayout.NORTH);
		add (ttexte = new TextArea (20, 20), BorderLayout.CENTER);
		ttexte.setFont (new Font ("Courier", Font.BOLD, 12));
		add (tstat = new TextArea (3, 20), BorderLayout.SOUTH);
	}

	private String arrondi (double d) {
		d = Math.floor (d * 1000.0 + 0.5) / 10.0;
		return Double.toString (d);
}


	public void actionPerformed (ActionEvent evt) {
		String s = ttexte.getText ();
		if (evt.getSource() == init) {
			sblettres = "abcdefghijklmnopqrstuvwxyz";
			lettres.setText (sblettres);
			ttexte.setText ("");
			tstat.setText ("");
			Mlettres.setText ("");
			tl.setText ("");
			tln.setText ("");
			index = -1;
		} else if (evt.getSource() == stat) {
			int[] effectifs = new int [26];
			for (int i = 0; i < s.length (); i++) {
				int j = sblettres.indexOf (s.charAt (i));
				if (j != -1) effectifs [j] ++;
			}
			String s1 = "";
			String s2 = "";
			double total = 0.0;
			for (int i = 0; i < 26; i++)
				total += effectifs [i];
			for (int i = 0; i < 26; i++) {
				int max = -1; int imax = - 1;
				for (int j = 0; j < 26; j++) {
					int k = effectifs [j];
					if (k > max) {
						max = k;
						imax = j;
					}
				}
				if (imax != -1 && effectifs [imax] > 0) {
					s1 += sblettres.charAt (imax) + "\t";
					s2 += arrondi (max / total) + "\t";
					effectifs [imax] = -1;
				}
			}
			tstat.setText (s1 + "\n" + s2);
		} else if (evt.getSource() == remplace) {
			String soc = tl.getText().toLowerCase();
			String snc = tln.getText().toUpperCase();
			if (soc.length () > 0 && snc.length () > 0) {
				char oc = soc.charAt(0);
				char nc = snc.charAt(0);
				tl.setText(soc.substring(0, 1));
				tln.setText(snc.substring(0, 1));
				int i = 0;
				for (; i <= index; i++) {
					if (nc == lcar[i])
						break;
				}
				if (i > index) {
					sblettres = sblettres.replace (oc, nc);
					s = s.replace (oc, nc);
					lettres.setText (sblettres);
					ttexte.setText (s);
					s = "";
					for (i = 0; i < alphabet.length (); i ++) {
						char c = Character.toUpperCase (alphabet.charAt (i));
						if (sblettres.indexOf (c) != -1)
							s += c;
					}
					Mlettres.setText(s);
					lcar [++index] = oc;
					lcar [++index] = nc;
				}
			}
		} else if (evt.getSource() == annule) {
			if (index > 0) {
				char oc = lcar [index --];
				char nc = lcar [index --];
				sblettres = sblettres.replace (oc, nc);
				s = s.replace (oc, nc);
				lettres.setText (sblettres);
				ttexte.setText (s);
				s = "";
				for (int i = 0; i < alphabet.length(); i++) {
					char c = Character.toUpperCase (alphabet.charAt (i));
					if (sblettres.indexOf (c) != -1)
						s += c;
				}
				Mlettres.setText (s);
			}
		}
	}


	public static void main (String args []) {
		int w = 700;
		int h = 400;
		decodage f = new decodage("Décodage d'un texte");
		f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		f.setSize (w, h);
		f.setVisible (true);
	}

}
