package es.dalocar.facturamas.swt;

import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

import es.dalocar.facturamas.swt.handlers.ListaFacturasHandler;
import es.dalocar.software.Messages;
import es.dalocar.software.utils.ResourceUtils;

public class MainApp {

	private static final String LOG4J_CONFIG_FILENAME = "conf/log4j.properties"; //$NON-NLS-1$

	/** The log4j logger */
	private static final Logger log = Logger.getLogger(MainApp.class.getName());

	
	protected Shell shlSwtApplication;
	private Button buttonFacturas;
	private Button buttonClientes;
	private Button buttonInfoFacturacion;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MainApp window = new MainApp();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shlSwtApplication.setMaximized(true);
		shlSwtApplication.open();
		shlSwtApplication.layout();
		while (!shlSwtApplication.isDisposed()) {
			//$protect>>$
			try {
				if (!display.readAndDispatch())
					display.sleep();
			} catch (Exception e) {
				handleError(shlSwtApplication, e);
			}
			//$protect<<$

		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlSwtApplication = new Shell();
		shlSwtApplication.setMinimumSize(new Point(640, 480));
		shlSwtApplication.setSize(640, 480);
		shlSwtApplication.setText("FacturaM\u00E1s - Vitrificados Chelo");
		RowLayout rowLayout = new RowLayout(SWT.VERTICAL);
		rowLayout.justify = true;
		rowLayout.fill = true;
		rowLayout.wrap = false;
		rowLayout.spacing = 2;
		shlSwtApplication.setLayout(rowLayout);
		
		buttonFacturas = new Button(shlSwtApplication, SWT.NONE);
		buttonFacturas.setLayoutData(new RowData(198, 67));
		buttonFacturas.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				new ListaFacturasHandler().run(shlSwtApplication);
			}
		});
		buttonFacturas.setText("Mantenimiento Facturas");
		
		buttonClientes = new Button(shlSwtApplication, SWT.NONE);
		buttonClientes.setLayoutData(new RowData(219, 67));
		buttonClientes.setText("Mantenimiento Clientes");
		
		buttonInfoFacturacion = new Button(shlSwtApplication, SWT.NONE);
		buttonInfoFacturacion.setLayoutData(new RowData(219, 66));
		buttonInfoFacturacion.setText("Informes Facturacion");
		
		Menu menu = new Menu(shlSwtApplication, SWT.BAR);
		shlSwtApplication.setMenuBar(menu);
		
		MenuItem mntmArchivo = new MenuItem(menu, SWT.CASCADE);
		mntmArchivo.setText("Archivo");
		
		Menu menu_1 = new Menu(mntmArchivo);
		mntmArchivo.setMenu(menu_1);
		
		MenuItem mntmMantenimientosFacturas = new MenuItem(menu_1, SWT.NONE);
		mntmMantenimientosFacturas.setText("Mantenimientos Facturas");
		
		MenuItem mntmMantenimientoClientes = new MenuItem(menu_1, SWT.NONE);
		mntmMantenimientoClientes.setText("Mantenimiento Clientes");
		
		MenuItem mntmInformesFacturacion = new MenuItem(menu_1, SWT.NONE);
		mntmInformesFacturacion.setText("Informes Facturacion");

	}
	
	/**
	 * Manejador general de errores. La implementacion muestra una pantalla de error con
	 * un mensaje genérico.
	 * @param shell Active Shell
	 * @param e Exception
	 */
	private static void handleError(Shell shell, Exception e) {
		
		log.fatal("Unexpected!: " +e.getMessage(), e); //$NON-NLS-1$
		
		if(ResourceUtils.getBoolean(ResourceUtils.DEVELOP_MODE)) {
			shell.dispose();
			throw new RuntimeException(e);
		} else {
			MessageBox m = new MessageBox(shell,SWT.ICON_ERROR);
			m.setText(Messages.getString("MainComposite.general.error.title")); //$NON-NLS-1$
			m.setMessage(Messages.getString("MainComposite.general.error")+": " +e.getMessage()); //$NON-NLS-1$
			m.open();
		}
	}

}
