package eu.dnetlib.efg1914.various.locales;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Created by argirok on 18/7/2014.
 */
public class MyLocale {
    private String locale;
    private ResourceBundle bundle;

    public MyLocale(LOCALE locale) throws IOException {
        this(locale.name());
    }

    public MyLocale(String locale) throws IOException {
        this.locale = locale;
        bundle = ResourceBundle.getBundle("eu.dnetlib.efg1914.various.locales.locale_" + locale);
    }

    public MyLocale() throws IOException {

        this(LOCALE.en_US.name());
    }

    public static void main(String[] args) throws FileNotFoundException, IOException {
        MyLocale locale1 = new MyLocale(LOCALE.en_US);
        System.out.println(locale1.getValue("mainPage.tooltip") + "      ");
        Locale l = new Locale("el", "GR", "Ελληνικά");
        System.out.println(l.getISO3Language() + l.getDisplayCountry() + l.getDisplayLanguage() + l.getDisplayName() + l.getDisplayVariant());
    }

    public String getValue(String key) {
        if (key == null) {
            return "";
        }
        if (bundle != null) {
            try {

                String val = bundle.getString(key);
                return new String(val.getBytes("ISO-8859-1"), "UTF-8");
            } catch (Exception e) {
                return key;
            }
        }
        return "";
    }

    public String getLanguage() {
        return getValue("language.text");
//        if (locale.equals("el_GR")) {
//            String s = "English";
//            try {
//                return new String(s.getBytes(), "UTF-8");
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//                return "";
//            }
//        } else if (locale.equals("en_US")) {
//            String s = "Ελληνικά";
//            try {
//                return new String(s.getBytes(), "UTF-8");
//            } catch (UnsupportedEncodingException e) {
//                e.printStackTrace();
//                return "";
//            }
//        }
//        return "";
    }

    public String getSecondLanguage(String locale1, String locale2) {
        String l;
        if (locale.equals(locale1)) {
            l = locale2;
        } else {
            l = locale1;
        }
        return l;
    }
//    public String getSecondLocale(String locale1,String locale2) {
//        String l;
//        if(locale.equals(locale1)){
//           return locale2;
//        }else{
//            return locale1;
//        }
//
//    }
//    private String selectLanguage(String locale){
////        if(locale.equals("en_US")){
////            return "English";
////        }else if(locale.equals("el_GR")){
////            return "Ελληνικά";
////        }else if(locale.equals("es_ESP")){
////            return "Spanish";
////        }else if(locale.equals("de_DEU")){
////            return "Deutch";
////        }else{
////            return "Unknown";
////        }
//    MyLocale l= new MyLocale(locale);
//
//
//    }

    public String getLocale() {
        return locale;
    }

    public String getTooltip() {
        return getValue("language.tooltip");
    }

    public ResourceBundle getBundle() {
        return bundle;
    }

    public enum LOCALE {
        en_US, el_GR, es_ESP, de_DEU;
    }
}

