package zemplen;

import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import zemplen.report.ReportGen;

public class ReportDialog extends Dialog {

	Text txtReportFile;
	Button btnReportFile;
	Text txtExportedFile;
	Button btnExportedFile;

	Button btnEmbeddable;
	
	String reportFileName;
	
	public ReportDialog(Shell parentShell) {
		super(parentShell);
		
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite cpt = new Composite(parent, SWT.NONE);
		
		GridLayout gridLayout = new GridLayout();
		GridData data;
		gridLayout.numColumns = 3;
		cpt.setLayout(gridLayout);
				
		new Label(cpt, SWT.NONE).setText("Riport megnyitása:");
		txtReportFile = new Text(cpt, SWT.BORDER);
		txtReportFile.setEnabled(false);
		btnReportFile = new Button(cpt, SWT.BORDER);
		btnReportFile.setText("Kiválaszt");
		data = new GridData(GridData.FILL_HORIZONTAL);
		btnReportFile.setLayoutData(data);
		
		new Label(cpt, SWT.NONE).setText("Exportálás fájlba:");
		txtExportedFile = new Text(cpt, SWT.BORDER);
		txtExportedFile.setEnabled(false);
		btnExportedFile = new Button(cpt, SWT.BORDER);
		btnExportedFile.setText("Kiválaszt");
		data = new GridData(GridData.FILL_HORIZONTAL);
		btnExportedFile.setLayoutData(data);

		new Label(cpt, SWT.NONE).setText("Beágyazható:");
		btnEmbeddable = new Button(cpt, SWT.CHECK);
		btnEmbeddable.setEnabled(false);
		
		btnReportFile.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				FileDialog fd = new FileDialog(getShell(), SWT.OPEN);
				fd.setText("Riport:");
				fd.setFilterPath("/home/espakm/workspace/zm1/src/zemplen/report/");
				fd.setFilterExtensions(new String[]{"*.rptdesign"});
				String path = fd.open();
				if (path != null)
					txtReportFile.setText(path);
			}
		});
		btnExportedFile.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				FileDialog fd = new FileDialog(getShell(), SWT.SAVE);
				fd.setText("Exportálandó fájl:");
				fd.setFilterExtensions(new String[]{"*.pdf", "*.html", "*.xls", "*.doc", "*.ppt", "*.ps"});
				String path = fd.open();
				if (path != null) {
					txtExportedFile.setText(path);
					btnEmbeddable.setEnabled(path.endsWith(".html"));
				}
				else
					btnEmbeddable.setEnabled(false);
			}
		});

		return cpt;
	}
	
	@Override
	protected void okPressed() {
		if (txtReportFile.getText() != null && txtExportedFile.getText() != null) {
			try {
				ReportGen.generate(txtReportFile.getText(), txtExportedFile.getText(), btnEmbeddable.getSelection());
				super.okPressed();
			} catch (EngineException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
