/**
 * Copyright (C) 2004-2011 Jive Software. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.spellchecker;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.dts.spell.SpellChecker;
import org.dts.spell.dictionary.openoffice.OpenOfficeSpellDictionary;
import org.dts.spell.dictionary.SpellDictionary;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.util.log.Log;

public class SpellcheckManager {
    private static SpellcheckManager instance = null;
    private SpellChecker checker;
    private ArrayList<String> languages;
    private SpellcheckerPreference preferences;

    public static SpellcheckManager getInstance() {
        if (instance == null) {
            instance = new SpellcheckManager();
        }
        return instance;
    }

    private SpellcheckManager() {
        loadSupportedLanguages();
        try {
            preferences = new SpellcheckerPreference(languages);
            String spellLanguage = preferences.getPreferences().getSpellLanguage();
            String language = spellLanguage == null ? SparkManager.getMainWindow().getLocale().getLanguage() : spellLanguage;
            checker = new SpellChecker(getDictionary(language));
        } catch (Exception e) {
            Log.error(e);
        }
    }

    public void loadDictionary(String language) {
        checker.setDictionary(getDictionary(language));
    }

    public SpellDictionary getDictionary(String language) {
        try {
            InputStream dictionary = getClass().getClassLoader().getResourceAsStream("dictionary/" + language + ".zip");
            if (dictionary == null) {
                Log.error("Dictionary not found");
            }
            File personalDictionary = new File(SparkManager.getUserDirectory(), "personalDictionary.dict");
            SpellDictionary dict = new OpenOfficeSpellDictionary(dictionary, personalDictionary);
            return dict;
        } catch (IOException e) {
            Log.error(e);
            return null;
        }
    }

    public SpellcheckerPreference getSpellcheckerPreference() {
        return preferences;
    }

    public List<String> getSupportedLanguages() {
        return languages;
    }

    public SpellChecker getSpellChecker() {
        return checker;
    }

    private void loadSupportedLanguages() {
        languages = new ArrayList<>();
        try {
            String qualifiedClassName = getClass().getName();
            Class<?> qc = Class.forName(qualifiedClassName);
            CodeSource source = qc.getProtectionDomain().getCodeSource();

            File jarFile = new File(source.getLocation().getFile());
            if (jarFile.exists() && jarFile.isFile()) {
                try (ZipFile zipFile = new JarFile(jarFile)) {
                    for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); ) {
                        JarEntry entry = (JarEntry) e.nextElement();
                        if (entry.getName().startsWith("dictionary/") && entry.getName().endsWith(".zip")) {
                            String languageFile = entry.getName().substring(11);
                            String lang = languageFile.substring(0, languageFile.lastIndexOf(".zip"));
                            languages.add(lang);
                        }
                    }
                }
            }
        } catch (Exception e) {
            Log.error(e);
        }
    }
}
