package roart.lang;

import java.util.Date;
import java.util.ArrayList;

import com.cybozu.labs.langdetect.Detector;
import com.cybozu.labs.langdetect.DetectorFactory;
import com.cybozu.labs.langdetect.Language;
import com.cybozu.labs.langdetect.LangDetectException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import roart.config.ConfigConstants;
import roart.config.MyConfig;
import roart.util.Constants;

import java.io.*;

public class LanguageDetect {

    private static Logger log = LoggerFactory.getLogger(LanguageDetect.class);

    private static boolean inited = false;

    private static String languages = "en";
    
    public static String[] getLanguages() {
    	return languages.split(",");
    }
    
    public static boolean isSupportedLanguage(String language) {
    	String[] langs = languages.split(",");
    	for (String l : langs) {
    		if (l.equals(language)) {
    			return true;
    		}
    	}
    	return false;
    }
    
    public static void init(String profileDirectory) throws LangDetectException {
	DetectorFactory.loadProfile(profileDirectory);
    String mylang = MyConfig.conf.languages;
    if (mylang != null) {
    	languages = mylang;
    }
	inited = true;
    }
    
    public static String detect(String text) {
        if (text == null || text.isEmpty()) {
            return null;
        }
	try {
	    if (!inited) init("./profiles/");
	    Date d = logstart();
	    Detector detector = DetectorFactory.create();
	    detector.append(text);
	    String retstr = detector.detect();
	    logstop(d);
	    log.info("language " + retstr);
	    log.info("language2 " + LanguageDetect.detectLangs(text));
	    return retstr;
	} catch (Exception e) {
	    log.error(Constants.EXCEPTION, e);
	} catch (Error e) {
	    System.gc();
        log.error("Error " + Thread.currentThread().getId() + " " + text.length());
	    log.error(Constants.ERROR, e);
	}
	return null;
    }

    public static ArrayList<Language> detectLangs(String text) throws LangDetectException {
	if (!inited) init("profiles");
	Detector detector = DetectorFactory.create();
	detector.append(text);
	return detector.getProbabilities();
    }

    public static Date logstart() {
        return new Date();
    }
    
    public static void logstop (Date d) {
        Date n = new Date();
        long m = n.getTime() - d.getTime();
        log.info("timerStop " + m);
    }

}

@@ -611,24 +611,26 @@
 tatic String
+%5B%5D
  languages =
@@ -633,13 +633,17 @@
 es =
+ %7B
  %22en%22
+ %7D
 ;%0A  
@@ -706,35 +706,24 @@
 rn languages
-.split(%22,%22)
 ;%0A    %7D%0A    
@@ -784,24 +784,24 @@
 language) %7B%0A
+
     %09String%5B
@@ -823,19 +823,8 @@
 ages
-.split(%22,%22)
 ;%0A  
@@ -1088,16 +1088,18 @@
   String
+%5B%5D
  mylang 

changing language to be array