/**
 * 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.ui.themes;


import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.Collections;
import java.util.Set;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;

import org.jivesoftware.resource.Res;
import org.jivesoftware.spark.component.tabbedPane.SparkTabbedPane;
import org.jivesoftware.spark.util.ColorPick;

/**
 * Handles the Color Preferences in Spark
 * 
 * @author Wolf Posdorfer
 * 
 */
public class ColorPreferencePanel extends SparkTabbedPane {
	private JList<String> colorList;
    private ColorSettings _colorsettings;

    private JLabel _errorlabel;
    
    private ColorPick _colorpick;

    public ColorPreferencePanel() {

	createUI();
    }

    private void createUI() {

	_colorsettings = ColorSettingManager.getColorSettings();
	Set<String> sets  = _colorsettings.getKeys();


	Vector<String> keys = new Vector<>(sets);
	sortList(keys);
	
	JPanel rightpanel = new JPanel(new GridBagLayout());
	
	_colorpick = new ColorPick(true);
	
	final JButton savebutton = new JButton(Res.getString("apply"));
	
	final JButton restoreDefaults = new JButton(Res.getString("use.default"));
	
	_errorlabel = new JLabel(" ");
	_errorlabel.setForeground(Color.red);
	
	
	rightpanel.add(_colorpick, new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0,GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));

	
	rightpanel.add(savebutton, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0 , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
	rightpanel.add(restoreDefaults, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0 , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));

	rightpanel.add(_errorlabel, new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0 , GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));

	colorList = new JList<>(keys);
		JScrollPane _jScrollPane = new JScrollPane(colorList);
	
	setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
	add( _jScrollPane );
	add(rightpanel);
	
	
	colorList.addListSelectionListener(e -> {

    String v = colorList.getSelectedValue();

    Color c = _colorsettings.getColorFromProperty(v);

    _colorpick.setColor(c);
    _errorlabel.setText("");
    } );
	
	savebutton.addActionListener(this::savebuttonaction);
	
	restoreDefaults.addActionListener( e -> {
    ColorSettingManager.restoreDefault();
    _colorsettings = ColorSettingManager.getColorSettings();
    } );

    }
    
    /**
     * Called when the Save-Button is pressed
     * @param e
     */
    private void savebuttonaction(ActionEvent e) {
	try {

	    Color c = _colorpick.getColor();

	    _colorsettings.setColorForProperty(
            colorList.getSelectedValue(), c);

	    UIManager.put( colorList.getSelectedValue(), c);

	    _errorlabel.setText(Res.getString("lookandfeel.color.saved"));

	} catch (Exception ex) {
	    _errorlabel.setText(Res.getString("title.error"));
	    _errorlabel.revalidate();
	}
    }
    
    /**
     * Sorts the provided List
     * @param set
     */
    private void sortList(Vector<String> set)
    {
	Collections.sort(set);
    }
    
}
