/*
 * Impressions.java, 21 sept. 2005
 * 
 * This file is part of FilmZ.
 *
 * Copyright © 2005 Johan Cwiklinski
 *
 * File :               Impressions.java
 * Author's email :     johan@x-tnd.be
 * Author's Website :   http://www.x-tnd.be/
 * 
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 */

package be.xtnd.films.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import be.xtnd.commons.GuiCommons;
import be.xtnd.commons.Print;
import be.xtnd.commons.db.Database;
import be.xtnd.commons.gui.EscapeInternalFrame;
import be.xtnd.commons.gui.MainGui;
import be.xtnd.films.Film;
import be.xtnd.films.Genre;
import be.xtnd.gui.Menus;

import com.jeta.forms.components.border.TitledBorderLabel;
import com.jeta.forms.components.panel.FormPanel;
import com.jeta.forms.components.separator.TitledSeparator;
import com.jgoodies.looks.plastic.PlasticInternalFrameUI;

/**
 * 
 *
 * @author Johan Cwiklinski
 */
public class Impressions extends EscapeInternalFrame {
	private static final long serialVersionUID = 687423873033827104L;
	private GuiCommons commons = new GuiCommons(Messages.getBundle());
	private JLabel titre_label, genres_label, tri_label_1, tri_label_2, tri_label_3;
	private TitledSeparator window_title;
	private JTextField titre;
	private JList genres;
	private JCheckBox pdf, sort_order;
    private DefaultListModel listModel;
    private TitledBorderLabel tri_title;
    private JComboBox tri_1, tri_2, tri_3;

    /**
     * 
     *
     */
	public Impressions(){
		super();
		super.setFrameIcon(Menus.PRINT_IMAGE);
		
        putClientProperty(
                PlasticInternalFrameUI.IS_PALETTE,
                Boolean.TRUE);

		setName(Messages.getString("window.print.name"));
		FormPanel pane = new FormPanel( "be/xtnd/gui/descriptions/impression.jfrm" );

		window_title = (TitledSeparator)pane.getComponentByName("window_title");
		window_title.setText(Messages.getString("window.print.title"));
		
		titre_label = pane.getLabel("titre_label");
		titre_label.setText(Messages.getString("window.print.labels.title"));
		
		titre = pane.getTextField("titre");
		
		genres_label = pane.getLabel("genres_label");
		genres_label.setText(Messages.getString("window.print.labels.type"));
		
		genres = pane.getList("genres");
		listModel = new DefaultListModel();
		ArrayList al = new Genre().listing();
		for(int i = 0 ; i < al.size() ; i++){
			listModel.addElement(al.get(i));
		}
		genres.setModel(listModel);
		
		pdf = pane.getCheckBox("pdf");
		pdf.setText(Messages.getString("window.print.labels.pdf"));
		
		sort_order = pane.getCheckBox("sort_order");
		sort_order.setText(Messages.getString("window.print.labels.sortorder"));
		
		tri_title = (TitledBorderLabel)pane.getComponentByName("tri_title");
		tri_title.setText(Messages.getString("window.print.labels.order_title"));
		
		tri_label_1 = pane.getLabel("tri_label_1");
		tri_label_1.setText(Messages.getString("window.print.labels.1storder"));
		
		tri_label_2 = pane.getLabel("tri_label_2");
		tri_label_2.setText(Messages.getString("window.print.labels.2ndorder"));
		
		tri_label_3 = pane.getLabel("tri_label_3");
		tri_label_3.setText(Messages.getString("window.print.labels.3rdorder"));
		
		
		ArrayList<String> valeurs_tri = new ArrayList<String>(3);
		valeurs_tri.add(Messages.getString("window.print.tri.number"));
		valeurs_tri.add(Messages.getString("window.print.tri.title"));
		valeurs_tri.add(Messages.getString("window.print.tri.type"));

	    tri_1 = pane.getComboBox("tri_1");
	    commons.buildCombo(tri_1, valeurs_tri);
	    
	    tri_2 = pane.getComboBox("tri_2");
	    commons.buildCombo(tri_2, valeurs_tri);
	    
	    tri_3 = pane.getComboBox("tri_3");
	    commons.buildCombo(tri_3, valeurs_tri);
		
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setClosable(true);
		setMaximizable(true);
		setIconifiable(true);
		
		add(pane);
		
		commons.createButtonBar(this,new OkayEvent());
		
		setMinimumSize(new Dimension(450,350));
		setPreferredSize(new Dimension(450,350));
		pack();
		setVisible(true);
		setLocation(MainGui.centerOnDesktop(getSize()));
		MainGui.desktop.add(this,JLayeredPane.MODAL_LAYER);
		try {
			setSelected(true);
		} catch (PropertyVetoException e) {}			

	}
	
	class OkayEvent implements ActionListener{
		/**
		 * @param e
		 */
		public void actionPerformed(ActionEvent e) {
			boolean where = false;
			StringBuffer query = new StringBuffer();
			query.append(Film.BASE_LIST_QUERY);

			if(!titre.getText().trim().equals("")){
				query.append((where)?" AND ":" WHERE ");
				where = true;
				query.append("titre LIKE '"+titre.getText()+"'");
			}
			if(genres.getSelectedValues().length>0){
				query.append((where)?" AND (":" WHERE ");
				Object [] o = genres.getSelectedValues();
				for(int i = 0 ; i < o.length ; i++){
					if(i>0) query.append(" OR ");
					query.append("genre='"+o[i]+"'");
				}
				if(where) query.append(")");
				where = true;
			}
			query.append(Film.GROUP_BY);
			if(tri_1.getSelectedIndex()>0){
				where = true;
				query.append(" ORDER BY ");
				query.append(getOrderBy(tri_1.getSelectedIndex()));
				if(tri_2.getSelectedIndex()>0 && 
						tri_1.getSelectedIndex()!=tri_2.getSelectedIndex()){
					query.append(", ");
					query.append(getOrderBy(tri_2.getSelectedIndex()));
					if(tri_3.getSelectedIndex()>0 &&
							tri_3.getSelectedIndex()!=tri_2.getSelectedIndex() &&
							tri_3.getSelectedIndex()!=tri_1.getSelectedIndex()){
						query.append(", ");
						query.append(getOrderBy(tri_3.getSelectedIndex()));						
					}
				}
				query.append((sort_order.isSelected())?" ASC":" DESC");
			}
			if(where){
				Database db = new Database(true);
				try {
					ResultSet rs = db.execQuery(query.toString());
					new Print(".config", "filmz.jrxml", rs);					
				} catch (SQLException e1) {e1.printStackTrace();}
			}else{
				new Print(".config", "filmz.jrxml");
			}
		}		
		
		private String getOrderBy(Integer selected){
			String toReturn = "";
			switch(selected){
				case 1:
					toReturn = "Num";
					break;
				case 2:
					toReturn = "Titre";
					break;
				case 3:
					toReturn = "Genre";
					break;
			}
			return toReturn;
		}
	}

}
