/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.vutbr.feec.utko.fojtoval.krypto.funkce;


/**
 * Pomocne metody pro kresleni grafu a tabulek frevencni analyzy.
 * @author fojtoval
 */
public class Fa extends Statistika {
   
    /**
     * Prochazi zadany text a do pole fre pricita pocet vyskytu jednotlivych pismen.
     * @param text
     */
    public void fa(String text) {

        int[]fre = new int[Constant.charNum];

        for(int i = 0; i < text.length(); i++) {
            fre[text.charAt(i)-'a']++;
        }

        //this.freq = fre;
    }


    /**
     * Vraci procentuelni rozlozeni znaku analyzovaneho textu
     * @param  text analyzovany text
     * @return procentuelni rozlozeni znaku
     */
    public static double[] procFa(String text){
        int[]freq = new int[Constant.charNum];
        double[] pomocna = new double[Constant.charNum];

        if (text != null && !text.trim().isEmpty()) {
            for (int i = 0 ;i < text.length(); i++) {
                freq[text.charAt(i)-'a']++;
            }
            for (int i  =0; i < Constant.charNum; i++) {
                pomocna[i] = 100 * Double.valueOf(freq[i]) / text.length();
            }

        } else {
              for (int i = 0; i < Constant.charNum; i++) {
                  pomocna[i] = 0;
              }
          }

        return (pomocna);
    }

    
    /**
     * Mapuje pismena analyzovaneho textu podle cetnosti na pismena ceske zakladni abecedy.
     * @param text analyzovany text
     */
    public void mapovani(String text) {

        procFa(text);
    
        int[] aklic = new int[Constant.charNum];
        int[] bklic = new int[Constant.charNum];

        for (int i = 0; i < 26; i++) {
            aklic[i]=0;
            bklic[i]=0;

            for (int j = 0; j < i; j++) {
                if (Constant.cj[j] <= Constant.cj[i]) {
                    aklic[i]++;
                }

                if (procFa(text)[j] <= procFa(text)[i]) {
                    bklic[i]++;
                }
            }

            for (int j = i + 1; j < 26; j++) {
                if (Constant.cj[j] < Constant.cj[i]) {
                    aklic[i]++;
                }

                if (procFa(text)[j] < procFa(text)[i]) {
                    bklic[i]++;
                }
            }
        }

        String[] abcdFA = new String[Constant.charNum];
        String[] posun = new String[Constant.charNum];

        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < 26; j++) {
                if (aklic[i] == bklic[j]) {
                    abcdFA[i] = (char)(j+97) + " ";
                    int rozdil = abcdFA[i].charAt(0) - Constant.abcd[i];

                    if (rozdil < 0) {
                        rozdil+=26;
                    }

                    posun[i] = Integer.toString(rozdil);

                }
            }
        }
    }

    /**
     * Plni tabulku daty.
     * @param s
     * @return
     */
    public static double[][] naplnprvnitabulku(String s){
        double[][] pole = new double[2][26];

        pole[0]=Constant.cj;
        pole[1]=procFa(s);
        return(pole);
    }

    /**
     * Plni tabulku daty.
     * @param s
     * @param jeempty
     * @return
     */
    public static String[][] naplndruhoutabulku(String s, Boolean jeempty){

        String[][] pole = new String[2][26];
        double[] stdata = procFa(s);

        if (!jeempty) {
        int[] aklic = new int[26];
        int[] bklic = new int[26];

        for (int i = 0; i < 26; i++) {

            aklic[i]=0;
            bklic[i]=0;

            for (int j = 0; j < i; j++) {

                if(Constant.cj[j]<=Constant.cj[i]) {
                    aklic[i]++;
                }

                if(stdata[j] <= stdata[i]) {
                  bklic[i]++;
                }

            }

            for (int j = i+1; j < 26; j++) {

                if(Constant.cj[j]<Constant.cj[i]) {
                    aklic[i]++;
                }

                if(stdata[j]<stdata[i]) {
                    bklic[i]++;
                }

            }

        }


        String[] abcdFA = new String[26];
        String[] posun = new String[26];

        for (int i=0;i<26;i++) {

            for (int j=0;j<26;j++) {

                if (aklic[i]==bklic[j]) {

                    abcdFA[i] = Character.toString((char)(j+97));
                    //abcdFA[i] = (char)(j+97) + " ";
                    int rozdil = abcdFA[i].charAt(0)-Constant.abcd[i];

                    if (rozdil<0) {
                        rozdil+=26;
                    }

                    posun[i] = Integer.toString(rozdil);

                }
            }

            pole[0] = abcdFA;
            pole[1] = posun;

        }
        }
        else {
            for (int j=0;j<26;j++) {
                pole[0][j] = Character.toString(Constant.abcd[j]);
                pole[1][j] = "0";
            }
        }

        return(pole);
    }

}
