package com.anysoftkeyboard.dictionaries;

import android.util.Log;

import com.anysoftkeyboard.AnyKeyboardContextProvider;
import com.anysoftkeyboard.AnySoftKeyboard;
import com.menny.android.anysoftkeyboard.AnyApplication;

public class DictionaryFactory
{
    private static final String TAG = "ASK DictFctry";
    
    private static final DictionaryFactory msFactory;
    
    static
    {
    	msFactory = AnyApplication.getDeviceSpecific().createDictionaryFactory();
    }
    
    public static DictionaryFactory getInstance()
    {
    	return msFactory;
    }
    
    private AutoDictionary mAutoDictionary = null;
    private String mUserDictionaryLocale = null;
    private AddableDictionary mUserDictionary = null;

    public DictionaryFactory()
    {
    }
    
    public synchronized AddableDictionary createUserDictionary(AnyKeyboardContextProvider context, String locale)
    {
        if (mUserDictionary != null && equalsString(mUserDictionaryLocale, locale)){
            return mUserDictionary;
        }
        
        mUserDictionary = new SafeUserDictionary(context, locale);
        mUserDictionary.loadDictionary();
        
        mUserDictionaryLocale = locale;
        return mUserDictionary;
    }
    
    public synchronized AddableDictionary createContactsDictionary(AnyKeyboardContextProvider context)
    {
          return null;
    }
    
    public boolean equalsString(String a, String b){
        if(a == null && b == null){
            return true;
        }
        if(a == null || b == null){
            return false;
        }
        return a.equals(b);
    }
    
    
    public synchronized AutoDictionary createAutoDictionary(AnyKeyboardContextProvider context, AnySoftKeyboard ime, String currentAutoDictionaryLocale)
    {
    	if (!AnyApplication.getConfig().useAutoDictionary())
    		return null;
    	
    	if (mAutoDictionary != null && equalsString(mAutoDictionary.getLocale(), currentAutoDictionaryLocale))
    	{
    		return mAutoDictionary;
    	}
    	
    	Log.d(TAG, "Creating AutoDictionary for locale: "+currentAutoDictionaryLocale);
        mAutoDictionary = new AutoDictionary(context, ime, currentAutoDictionaryLocale);
        mAutoDictionary.loadDictionary();
        
        return mAutoDictionary;
    }
}
