package com.capgemini.framework.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/** Class which managed the language problematic of string values in reports. */
public final class LanguageHelper {

    private static final int FIRST_GROUP = 1;
    private static final int SECOND_GROUP = 2;
    private static final int THIRD_GROUP = 3;
    private static final int SIZE_2 = 2;
    private static final int SIZE_4 = 4;
    private static final String STR_SEPARATOR = "/";
    private static final String EMPTY_STRING = StringHelper.EMPTY;
    private static final String REGEX_SEPARATOR = "\\s*\\/\\s*"; // A slash (/) character surrounded by any number of spaces (including none)

    private LanguageHelper() {

    }

    /**
     * Extracts left side of a string of the form "French text / English text".<br>
     * Separator slash may have *any* number (0+) of white spaces around them.<br>
     * Warning : The form may be "French text 1 / French text 2 / English text 1 / English text 2". and in this case, the two left sides are extracted.
     * 
     * @param string {@link String} - the string value to check
     * @return the "French side" (if any "side" exists), an empty string otherwise
     */
    public static String french(final String string) {
        if (string == null || string.isEmpty()) {
            return EMPTY_STRING;
        }
        String[] list = string.split(REGEX_SEPARATOR);
        if (list.length > 0) {
            String result;
            if (list.length == SIZE_4) {
                result = list[0].concat(STR_SEPARATOR).concat(list[1]);
            } else {
                result = list[0];
            }
            return result;
        } else {
            return EMPTY_STRING;
        }
    }

    /**
     * Extracts right side of a string of the form "French text / English text".<br>
     * Separator slash may have *any* number (0+) of white spaces around them.<br>
     * Warning : The form may be "French text 1 / French text 2 / English text 1 / English text 2". and in this case, the two right sides are extracted.
     * 
     * @param string {@link String} - the string value to check
     * @return the "English side" (if any "side" exists), an empty string otherwise
     */
    public static String english(final String string) {
        if (string == null || string.isEmpty()) {
            return EMPTY_STRING;
        }
        String[] list = string.split(REGEX_SEPARATOR);
        if (list.length > 0) {
            String result;
            if (list.length == SIZE_2) {
                result = list[FIRST_GROUP];
            } else if (list.length == SIZE_4) {
                result = list[SECOND_GROUP].concat(STR_SEPARATOR).concat(list[THIRD_GROUP]);
            } else {
                result = list[0];
            }
            return result;
        } else {
            return EMPTY_STRING;
        }
    }

    /**
     * Allow to findAll name of a type object without suffix "Revision".
     * 
     * @param nameItemRevision the name
     * @return name of type object.
     */
    public static String getNameTypeWithoutRevision(String nameItemRevision) {
        Pattern pattern = Pattern.compile("(.*)( Revision)");
        Matcher matcher = pattern.matcher(nameItemRevision);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }
}
