/*
 * FilmzOptions.java, 21 mai 2006
 * 
 * This file is part of FilmZ.
 *
 * Copyright © 2006 Johan Cwiklinski
 *
 * File :               FilmzOptions.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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyVetoException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.table.AbstractTableModel;

import be.xtnd.commons.GuiCommons;
import be.xtnd.commons.MessagesCommons;
import be.xtnd.commons.gui.EscapeInternalFrame;
import be.xtnd.commons.gui.MainGui;
import be.xtnd.films.Collection;
import be.xtnd.films.Preferences;
import be.xtnd.films.Users;
import be.xtnd.films.plugins.Plugins;
import be.xtnd.gui.FilmzMain;
import be.xtnd.gui.Menus;

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 FilmzOptions extends EscapeInternalFrame  {
	private static final long serialVersionUID = -1029596119916146958L;
	/** Nom de la fenêtre */
	public static final String NAME = "options";
	private JTabbedPane tbd;
	private TitledSeparator title;
	private JLabel covers_path_label, default_collection_label, default_web_label, plugins_path_label;
	private JTextField covers_path, plugins_path;
	private JComboBox default_collection, default_web;
	private JButton ok, apply, cancel, add_user, del_user, choose_path, choose_plugins_path;
	private JTable users;
	private Preferences prefs = FilmzMain.prefs;
	private GuiCommons commons = new GuiCommons(Messages.getBundle());

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

		setName(NAME);
		setTitle(Messages.getString("window.options.title"));
		FormPanel pane = new FormPanel( "be/xtnd/gui/descriptions/options.jfrm" );

		title = (TitledSeparator)pane.getComponentByName("title");
		title.setText(Messages.getString("window.options.title"));
		
		tbd = pane.getTabbedPane("tbd");
		tbd.setTitleAt(0, Messages.getString("window.options.tabs.general"));
		tbd.setToolTipTextAt(0, Messages.getString("window.options.tabs.general.tooltip"));
		tbd.setTitleAt(1, Messages.getString("window.options.tabs.users"));
		tbd.setToolTipTextAt(1, Messages.getString("window.options.tabs.users.tooltip"));
		
		covers_path_label = pane.getLabel("covers_path_label");
		covers_path_label.setText(Messages.getString("window.options.covers_path_label"));

		covers_path = pane.getTextField("covers_path");
		covers_path.setText(prefs.getCovers_path());
		
		choose_path = (JButton)pane.getButton("choose_path");
		choose_path.setToolTipText(Messages.getString("window.options.covers_path_button"));
		choose_path.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				// TODO Raccord de méthode auto-généré
			}
		});
		
		default_collection_label = pane.getLabel("default_collection_label");
		default_collection_label.setText(Messages.getString("window.options.default_collection_label"));
		
		default_collection = pane.getComboBox("default_collection");
		commons.buildCombo(default_collection, Collection.liste(false));
		default_collection.setSelectedItem(new Collection(prefs.getDefault_collection()).getNom());
		
		default_web_label = pane.getLabel("default_web_label");
		default_web_label.setText(Messages.getString("window.options.default_web_label"));
		
		default_web = pane.getComboBox("default_web");
		commons.buildCombo(default_web, Plugins.liste());
		System.err.println(prefs.getWeb_default());
		default_web.setSelectedItem(prefs.getWeb_default());		
		
		plugins_path_label = pane.getLabel("plugins_path_label");
		plugins_path_label.setText(Messages.getString("window.options.plugins_path_label"));
		
		plugins_path = pane.getTextField("plugins_path");
		plugins_path.setText(prefs.getPlugins_path());
		
		choose_plugins_path = (JButton)pane.getButton("choose_plugins_path");
		choose_plugins_path.setToolTipText(Messages.getString("window.options.plugins_path_button"));
		choose_plugins_path.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				// TODO Raccord de méthode auto-généré
			}
		});
		
		users = pane.getTable("users");
		ArrayList<Object[]> u = Users.list();
		Object[][] users_data = new Object[u.size()][];
		for(int i = 0 ; i < u.size() ; i++){
			users_data[i] = u.get(i);
		}
		users.setModel(new UsersTableModel(
				Users.tableHeaders(),
				users_data));
		int checkbox_width = 30;
		users.getColumnModel().getColumn(2).setPreferredWidth(checkbox_width);
		users.getColumnModel().getColumn(2).setMinWidth(checkbox_width);
		users.getColumnModel().getColumn(2).setMaxWidth(checkbox_width);
		users.getColumnModel().getColumn(3).setPreferredWidth(checkbox_width);
		users.getColumnModel().getColumn(3).setMinWidth(checkbox_width);
		users.getColumnModel().getColumn(3).setMaxWidth(checkbox_width);
		users.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){
				if(e.getClickCount() == 2){
					//TODO modification/consultation de l'utilisateur
					String id = users.getValueAt(users.rowAtPoint(e.getPoint()),0).toString();
					if(MainGui.verifUnicite(DetailsUtilisateur.MODIF_NAME+id,null)){
						new DetailsUtilisateur(id);
					}
				}
			}
		});
		
		add_user = (JButton)pane.getButton("add_user");
		add_user.setToolTipText(Messages.getString("window.options.add_user_tooltip"));
		add_user.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(MainGui.verifUnicite(DetailsUtilisateur.NEW_NAME,null)){
					new DetailsUtilisateur();
				}
			}
		});
		
		del_user = (JButton)pane.getButton("del_user");
		del_user.setToolTipText(Messages.getString("window.options.del_user_tooltip"));
		del_user.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				// TODO Raccord de méthode auto-généré
			}
		});
		
		ok = (JButton)pane.getButton("ok");
		ok.setText(MessagesCommons.getString("window.buttons.okay"));
		
		apply = (JButton)pane.getButton("apply");
		apply.setText(MessagesCommons.getString("window.buttons.apply"));
		
		cancel = (JButton)pane.getButton("cancel");
		cancel.setText(MessagesCommons.getString("window.buttons.cancel"));
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		getRootPane().setDefaultButton(ok);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setClosable(true);
		setMaximizable(false);
		setIconifiable(false);
		setSize(450,250);
		
		add(pane);
		
		setVisible(true);
		setLocation(MainGui.centerOnDesktop(getSize()));
		MainGui.desktop.add(this,JLayeredPane.POPUP_LAYER);
		try {
			setSelected(true);
		} catch (PropertyVetoException e) {}	
	}
	
    private class UsersTableModel extends AbstractTableModel {
		private static final long serialVersionUID = 3282200795160431498L;
		private String[] columnNames;
        private Object[][] data;
    	        
        UsersTableModel(String[] colNames, Object[][] rowsDatas){
        	this.columnNames = colNames;
        	this.data = rowsDatas;
        }

        /**
         * @return int
         */
        public int getColumnCount() {
            return columnNames.length;
        }

        /**
         * @return int
         */
        public int getRowCount() {
            return data.length;
        }

        /**
         * @param col
         * @return String
         */
        public String getColumnName(int col) {
            return columnNames[col];
        }

        /**
         * @param row
         * @param col
         * @return Object
         */
        public Object getValueAt(int row, int col) {
        	return data[row][col];
        }

        /**
         * @param c
         * @return Class
         */
        public Class<?> getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /**
         * @param row
         * @param col
         * @return boolean
         */
        public boolean isCellEditable(int row, int col) {
        	return false;
        }

        /**
         * @param value
         * @param row
         * @param col
         */
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }
    }
}
