package com.epimorphics.registry.language;

import com.epimorphics.appbase.core.App;
import com.epimorphics.appbase.core.Startup;
import com.epimorphics.registry.language.message.FileMessageManager;
import com.epimorphics.registry.language.message.MessageManager;
import com.epimorphics.registry.language.message.Messages;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class MultiLanguageManager implements LanguageManager, Startup {
    private final Logger log = LoggerFactory.getLogger(MultiLanguageManager.class);
    private MessageManager msgManager;

    private String defaultLang = "en";
    private Boolean useCookies = false;
    private LanguageConfig config;
    private Messages defaultMsgs;

    public MultiLanguageManager() {
        this.msgManager = new FileMessageManager();
    }

    MultiLanguageManager(MessageManager msgManager) {
        this.msgManager = msgManager;
    }

    public void setUseCookies(Boolean useCookies) {
        this.useCookies = useCookies;
    }

    @Override public Boolean getUseCookies() {
        return useCookies;
    }

    public void setDefaultLanguage(String defaultLang) {
        this.defaultLang = defaultLang;
    }

    @Override public String getDefaultLanguage() {
        return defaultLang;
    }

    public void setConfig(LanguageConfig config) {
        this.config = config;
    }

    public Boolean isMultilingual() {
        return getLanguages().size() > 1;
    }

    public List<Language> getLanguages() {
        return config.getLanguages();
    }

    @Override public void startup(App app) {
        Messages defaultMsgs = msgManager.getMessages(defaultLang);
        if (defaultMsgs == null) {
            log.error("Messages for default language (" + defaultLang + ") are not configured.");
            defaultMsgs = new Messages.Empty();
        }

        this.defaultMsgs = defaultMsgs;
    }

    @Override public Messages getMessages(String lang) {
        Messages msgs = msgManager.getMessages(lang);
        if (msgs == null) {
            return defaultMsgs;
        } else {
            return new Messages.WithDefault(msgs, defaultMsgs);
        }
    }
}

@@ -1973,24 +1973,117 @@
 ing lang) %7B%0A
+        if (lang.equals(defaultLang)) %7B%0A            return defaultMsgs;%0A        %7D else %7B%0A    
         Mess
@@ -2128,24 +2128,28 @@
 g);%0A        
+    
 if (msgs == 
@@ -2148,32 +2148,36 @@
 msgs == null) %7B%0A
+    
             retu
@@ -2192,32 +2192,36 @@
 ltMsgs;%0A        
+    
 %7D else %7B%0A       
@@ -2221,24 +2221,28 @@
             
+   
+ 
 return new M
@@ -2277,24 +2277,38 @@
 faultMsgs);%0A
+            %7D%0A
         %7D%0A  

FIX redundant default messages