/**
 * 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.util.ArrayList;

import javax.swing.Icon;
import javax.swing.JComponent;

import org.jivesoftware.spark.preference.Preference;

import static org.jivesoftware.spellchecker.SpellcheckerRes.ICON_SPELLING;

public class SpellcheckerPreference implements Preference {
    public static String NAMESPACE = "spellchecking";
    private SpellcheckerPreferenceDialog dialog;
    private final SpellcheckerPreferences preferences = new SpellcheckerPreferences();
    private final ArrayList<String> languages;

    /**
     * Initializes the SpellcheckerPreference
     *
     * @param languages an {@link ArrayList} of {@link String} containing the supported languages
     */
    public SpellcheckerPreference(final ArrayList<String> languages) {
        this.languages = languages;
    }

    /**
     * Returns the Preferences
     *
     * @return {@link SpellcheckerPreference}
     */
    public SpellcheckerPreferences getPreferences() {
        return preferences;
    }

    @Override
    public void commit() {
        preferences.setAutoSpellCheckerEnabled(dialog.isAutoSpellCheckingEnabled());
        preferences.setSpellCheckerEnabled(dialog.isSpellCheckingEnabled());
        preferences.setSpellLanguage(dialog.getSelectedLanguage());
        preferences.setIgnoreUppercase(dialog.getIgnoreUppercase());
        preferences.setLanguageSelectionInChatRoom(dialog.getEnableLanguageSelection());
        SpellcheckManager.getInstance().loadDictionary(dialog.getSelectedLanguage());
        preferences.save();
    }

    @Override
    public Object getData() {
        return preferences;
    }

    @Override
    public String getErrorMessage() {
        return null;
    }

    @Override
    public JComponent getGUI() {
        dialog = new SpellcheckerPreferenceDialog(languages);
        dialog.setAutoSpellCheckingEnabled(preferences.isAutoSpellCheckerEnabled());
        dialog.setSelectedLanguage(preferences.getSpellLanguage());
        dialog.setSpellCheckingEnabled(preferences.isSpellCheckerEnabled());
        dialog.setEnableLanguageSelection(preferences.getLanguageSelectionInChatRoom());
        dialog.setIgnoreUppercase(preferences.getIgnoreUppercase());
        return dialog;
    }

    @Override
    public Icon getIcon() {
        return ICON_SPELLING;
    }

    @Override
    public String getListName() {
        return SpellcheckerRes.getString("title.spellchecker");
    }

    @Override
    public String getNamespace() {
        return NAMESPACE;
    }

    @Override
    public String getTitle() {
        return SpellcheckerRes.getString("title.spellchecker");
    }

    @Override
    public String getTooltip() {
        return SpellcheckerRes.getString("title.spellchecker");
    }

    @Override
    public boolean isDataValid() {
        return true;
    }

    @Override
    public void load() {
    }
}
