/*
 * 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;

import ecatalog.gui.visualization.VisualizationGui;
import ecatalog.gui.criteriaSelection.CriteriaSelectionGui;
import ecatalog.ECatalog;
import ecatalog.db.*;

import utils.JMultiLineToolTip;
import utils.SwingTools;
import info.clearthought.layout.TableLayout;

import ecatalog.jaxb.ecatalogConfig.ECatalogConfigType;
import ecatalog.jaxb.ecatalogConfig.AttributeType;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.SQLException;

import dpc.utils.DebugUtil;
import dpc.utils.TextUtil;


/* The main window. It creates the Criteria Selection window, the Visualiation window and the Analysis window
 */
public class MainGui {
    public void init(ECatalog ecatalog) {
	ECatalog.lowlevelMsg("MainGui.init");
	this.ecatalog = ecatalog;
	this.config = ecatalog.getConfig();
	this.db = ecatalog.getDatabase();

	try {
	    initGui();
	} catch (Exception e) { 
	    ECatalog.error(e); 
	    throw new Error(); //it is already done, but to avoid the java compiling error
	}
    }

    public final static String fontName = "Verdana";
    public final static Font  font = new Font(fontName, Font.PLAIN, 10);  //Dialog, Courier, Arial, Verdana...
    public final static Color colorConstraintGiven = Color.black;
    public final static Color colorOverconstrained = Color.red;
    public final static Color colorInferred = Color.gray;
    public final static Color colorExample = new Color(0.0f, 0.5f, 0.0f);

    /* Background color to show in the visualization window if the item is compatible with the database (and the user constraints) */
    public final static Color colorBgCompatible = Color.white;

    /* Background color to show in the visualization window if the item is not compatible with the database (and the user constraints) */
    public final static Color colorBgIncompatible = new Color(0.95f, 0.95f, 0.95f);


    public final static Color colorBgEvenItems = new Color(1.0f, 1.0f, 0.95f);
    public final static Color colorBgOddItems = new Color(0.95f, 1.0f, 1.0f);

    public final static Color colorBgEvenUnmatchingItems = new Color(1.0f, 0.90f, 0.85f);
    public final static Color colorBgOddUnmatchingItems = new Color(0.95f, 0.90f, 0.90f);

    /*
    public static final Color exampleColor = new Color(0.0f, 0.5f, 0.0f);
    public static final String fontName = "Arial";

    final static Color bgColorNoList = Color.white;
    final static Color bgColorListSelecting = new Color(171, 192, 255);
    final static Color bgColorListNoSelected = new Color(0.9f,0.9f,0.9f);

    final static Color fgColorNoListValueGiven = Color.black;
    final static Color fgColorNoListValueInferred = Color.gray;
    final static Color fgColorNoListValueExample = MainGui.exampleColor;
    final static Color fgColorNoListValueIncompatible = Color.red;

    final static Color fgColorListCompatible = MainGui.exampleColor;
    final static Color fgColorListIncompatible = Color.red;
    //final static Font  fontAttributes = new Font("Dialog", Font.PLAIN, 12);
    */

    JPanel mainPanel;
    public CriteriaSelectionGui criteriaSelectionGui;
    public VisualizationGui visualizationGui;
    public AnalysisGui analysisGui;

    ECatalog ecatalog;
    ECatalogConfigType config;
    Database db;

    JFrame frame;
    JLabel mainMessage;

    JSplitPane splitPane1, splitPane2;

    public JComponent getComponent() {
	return mainPanel;
    }

    public void blockSplitPanes(boolean block) {
	splitPane1.setEnabled(!block);
	splitPane2.setEnabled(!block);
    }

    /*
    public void restart() {
	criteriaSelectionGui.restart();
	visualizationGui.restart();
    }
    */

    public void update() throws SQLException {
	ECatalog.lowlevelMsg("MainGui.update");

	criteriaSelectionGui.update();
	visualizationGui.update();

	removeFocus();
    }

    public void setEnabled(boolean enabled) {
	criteriaSelectionGui.setEnabled(enabled);
	visualizationGui.setEnabled(enabled);
    }

    public void setMainMessage(String text) {
	mainMessage.setText(text);
    }

    public static JToolTip createToolTip() {
	return new JMultiLineToolTip(6.0, 600, 1,1,1,1);
    }

