/*
 * ECatalog is a database front-end, with two main features:
 * 1. Use of preferences
 *  A preference-based approach, where the user is allowed to define the importance of each criterion.
 *  Then the items are ranked accordingly to his criteria.
 * 2. Trade-off analysis
 *  A cooperative database approach, where the system "argues" with the user about his criteria.
 *  When there are no matching items, the system explains the minimal conflicting set and
 *  give some possible strong and weak relaxations about his criteria.
 * This package also containts the software and the set-up details used for our User Study,
 * comparing the use or not of the two previous features mentioned above.
 *
 * Copyright (C) 2006 David Portabella Clotet, Artificial Intelligence Laboratory, EPFL
 * 
 * This file is part of ecatalog-1.0.zip
 * 
 * ECatalog is free software and a free user study set-up;
 * 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 2 of the License, or
 * (at your option) any later version.
 * 
 * ECatalog 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 ECatalog; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * @version 1.0
 * @author David Portabella
 * To contact the author:
 * email: david@portabella.name and david.portabella@epfl.ch
 * 
 * More information about ECatalog:
 *  http://sourceforge.net/projects/ecatalog/
 *  http://icwww.epfl.ch/~portabel/ecatalogs/
 */

package ecatalog.gui.criteriaSelection;

import ecatalog.gui.MainGui;
import ecatalog.ECatalog;
import ecatalog.db.Weight;

import utils.ButtonGroupPanel;
import utils.ButtonGroupPanelListener;
import utils.IconToggleButton;

import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstraints;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Vector;


class WeightSelector {
    final String unselectedIconName = "images/clear_star_blue_outline.gif";
    final String selectedIconName = "images/yellow_star_blue_outline.png";
    final String pressedIconName = "images/yellow_star_lite.gif";
    final String disabledIconName = "images/clear_star_blue_outline.gif";  //TODO: get another distinct one

    final static int numberOfWeights = 5;
    JToggleButton[] buttons;

    JPanel panel;
    //ButtonGroupPanel buttonGroupPanel;
    ConstraintGui constraintGui;
    ECatalog ecatalog;

    boolean systemChangingFields = false;

    WeightSelector(ECatalog ecatalog) {
	this.ecatalog = ecatalog;

	panel = new JPanel();
	TableLayout layout = new TableLayout(new double[][] {{}, {TableLayout.PREFERRED}});
	panel.setLayout(layout);

	buttons = new JToggleButton[numberOfWeights];
	for (int i = 0; i < numberOfWeights; i++) {
		buttons[i] = new IconToggleButton(
			new ImageIcon(unselectedIconName),
			new ImageIcon(pressedIconName),
			new ImageIcon(selectedIconName),
			new ImageIcon(disabledIconName)) {
		    public JToolTip createToolTip() { return MainGui.createToolTip(); }
		    public String getToolTipText(MouseEvent event) { return WeightSelector.this.getToolTipText(event); }
		};
		buttons[i].setToolTipText("...");
		final int index = i;
		buttons[i].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!systemChangingFields) weightSelected(index); }});
		layout.insertColumn(i, TableLayout.PREFERRED);
		panel.add(buttons[i], new TableLayoutConstraints(i, 0));
	}
	//buttonGroupPanel = new ButtonGroupPanel(buttons);
	//buttonGroupPanel.addButtonGroupPanelListener(new ButtonGroupPanelListener() { public void selected(int index) { if (!systemChangingFields) weightSelected(index); }});
    }

    Component getComponent() { return panel; }

    void setConstraintGui(ConstraintGui constraintGui) { this.constraintGui = constraintGui; }

    void weightSelected(int weightNum) {
	Weight weight = new Weight( (weightNum+1.0) / numberOfWeights);
	if (ecatalog.getLog() != null)
	    ecatalog.getLog().changeConstraintWeigth(constraintGui, weight);
	
	constraintGui.getConstraint().setWeight(weight);
	ecatalog.weightChanged();
	updateButtons();
    }

    void update() {
	updateButtons();
    }

    void updateButtons() {
	systemChangingFields = true;
	int index;

	if (constraintGui == null || constraintGui.getConstraint() == null) {
	    index = 0;
	} else {
	    Weight weight = constraintGui.getConstraint().getWeight();
	    index = getWeightIndex(weight);
	}
	
	for (int i = 0; i <= index; i++)
	    buttons[i].setSelected(true);
	for (int i = index+1; i < numberOfWeights; i++)
	    buttons[i].setSelected(false);
	
	systemChangingFields = false;
    }

    void setEnabled(boolean enabled) {
	panel.setEnabled(enabled);
	//TODO: ...
    }

    public String getToolTipText(MouseEvent event) { 
	return null;

	/*
	String text;
	if (constraintGui.getConstraint().areDetailsDefined()) {
	    text = "You can set the importance of this criteria, to \"prefer\", \"important\" or \"very important\".\n" +
		"Now it is defined as \"" + constraintGui.getConstraint().getWeight().toString() + "\".";
	} else {
	    text = "To define the importance of this criteria, first you need to define the criteria.\n" +
		"For instance, you can select the example value (the green value) by clicking on it.";
	}
	return text;
	*/
    }

    int getWeightIndex(Weight weight) {
	if (weight.getWeightValue() == -1)
	    throw new Error("MUST weight not expected");
	int index = (int) (weight.getWeightValue() * 5 - 1);
	if (index < 0 || index >= numberOfWeights)
	    throw new Error("weight value not expected: " + weight.getWeightValue());
	return index;
    }
}


