package zemplen;

import java.util.Scanner;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.hibernate.cfg.Configuration;

public class ConfigurationDialog extends Dialog {

	Text txtHost, txtPort, txtDb, txtUser, txtPassword;
	Configuration cfg;
	
	protected ConfigurationDialog(IShellProvider parentShell, Configuration cfg) {
		super(parentShell);
		
		this.cfg = cfg;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite cpt = new Composite(parent, SWT.NONE);
		GridLayout gridLayout = new GridLayout();
		GridData data;
		gridLayout.numColumns = 2;
		cpt.setLayout(gridLayout);

		new Label(cpt, SWT.NONE).setText("Host:");
		
		txtHost = new Text(cpt, SWT.BORDER);
		data = new GridData(GridData.FILL_HORIZONTAL);
		txtHost.setLayoutData(data);
		
		new Label(cpt, SWT.NONE).setText("Port:");
		
		txtPort = new Text(cpt, SWT.BORDER);
		data = new GridData(GridData.FILL_HORIZONTAL);
		txtPort.setLayoutData(data);
		
		new Label(cpt, SWT.NONE).setText("Adatbázis:");
		
		txtDb = new Text(cpt, SWT.BORDER);
		data = new GridData(GridData.FILL_HORIZONTAL);
		txtDb.setLayoutData(data);
		
		new Label(cpt, SWT.NONE).setText("Felhasználó:");
		
		txtUser = new Text(cpt, SWT.BORDER);
		data = new GridData(GridData.FILL_HORIZONTAL);
		txtUser.setLayoutData(data);
		
		new Label(cpt, SWT.NONE).setText("Jelszó:");
		
		txtPassword = new Text(cpt, SWT.BORDER | SWT.PASSWORD);
		data = new GridData(GridData.FILL_HORIZONTAL);
		txtPassword.setLayoutData(data);
		
		String url = cfg.getProperty("connection.url");
		if (url != null) {
			Scanner sc = new Scanner(url);
			System.out.println(url);
			Pattern urlPattern = Pattern.compile("^jdbc:postgresql://([^/:]*)(:\\d+)?/([^/]*)$");
			if (sc.hasNext(urlPattern)) {
				sc.next(urlPattern);
				MatchResult match = sc.match();
				String host = match.group(1);
				String port = match.group(2);
				String db = match.group(3);
				txtHost.setText(host);
				txtPort.setText(port != null ? port : "<alapértelmezett>");
				txtDb.setText(db);
				txtHost.setEnabled(false);
				txtPort.setEnabled(false);
				txtDb.setEnabled(false);
			}
		}
		String user = cfg.getProperty("connection.username");
		if (user != null) {
			txtUser.setText(user);
			txtUser.setEnabled(false);
		}
		String password = cfg.getProperty("connection.password");
		if (password != null) {
			txtPassword.setText(user);
			txtPassword.setEnabled(false);
		}

		return cpt;
	}
	
	@Override
	protected void okPressed() {
		if (txtHost.getEnabled()) {
			StringBuilder url = new StringBuilder("jdbc:postgresql://");
			url.append(txtHost.getText());
			if (!txtPort.getText().equals("<alapértelmezett>"))
				url.append(':').append(txtPort.getText());
			url.append('/').append(txtDb.getText());
			cfg = cfg.setProperty("connection.url", url.toString());
		}
		if (txtUser.getEnabled())
			cfg = cfg.setProperty("connection.username", txtUser.getText());
		if (txtPassword.getEnabled())
			cfg = cfg.setProperty("connection.password", txtPassword.getText());
		super.okPressed();
	}
}