    void initGui() throws java.lang.Exception
    {
	ECatalog.lowlevelMsg("MainGui.initGui");

	//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
	//frame = new JFrame("Application");
	//frame.setFont(font);   //TODO: will all children take this font by default?

	//Specifies the tooltip dismissal delay value to 30 seconds
	ToolTipManager.sharedInstance().setDismissDelay(30000);
	ToolTipManager.sharedInstance().setInitialDelay(0);
	UIManager.put("font", font);
	//TODO minor: maybe with UIManager we can install the default JToolTip class?

	
	mainPanel = new JPanel();
	mainPanel.setBackground(Color.white);
	TableLayout layout = new TableLayout(new double[][] {{TableLayout.FILL},{}});
	mainPanel.setLayout(layout);

	/*
	JToolBar toolBar = createToolBar();
	layout.insertRow(0, TableLayout.PREFERRED);
	mainPanel.add(toolBar, "0, 0");

	layout.insertRow(1, 10);

	mainMessage = new JLabel("You have found...");
	mainMessage.setFont(new Font(fontName, Font.BOLD, 18));
	layout.insertRow(2, TableLayout.PREFERRED);
	mainPanel.add(mainMessage, "0, 2");
	*/

	//TOP PANEL
	JPanel topPanel = new JPanel();
	TableLayout topLayout = new TableLayout(new double[][] {{TableLayout.PREFERRED, 10, TableLayout.FILL},{TableLayout.PREFERRED}});
	topPanel.setLayout(topLayout);

	//JButton restartButton = new JButton("Restart");
	//restartButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { ecatalog.restart(); } });
	//topPanel.add(restartButton, "0, 0");

	mainMessage = new JLabel("You have found...");
	mainMessage.setFont(new Font(fontName, Font.BOLD, 18));
	topPanel.add(mainMessage, "2, 0");

	layout.insertRow(0, TableLayout.PREFERRED);
	mainPanel.add(topPanel, "0, 0");

	//SEPARATION
	layout.insertRow(1, 0);  //TODO: change the other ones below to remove these 2
	layout.insertRow(2, 0);
	layout.insertRow(3, 10);


	//CRITERIA WINDOW
	criteriaSelectionGui = new CriteriaSelectionGui();
	JPanel criteriaSelectionPanel = criteriaSelectionGui.init(this, ecatalog);

	//VISUALIZATION WINDOW
	visualizationGui = new VisualizationGui();
	visualizationGui.init(this, ecatalog);

	splitPane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, criteriaSelectionPanel, visualizationGui.getComponent());

	int width = (config.getCriteriaPanelWidth() != null) ?
	    config.getCriteriaPanelWidth().intValue()
	    : (criteriaSelectionPanel.getPreferredSize().width +
	       criteriaSelectionGui.constraintsScrollPanel.getVerticalScrollBar().getPreferredSize().width + 1);
	splitPane1.setDividerLocation(width);

	//ANALYSIS WINDOW
	analysisGui = new AnalysisGui();
	JPanel analysisPanel = analysisGui.init(this, ecatalog);
	analysisPanel.setPreferredSize(new Dimension(1000,200));

	splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, splitPane1, analysisPanel);
	//splitPane2.setOneTouchExpandable(true);
	splitPane2.setResizeWeight(0.75);

	layout.insertRow(4, TableLayout.FILL);
	mainPanel.add(splitPane2, "0, 4");
    }

    public JLabel createPanelLabel(String text) {
	JLabel aLabel = new JLabel(text);
	aLabel.setFont(new Font(fontName, Font.BOLD, 18));
	aLabel.setBackground(Color.BLUE);
	aLabel.setOpaque(true);
	aLabel.setForeground(Color.WHITE);
	return aLabel;
    }
 
    void removeFocus() {
	//mainMessage.requestFocus();
	analysisGui.requestFocus();
	//TODO: Still it does not work. The focus remains in the "Reset" button
    }

    /*
    JToolBar createToolBar() {
	JToolBar toolBar = new JToolBar("Still draggable");
	toolBar.setFloatable(false);
	
	JButton restartButton = new JButton("Restart");
	restartButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { ecatalog.restart(); } });
	toolBar.add(restartButton);

	//toolBar.add(new JButton("Undo"));
	return toolBar;
    }
    */

    /*
    void fixWindowSize(JFrame frame)
    {
	Dimension dim = frame.getSize();

	Dimension maxDim = Toolkit.getDefaultToolkit().getScreenSize();
	//Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(...);
	Insets insets = new Insets(20,20,20,20);
	maxDim.width = (maxDim.width - (insets.left + insets.right))*3/4;
	maxDim.height = (maxDim.height - (insets.top + insets.bottom))*35/40;
		
	//dim.width = Math.min(dim.width, maxDim.width);
	//dim.height = Math.min(dim.height, maxDim.height);
	dim = maxDim;
	frame.setSize(dim);
    }
    */

    /*
    static String getColorString(Color color) {
	return "#" + TextUtil.int2ToHex(color.getRed()) + TextUtil.int2ToHex(color.getGreen()) + TextUtil.int2ToHex(color.getBlue());
    }
    */
}




