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

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Objects;

import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jivesoftware.resource.Res;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.TitlePanel;
import org.jivesoftware.spark.component.renderer.JLabelIconRenderer;
import org.jivesoftware.spark.preference.Preference;
import org.jivesoftware.spark.preference.PreferenceManager;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;

import static java.util.Objects.requireNonNull;

public class PreferencesPanel extends JPanel implements ListSelectionListener {
    /**
     * flowpanel is the right panel, where the plugin specific UI is displayed
     */
    private final JPanel flowPanel = new JPanel(new BorderLayout());
    private final DefaultListModel<PreferenceUI> listModel = new DefaultListModel<>();
    private final JList<PreferenceUI> list = new JList<>(listModel);
    private Preference currentPreference;
    
    /**
     * <h1>Constructor - PreferencesPanel</h1>
     * This is an option to select the transmitted preference by code
     *
     * @param preferences the preference list
     * @param displayPref the preference you want to select
     */
    public PreferencesPanel (Iterable<Preference> preferences, Preference displayPref){
        this(requireNonNull(preferences));
        requireNonNull(displayPref);
            // iterate through all preference-ui items
            for (int i = 0; i < listModel.size(); i++){
                PreferenceUI p = listModel.get( i );
                // check if the namespace is the namespace we search for
                if ( Objects.equals( p.getPreference().getNamespace(), displayPref.getNamespace() ) ){
                    // if we've got our target, we can select this item and stop the search
                    list.setSelectedIndex( i );
                    break; 
                }
            }
            // if we got a valid target, we trigger the selection changed method
            if (list.getSelectedIndex() > -1) selectionChanged();
    }
    
    public PreferencesPanel(Iterable<Preference> preferences) {
        this.setLayout(new GridBagLayout());

        JLabel titleLabel = new JLabel();
        titleLabel.setText(Res.getString("title.preferences"));
        titleLabel.setFont(new Font("Dialog", Font.BOLD, 15));
        // scrollPane is the left panel displaying the preference icons
        JScrollPane scrollPane = new JScrollPane( list );
        scrollPane.setPreferredSize(new Dimension(125, 0));
        scrollPane.setMinimumSize(new Dimension(125,100));
        list.setFixedCellHeight(40);

        add( scrollPane, new GridBagConstraints(0, 0, 1, 1, 0.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(5, 5, 5, 5), 50, 0));
        add(flowPanel, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));

        list.setCellRenderer(new JLabelIconRenderer());
        list.addListSelectionListener(this);
        // Populate with current preferences
        for (Preference preference : preferences) {
            listModel.addElement(new PreferenceUI(preference));
        }

        list.setSelectedIndex(0);
    }

    private synchronized void selectionChanged(){
        PreferenceUI o = list.getSelectedValue();
        Preference pref = o.getPreference();
        PreferenceManager prefManager = SparkManager.getPreferenceManager();
        prefManager.getPreferenceData(pref.getNamespace());

        JComponent comp = pref.getGUI();
        flowPanel.removeAll();

        // Create the title panel for this dialog
        String title = "";
        String toolTip = "";
        Icon icon = null;
        try {
            title = pref.getTitle();
            toolTip = pref.getTooltip();
            icon = pref.getIcon();
        } catch (Exception e) {
            Log.error(e);
        }
        TitlePanel titlePanel = new TitlePanel(title, toolTip, icon, false);


        flowPanel.add(comp, BorderLayout.CENTER);
        flowPanel.add(titlePanel, BorderLayout.NORTH);
        flowPanel.invalidate();
        flowPanel.validate();
        flowPanel.repaint();
        currentPreference = pref;
    }

    @Override
	public void valueChanged(ListSelectionEvent e) {
        if (e.getValueIsAdjusting()) {
            return;
        }
            if (currentPreference != null) {
                try {
                    if (currentPreference.isDataValid()) {
                        Log.debug("Saving preference " + currentPreference.getNamespace());
                        currentPreference.commit();
                    }
                    else {
                        JOptionPane.showMessageDialog(this, currentPreference.getErrorMessage(),
                                Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                        list.removeListSelectionListener(this);
                        list.setSelectedIndex(e.getLastIndex());
                        list.addListSelectionListener(this);
                    }
                }
                catch ( Throwable t )
                {
                    // This is observed with some plugins, that require classes not (no longer) provided by Spark.
                    Log.error( "An error occurred while trying to commit settings for preference: " + currentPreference.getListName(), t );
                    JOptionPane.showMessageDialog( this, "Unable to save all settings for " + currentPreference.getListName() + ":\n" + t.getMessage(),
                                                   Res.getString( "title.error" ), JOptionPane.ERROR_MESSAGE );
                    list.removeListSelectionListener( this );
                    list.setSelectedIndex( e.getLastIndex() );
                    list.addListSelectionListener( this );
                }
            }
            selectionChanged();
    }

    public boolean closing() {
        if (currentPreference != null) {
            if (currentPreference.isDataValid()) {
                Log.debug("Saving preference " + currentPreference.getNamespace());
                currentPreference.commit();
                SettingsManager.fireListeners();
                return true;
            }
            else {
                JOptionPane.showMessageDialog(this, currentPreference.getErrorMessage(),
                        Res.getString("title.error"), JOptionPane.ERROR_MESSAGE);
                return false;
            }
        }
        return true;
    }
}
