package fanch.multijeu.common.utils;

import java.text.Normalizer;

public class LetterUtils {
    public static final int     LETTER_ASCII_TOTAL=8443;
    public static final int     LETTER_ASCII_V=2876;
    public static final int     LETTER_ASCII_C=LETTER_ASCII_TOTAL-LETTER_ASCII_V;
    public static final int[][] LETTER_ASCII_FREQ = {
            {'a', 840, 1},//a
            {'b',106, 0}, //b
            {'c',303, 0}, //c
            {'d',418, 0}, //d
            {'e',172, 1}, //e
            {'f',112, 0}, //f
            {'g',127, 0}, //g
            {'h', 92, 0}, //h
            {'i',734, 1}, //i
            {'j',31, 0}, //j
            {'k',5, 0}, //k
            {'l',601, 0}, //l
            {'m',296, 0}, //m
            {'n',713,  0},//n
            {'o',526, 1}, //o
            {'p',301, 0}, //p
            {'q',99,  0},//q
            {'r',655, 0},//r
            {'s',808, 0}, //s
            {'t',707, 0}, //t
            {'u',574, 1}, //u
            {'v',132, 0}, //v
            {'w',4, 0}, //w
            {'x',45, 0}, //x
            {'y',30, 1}, //y
            {'z',12, 0} //z
    };


    public static final int     LETTER_FREQ_TOTAL=7726;
    public static final int     LETTER_FREQ_V=2725;
    public static final int     LETTER_FREQ_C=LETTER_FREQ_TOTAL-LETTER_FREQ_V;
    public static final int[][] LETTER_FREQ = {
            {'e' ,121,1},
            {'a' ,711,1},
            {'i' ,659,1},
            {'s' ,651,0},
            {'n' ,639,0},
            {'r' ,607,0},
            {'t' ,592,0},
            {'o' ,502,1},
            {'l' ,496,0},
            {'u' ,449,1},
            {'d' ,367,0},
            {'c' ,318,0},
            {'m' ,262,0},
            {'p' ,249,0},
            {'é' ,194,1},
            {'g' ,123,0},
            {'b' ,114,0},
            {'v' ,111,0},
            {'h' ,111,0},
            {'f' ,111,0},
            {'q',65,0},
            {'y',46,0},
            {'x',38,0},
            {'j',34,0},
            {'è',31,1},
            {'à',31,1},
            {'k',29,0},
            {'w',17,0},
            {'z',15,0},
            {'ê',8,1},
            {'ç',6,0},
            {'ô',4,1},
            {'â',3,1},
            {'î',3,1},
            {'û',2,1},
            {'ù',2,1},
            {'ï',1,1},
            {'ü',1,1},
            {'ë',1,1},
            {'ö',1,1},
            {'í',1,1}
    };



    public static char randLetter(boolean accent)
    {
        int total=(accent)?LETTER_FREQ_TOTAL:LETTER_ASCII_TOTAL;
        int arr[][] = (accent)?LETTER_ASCII_FREQ:LETTER_ASCII_FREQ;
        int rand = IntUtils.rand(0,total);
        for(int i=0; i<arr.length; i++)
        {
            rand-=arr[i][1];
            if(rand<0) return (char)arr[i][0];
        }
        return (char)arr[arr.length-1][0];
    }

    public static char randVoyelle(boolean accent)
    {
        int total=(accent)?LETTER_FREQ_V:LETTER_FREQ_V;
        int arr[][] = (accent)?LETTER_ASCII_FREQ:LETTER_ASCII_FREQ;
        int rand = IntUtils.rand(0,total);
        for(int i=0; i<arr.length; i++)
        {
            if(arr[i][0]==1) {
                rand -= arr[i][1];
                if (rand < 0) return (char) arr[i][0];
            }
        }
        return (char)'a';
    }

    public static char randConsonne(boolean accent)
    {
        int total=(accent)?LETTER_FREQ_C:LETTER_FREQ_C;
        int arr[][] = (accent)?LETTER_ASCII_FREQ:LETTER_ASCII_FREQ;
        int rand = IntUtils.rand(0,total);
        for(int i=0; i<arr.length; i++)
        {
            if(arr[i][0]==0) {
                rand -= arr[i][1];
                if (rand < 0) return (char) arr[i][0];
            }
        }
        return (char)'z';
    }


    public static boolean isConsonne(char cc)
    {
        char c = Character.toLowerCase(cc);
        for(int i=0; i<LETTER_FREQ.length; i++) {
            if(LETTER_FREQ[i][0]==c)
                return LETTER_FREQ[i][2]==0;
        }
        return false;
    }

    public static boolean isVoyelle(char cc)
    {
        char c = Character.toLowerCase(cc);
        for(int i=0; i<LETTER_FREQ.length; i++) {
            if(LETTER_FREQ[i][0]==c)
                return LETTER_FREQ[i][2]==1;
        }
        return false;
    }

    public static String toAscii(char c)
    {
        return toAscii(""+c);
    }

    public static String toAscii(String c)
    {
        String s = Normalizer.normalize(c, Normalizer.Form.NFD);
        return s.replaceAll("[^\\p{ASCII}]", "");
    }


}
