/*
 * Login.java, 21 mai 2006
 * 
 * This file is part of FilmZ.
 *
 * Copyright © 2006 Johan Cwiklinski
 *
 * File :               Login.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.beans.PropertyVetoException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import be.xtnd.commons.MessagesCommons;
import be.xtnd.commons.gui.EscapeInternalFrame;
import be.xtnd.commons.gui.MainGui;
import be.xtnd.films.Authentification;
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 Login extends EscapeInternalFrame{
	private static final long serialVersionUID = -4603969317192141661L;
	
	/** Nom d ela fenêtre */
	public static final String NAME = "login_window";

	private TitledSeparator title;
	private JLabel login_label, passe_label;
	private JTextField login;
	private JPasswordField passe;
	private JButton connect, cancel;
	
	/**
	 * 
	 *
	 */
	public Login(){
		super();
		super.setFrameIcon(Menus.SECURE_IMAGE);
		
        putClientProperty(
                PlasticInternalFrameUI.IS_PALETTE,
                Boolean.TRUE);

		setName(Messages.getString("window.print.name"));
		FormPanel pane = new FormPanel( "be/xtnd/gui/descriptions/login.jfrm" );
		
		title = (TitledSeparator)pane.getComponentByName("title");
		title.setText(Messages.getString("window.login.title"));
		
		login_label = pane.getLabel("login_label");
		login_label.setText(Messages.getString("window.login.login"));
		
		passe_label = pane.getLabel("passe_label");
		passe_label.setText(Messages.getString("window.login.passe"));
		
		login = pane.getTextField("login");
		passe = (JPasswordField)pane.getComponentByName("passe");
		
		connect = (JButton)pane.getButton("connect");
		connect.setText(Messages.getString("window.login.connect"));
		connect.addActionListener(new OkayEvent());
		getRootPane().setDefaultButton(connect);
		
		cancel = (JButton)pane.getButton("cancel");
		cancel.setText(MessagesCommons.getString("window.buttons.cancel"));
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Menus.setInactive();
				dispose();
			}
		});
		
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setClosable(false);
		setMaximizable(false);
		setIconifiable(false);
		
		add(pane);
		
		pack();
		setVisible(true);
		setLocation(MainGui.centerOnDesktop(getSize()));
		MainGui.desktop.add(this,JLayeredPane.POPUP_LAYER);
		try {
			setSelected(true);
		} catch (PropertyVetoException e) {}			
	}
	
	private boolean verifEntrees(){
		boolean valid = true;
		StringBuffer sb = new StringBuffer();
		sb.append(MessagesCommons.getString("window.messages.required"));

		if(login.getText().trim().equals("")){
			sb.append("\n- " + Messages.getString("window.login.required.login"));
			if(valid) login.requestFocus();
			valid = false;
		}

		if(passe.getPassword().length==0){
			sb.append("\n- " + Messages.getString("window.login.required.password"));
			if(valid) passe.requestFocus();
			valid = false;
		}

		if(!valid){
			JOptionPane.showMessageDialog(
				MainGui.desktop, 
				sb.toString(),
				MessagesCommons.getString("window.messages.error"),
				JOptionPane.ERROR_MESSAGE);			
		}
		return valid;
	}
	
	class OkayEvent implements ActionListener{
		/**
		 * @param e
		 */
		public void actionPerformed(ActionEvent e) {
			if(verifEntrees()){
				if(
						Authentification.connect(
								login.getText(), 
								String.copyValueOf(passe.getPassword()))
						){
					FilmzMain.setParentalActive(true);
					FilmzMain.menus.populateCollections();
					FilmzMain.statusBar.setIcon(Menus.SECURE_IMAGE);
					dispose();
				}else{ //login incorrect
					JOptionPane.showMessageDialog(
							MainGui.desktop, 
							Messages.getString("window.login.messages.badlogin"),
							MessagesCommons.getString("window.messages.error"),
							JOptionPane.ERROR_MESSAGE);								
				}
			}
		}
	}
}
