/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.plot.ui.proppanels;

import java.awt.GridBagLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;

import com.approximatrix.charting.model.MultiScatterDataModel;
import com.approximatrix.charting.model.ObjectChartDataModel;
import com.approximatrix.charting.render.AbstractChartRenderer;
import com.approximatrix.charting.swing.BufferedChartPanel;
import com.approximatrix.charting.render.RowColorModel;
import com.approximatrix.draco.io.XMLio.PlotTags;
import com.approximatrix.draco.plot.ui.images.ColorButtonIcon;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import java.awt.Dimension;
import java.util.Iterator;
import java.util.Map;

public class BarPropPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
	private BufferedChartPanel panel = null;
	private ObjectChartDataModel model = null;

	private JPanel jPanel1 = null;
	private JComboBox jComboBox = null;
	private JPanel jPanel2 = null;
	private JLabel jLabel1 = null;
	private JButton jButton = null;
	private ColorButtonIcon cbi = null;  //  @jve:decl-index=0:
	private JColorChooser chooserPane = null; 
	
	/**
	 * This is the default constructor
	 */
	public BarPropPanel(BufferedChartPanel incoming_panel) {
		super();
		
		if(incoming_panel == null) {
			System.err.println("BarPropPanel: Panel is null!");
		}
		
		panel = incoming_panel;
		model = (ObjectChartDataModel)incoming_panel.getChartDataModel();
		
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);
		this.setLayout(new BorderLayout());
		this.add(getJPanel1(), BorderLayout.NORTH);
		this.add(getJPanel2(), BorderLayout.CENTER);
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(new FlowLayout());
			JLabel jLabel = new JLabel();
			jLabel.setText("Available Series: ");
			jPanel1.add(jLabel, null);
			//jPanel1.add(getAvail_series_combo(), null);
			jPanel1.add(getJComboBox(), null);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setPreferredSize(new java.awt.Dimension(180,21));
			jComboBox.addItemListener(new java.awt.event.ItemListener() { 
				public void itemStateChanged(java.awt.event.ItemEvent e) {    
					update_fields_from_combo();
					panel.revalidate();
				}
			});
			for(int i=0;i<model.getDataSetNumber();i++) {
				jComboBox.addItem((Object)model.getDataSetName(i));
			}
			jComboBox.setSelectedIndex(0);
		}
		return jComboBox;
	}

	private void update_fields_from_combo() {
		// Based on name, get plot properties
		String selected_name = getJComboBox().getSelectedItem().toString();
		int series_index = getSeriesNumberFromCombo();
		
		RowColorModel color_model = getRowColorModel();
		if(color_model != null) {
			if(series_index < color_model.getRowCount() && series_index >=0)
				getSeriesIcon().setColor(color_model.getColor(series_index));
		}
		getJButton().revalidate();
	}
        
	private int getSeriesNumberFromCombo() {
		String selected_name = getJComboBox().getSelectedItem().toString();
		int series_index = -1;
		for(series_index = 0;series_index<model.getDataSetNumber();series_index++) {
			if(model.getDataSetName(series_index).compareTo(selected_name)==0) break;
		}
		if(series_index >= model.getDataSetNumber()) return -1;
		return series_index;
	}
	
    private RowColorModel getRowColorModel() {
    	Map renderers = panel.getChartRenderer();
        for(Iterator<AbstractChartRenderer>iter = renderers.values().iterator();iter.hasNext();) {
        	AbstractChartRenderer now = iter.next();
        	if(now != null) {
        		return now.getRowColorModel();
        	}
        }
        return null;
    }

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.gridy = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jLabel1 = new JLabel();
			jLabel1.setText("Bar Color: ");
			jPanel2 = new JPanel();
			jPanel2.setLayout(new GridBagLayout());
			jPanel2.add(jLabel1, gridBagConstraints);
			jPanel2.add(getJButton(), gridBagConstraints1);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setPreferredSize(new Dimension(45, 25));
			jButton.setIcon(getSeriesIcon());
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Color newColor = JColorChooser.showDialog(BarPropPanel.this, "Choose color for series...",cbi.getColor());
					if(newColor != null) {
						RowColorModel cmodel = getRowColorModel();
	             		int series_index = getSeriesNumberFromCombo();
	             		if(series_index < cmodel.getRowCount() && series_index >=0) {
	             			cbi.setColor(newColor);
	             			cmodel.setColor(series_index,newColor);
	             		}
	             		
	             		jButton.repaint();
	             		repaintPanel();
					}
					
				}
			});
		}
		return jButton;
	}
	
	private ColorButtonIcon getSeriesIcon() {
		if(cbi == null) {
			cbi = new ColorButtonIcon();
		}
		return cbi;
	}
	
	private void repaintPanel() {
	    Map<Integer,AbstractChartRenderer> renderers = panel.getChartRenderer();
	    for(Integer key : renderers.keySet())
	        renderers.get(key).fireRenderChangeEvent();
	}
	
}
