package com.publicuhc.translate;

import com.google.inject.Inject;
import com.publicuhc.configuration.Config;
import com.publicuhc.translate.exceptions.LocaleNotFoundError;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;

import java.util.HashMap;
import java.util.Map;

public class DefaultTranslate implements Translate {

    private final Config m_config;

    @Inject
    protected DefaultTranslate(Config config){
        m_config = config;
    }

    @Override
    public String translate(String key, String locale) {
        return translate(key, locale, new HashMap<String, String>());
    }

    @Override
    public String translate(String key, String locale, Map<String, String> vars) {
        FileConfiguration configuration = m_config.getConfig("translations/"+locale);

        String value = configuration.getString(key, "TRANSLATION KEY NOT SET FOR "+key);

        if(null == value){
            throw new LocaleNotFoundError();
        }
        for(String s : vars.keySet()){
            value = value.replaceAll("%"+s+"%", vars.get(s));
        }
        return value;
    }

    @Override
    public String translate(String key, String locale, String var, String value) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put(var, value);
        return translate(key, locale, map);
    }

    @Override
    public String getLocaleForSender(CommandSender sender) {
        //TODO find the saved locale for the user
        //TODO return the default locale from the config file
        return null;
    }
}

@@ -840,171 +840,180 @@
     
-String value = configuration.getString(key, %22TRANSLATION KEY NOT SET FOR %22+key);%0A%0A        if(null == value)%7B%0A            throw new LocaleNotFoundError();%0A        %7D
+if(null == configuration)%7B%0A            throw new LocaleNotFoundError();%0A        %7D%0A%0A        String value = configuration.getString(key, %22TRANSLATION KEY NOT SET FOR %22+key);%0A
 %0A   

check the right thing for null locale