/**
 * 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.spark.preference;

import org.jivesoftware.MainWindowListener;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.plugin.privacy.ui.PrivacyPreferences;
import org.jivesoftware.sparkimpl.preference.PreferenceDialog;
import org.jivesoftware.sparkimpl.preference.PreferencesPanel;
import org.jivesoftware.sparkimpl.preference.chat.ChatPreference;
import org.jivesoftware.sparkimpl.preference.groupchat.GroupChatPreference;
//import org.jivesoftware.sparkimpl.preference.media.MediaPreference;
import org.jivesoftware.sparkimpl.settings.local.LocalPreference;

import javax.swing.*;
import java.util.*;

/**
 * Usage of the PreferenceManager to handle loading of preferences within Spark.
 *
 * @author Derek DeMoro
 */
public class PreferenceManager {
    private final Map<String, Preference> map = new LinkedHashMap<>();
    private PreferenceDialog preferenceDialog;

    public PreferenceManager() {
        // Initialize base preferences
        ChatPreference chatPreferences = new ChatPreference();
        addPreference(chatPreferences);
        chatPreferences.load();
        
        GroupChatPreference groupChatPreferences = new GroupChatPreference();
        addPreference(groupChatPreferences);
        groupChatPreferences.load();
        
//        MediaPreference preferences = new MediaPreference();
//        addPreference(preferences);
//        preferences.load();
        
        PrivacyPreferences privacy = new PrivacyPreferences();
        addPreference(privacy);
        privacy.load();

        LocalPreference localPreferences = new LocalPreference();
        addPreference(localPreferences);
        localPreferences.load();

        getPreferences();

        SparkManager.getMainWindow().addMainWindowListener(new MainWindowListener() {
            @Override
			public void shutdown() {
                fireShutdown();
            }

            @Override
			public void mainWindowActivated() {

            }

            @Override
			public void mainWindowDeactivated() {

            }
        });
    }

    /**
     * <h1>showPreferences</h1>
     * This will open the Preference-Dialog and select the given preference.
     *
     * @param selectedPref the preference you want to select
     */
    public void showPreferences(Preference selectedPref) {
        preferenceDialog = new PreferenceDialog();

        preferenceDialog.invoke(SparkManager.getMainWindow(), new PreferencesPanel(getPreferences(), selectedPref));
    }
    
    public void showPreferences() {
        preferenceDialog = new PreferenceDialog();

        preferenceDialog.invoke(SparkManager.getMainWindow(), new PreferencesPanel(getPreferences()));
    }


    public void addPreference(Preference preference) {
        map.put(preference.getNamespace(), preference);
    }

    public void removePreference(Preference preference) {
        map.remove(preference.getNamespace());
    }

    public Preference getPreference(String namespace) {
        return map.get(namespace);
    }

    public Object getPreferenceData(String namespace) {
        return getPreference(namespace).getData();
    }

    public Iterator<Preference> getPreferences() {
        final List<Preference> returnList = new ArrayList<>();
        for (String namespace : map.keySet()) {
            returnList.add(map.get(namespace));
        }
        return returnList.iterator();

    }

    private void fireShutdown()
    {
        for ( final Preference preference : map.values() )
        {
            try
            {
                preference.shutdown();
            }
            catch ( Exception e )
            {
                Log.error( "An exception occurred while trying to shutdown preference: " + preference, e );
            }
        }
    }

    public JDialog getPreferenceDialog() {
        return preferenceDialog.getDialog();
    }
}
