/*
 * LembretesWindow.java
 *
 * Created on 13 de Fevereiro de 2007, 11:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package siprp.lembretes;

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

import com.evolute.utils.tables.*;
import com.evolute.utils.tracker.TrackableWindow;
import com.evolute.utils.ui.DialogException;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import siprp.lembretes.remarcacoes.LembretesRemarcacaoPanel;

/**
 *
 * @author Frederico
 */
public class LembretesWindow extends JFrame
	implements ActionListener, TrackableWindow, ListSelectionListener,
				LembretesConstants
{
	public static final String TITLE = "Lembretes";
	
	protected VectorTableModel tiposModel;
	protected BaseTable tiposTable;
	protected JPanel lembretesPanel;
	protected JButton recarregarButton;
	
	protected LembretesDataProvider provider;
	
	/** Creates a new instance of LembretesWindow */
	public LembretesWindow()
		throws Exception
	{
		provider = LembretesDataProvider.getProvider();
		setupComponents();
	}
	
	private void setupComponents()
	{
		setTitle( "Lembretes" );
		tiposModel = new VectorTableModel( new String[]{ "tipo", "quantidade" } );
		tiposTable = new BaseTable( tiposModel );
		tiposTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		tiposTable.fixColumnWidth( 0, 200 );
		tiposTable.fixColumnWidth( 1, 100 );
		tiposTable.getSelectionModel().addListSelectionListener( this );
		JScrollPane scp =
			new JScrollPane( tiposTable, JScrollPane.VERTICAL_SCROLLBAR_NEVER,
			JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
		scp.setPreferredSize( new Dimension( 300, 20 ) );
		lembretesPanel = new JPanel();
		lembretesPanel.setLayout( new GridLayout( 1, 1 ) );
		recarregarButton = new JButton( "Recarregar" );
		recarregarButton.addActionListener( this );
		
		getContentPane().setLayout( new BorderLayout() );
		getContentPane().add( scp, BorderLayout.WEST );
		getContentPane().add( lembretesPanel, BorderLayout.CENTER );
		getContentPane().add( recarregarButton, BorderLayout.SOUTH );
		
		addWindowListener( new WindowAdapter(){
			public void windowClosing( WindowEvent e )
			{
				close();
			}
			
			public void windowOpened( WindowEvent e )
			{
//				setExtendedState( getExtendedState() | MAXIMIZED_BOTH );
				setSize( 1024, 700 );
				reloadTotais();
			}
		} );
	}
	
	public void refresh()
	{
	}

	public void open()
	{
		setVisible( true );
	}
	
	public void close()
	{
		SwingUtilities.invokeLater( new Runnable() {
			public void run()
			{
				setVisible( false );
				dispose();
			}
		} );
	}
	
	protected void reloadTotais()
	{
		try
		{
			Vector values = tiposModel.getValues();
			values.clear();
			TipoLembrete tipos[] = provider.getTiposLembrete();
			for( int n = 0; n < tipos.length; n++ )
			{
				int count = provider.countLembretesByTipo( tipos[ n ].getID() );
				values.add( new ColumnizedObjectArray( 
					new Object[]{ tipos[ n ].getID(), tipos[ n ].getDescricao(),
									"" + count }, true ) );
			}
			tiposModel.setValues( values );
		}
		catch( Exception ex )
		{
			DialogException.showExceptionMessage( ex, "Erro a carregar lembretes", true );
		}
	}
	
	protected void reloadLembretes()
	{
		lembretesPanel.removeAll();
		int selected = tiposTable.getSelectedRow();
		if( selected != -1 )
		{
			ColumnizedObjectArray line = ( ColumnizedObjectArray ) tiposModel.getRowAt( selected );
			Integer tipoID = line.getID();
			try
			{
				TipoLembrete tipo = provider.getTipoLembreteByID( tipoID );
				if( CODE_EXTERNO.equals( tipo.getCodigo() ) )
				{
					
				}
				if( CODE_REMARCACOES.equals( tipo.getCodigo() ) )
				{
					lembretesPanel.add( new LembretesRemarcacaoPanel() );
				}
				if( CODE_MARCACOES.equals( tipo.getCodigo() ) )
				{
				}
				if( CODE_OUTROS.equals( tipo.getCodigo() ) )
				{
				}
				
			}
			catch( Exception ex )
			{
				DialogException.showExceptionMessage( ex, "Erro a carregar lembretes", true );
			}
		}
		validate();
		repaint();
	}

	public boolean closeIfPossible()
	{
		close();
		return true;
	}

	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if( source.equals( recarregarButton ) )
		{
			reloadTotais();
		}
	}

	public void valueChanged(ListSelectionEvent e)
	{
		if( !e.getValueIsAdjusting() )
		{
			reloadLembretes();
		}
	}
}
