package gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
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.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import other.MsgBox;
import other.SettingsData;
import other.SettingsHandler;

/**
 * SettingsDialog Klasse
 * Diese Klasse erzeugt ein Formular mit Hilfe dessen alle,
 * für die Netzwerkkommunikation nötigen Einstellungen
 * (Benutzernamen, Passwort, usw.) eingegeben werden 
 */
public class SettingsDialog extends Dialog {

	// Attribute
	private Shell sShell = null;
	private Group group = null;
	private Group group1 = null;
	private Group group2 = null;
	private Group group3 = null;
	private Composite composite = null;
	private Button button = null;
	private Button button1 = null;
	private Button chMaximized = null;
	private Label label = null;
	private Text tIpAddress = null;
	private Label label1 = null;
	private Text tNetInterv = null;
	private Label label2 = null;
	private Text tCmdInterv = null;
	private Label label3 = null;
	private Text tSqlUser = null;
	private Label label4 = null;
	private Text tSqlPass = null;
	private Label label5 = null;
	private Text tFtpUser = null;
	private Label label6 = null;
	private Text tFtpPass = null;
	private Label label7 = null;
	private Text tFtpDir = null;
	private Label label8 = null;
	private Text tCmdPass = null;
	private SettingsHandler setHandler = null;
	
	/**
	 * Diese Methode initialisiert die Shell. Alle Komponeten
	 * werden erzeugt und auf dem Formular angeordnet
	 */
	private void createSShell() {
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.marginHeight = 10;
		gridLayout1.verticalSpacing = 8;
		gridLayout1.marginWidth = 10;
		sShell = new Shell(SWT.APPLICATION_MODAL | SWT.CLOSE | SWT.TITLE);
		sShell.setText("Einstellungen");
		createGroup();
		createGroup1();
		createGroup2();
		createGroup3();
		createComposite();
		sShell.setLayout(gridLayout1);
		sShell.setSize(new Point(404, 456));
	}

	/**
	 * Diese Methode initialisiert die GroupBox. Alle Komponeten
	 * werden erzeugt und auf der GroupBox angeordnet.
	 */
	private void createGroup() {
		GridData gridData11 = new GridData();
		gridData11.grabExcessHorizontalSpace = true;
		gridData11.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData6 = new GridData();
		gridData6.horizontalSpan = 2;
		GridLayout gridLayout2 = new GridLayout();
		gridLayout2.numColumns = 2;
		gridLayout2.marginHeight = 10;
		gridLayout2.marginWidth = 15;
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		group = new Group(sShell, SWT.NONE);
		group.setLayoutData(gridData);
		group.setLayout(gridLayout2);
		group.setText("Allgemein");
		chMaximized = new Button(group, SWT.CHECK);
		chMaximized.setText("Maximiert starten");
		chMaximized.setLayoutData(gridData6);
		label = new Label(group, SWT.NONE);
		label.setText("IP-Adresse der Servers:");
		tIpAddress = new Text(group, SWT.BORDER);
		tIpAddress.setLayoutData(gridData11);
		label1 = new Label(group, SWT.NONE);
		label1.setText("Allgemeines Timer-Intervall:");
		tNetInterv = new Text(group, SWT.BORDER);
		label2 = new Label(group, SWT.NONE);
		label2.setText("Socket-Timer-Intervall:");
		tCmdInterv = new Text(group, SWT.BORDER);
	}

	/**
	 * Diese Methode initialisiert die GroupBox. Alle Komponeten
	 * werden erzeugt und auf der GroupBox angeordnet.
	 */
	private void createGroup1() {
		GridData gridData8 = new GridData();
		gridData8.grabExcessHorizontalSpace = true;
		gridData8.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData7 = new GridData();
		gridData7.grabExcessHorizontalSpace = true;
		gridData7.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridLayout gridLayout3 = new GridLayout();
		gridLayout3.numColumns = 4;
		gridLayout3.marginWidth = 15;
		gridLayout3.marginHeight = 10;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData1.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData1.grabExcessVerticalSpace = true;
		group1 = new Group(sShell, SWT.NONE);
		group1.setLayoutData(gridData1);
		group1.setLayout(gridLayout3);
		group1.setText("SQL-Verbindung");
		label3 = new Label(group1, SWT.NONE);
		label3.setText("User:");
		tSqlUser = new Text(group1, SWT.BORDER);
		tSqlUser.setLayoutData(gridData8);
		label4 = new Label(group1, SWT.NONE);
		label4.setText("Passwort:");
		tSqlPass = new Text(group1, SWT.BORDER);
		tSqlPass.setEchoChar('*');
		tSqlPass.setLayoutData(gridData7);
	}

	/**
	 * Diese Methode initialisiert die GroupBox. Alle Komponeten
	 * werden erzeugt und auf der GroupBox angeordnet.
	 */
	private void createGroup2() {
		GridData gridData12 = new GridData();
		gridData12.horizontalSpan = 3;
		gridData12.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData10 = new GridData();
		gridData10.grabExcessHorizontalSpace = true;
		gridData10.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData9 = new GridData();
		gridData9.grabExcessHorizontalSpace = true;
		gridData9.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridLayout gridLayout4 = new GridLayout();
		gridLayout4.numColumns = 4;
		gridLayout4.marginWidth = 15;
		gridLayout4.marginHeight = 10;
		GridData gridData2 = new GridData();
		gridData2.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData2.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData2.grabExcessVerticalSpace = true;
		group2 = new Group(sShell, SWT.NONE);
		group2.setLayoutData(gridData2);
		group2.setLayout(gridLayout4);
		group2.setText("FTP-Verbindung");
		label5 = new Label(group2, SWT.NONE);
		label5.setText("User:");
		tFtpUser = new Text(group2, SWT.BORDER);
		tFtpUser.setLayoutData(gridData10);
		label6 = new Label(group2, SWT.NONE);
		label6.setText("Passwort:");
		tFtpPass = new Text(group2, SWT.BORDER);
		tFtpPass.setEchoChar('*');
		tFtpPass.setLayoutData(gridData9);
		label7 = new Label(group2, SWT.NONE);
		label7.setText("Verzeichnis:");
		tFtpDir = new Text(group2, SWT.BORDER);
		tFtpDir.setLayoutData(gridData12);
	}

	/**
	 * Diese Methode initialisiert die GroupBox. Alle Komponeten
	 * werden erzeugt und auf der GroupBox angeordnet.
	 */
	private void createGroup3() {
		GridData gridData13 = new GridData();
		gridData13.horizontalIndent = -1;
		gridData13.widthHint = 120;
		gridData13.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridLayout gridLayout5 = new GridLayout();
		gridLayout5.marginHeight = 10;
		gridLayout5.numColumns = 2;
		gridLayout5.marginWidth = 15;
		GridData gridData3 = new GridData();
		gridData3.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData3.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData3.grabExcessVerticalSpace = true;
		group3 = new Group(sShell, SWT.NONE);
		group3.setLayoutData(gridData3);
		group3.setLayout(gridLayout5);
		group3.setText("Command-Verbindung");
		label8 = new Label(group3, SWT.NONE);
		label8.setText("Passwort:");
		tCmdPass = new Text(group3, SWT.BORDER);
		tCmdPass.setEchoChar('*');
		tCmdPass.setLayoutData(gridData13);
	}

	/**
	 * Diese Methode initialisiert das Composite. Alle Komponeten
	 * werden erzeugt und auf diesem angeordnet.
	 */
	private void createComposite() {
		GridData gridData5 = new GridData();
		gridData5.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData4 = new GridData();
		gridData4.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		gridLayout.horizontalSpacing = 20;
		gridLayout.makeColumnsEqualWidth = true;
		composite = new Composite(sShell, SWT.NONE);
		composite.setLayout(gridLayout);
		composite.setLayoutData(gridData4);
		button1 = new Button(composite, SWT.NONE);
		button1.setText("   Abbrechen   ");
		button1.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {    
        		sShell.close();
        	}
        });
		button = new Button(composite, SWT.NONE);
		button.setText("OK");
		button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {    
        		storeValuesAndClose();
        	}
        });
		button.setLayoutData(gridData5);
	}

	/**
	 * Konstruktor
	 * @param parent übergeordnete Shell
	 */
	public SettingsDialog(Shell parent, SettingsHandler setHandl){
		super(parent);
		setHandler = setHandl;
		// Shell erzeugen
		createSShell();
		// Werte initialisieren
		initValues(setHandl.getData());
	}

	/* ##########################################################################
	 * 						ÖFFENTLICHE METHODEN
	 * ##########################################################################*/	
	
	/**
	 * Öffnet den Dialog
	 */
	public void open(){
		// Ereignisschleife
		sShell.open();
		Display display = getParent().getDisplay();
		while(!sShell.isDisposed()){
			if (!display.readAndDispatch()) display.sleep();
		}
	}
	
	/* ##########################################################################
	 * 						HILFSMETHODEN
	 * ##########################################################################*/
	
	/**
	 * Initialisiert die Felder des Dialogs
	 * @param setData Objekt das alle einzustellenden Attribute als Variable enthält
	 */
	private void initValues(SettingsData setData){
		chMaximized.setSelection(setData.startMaximized);
		tIpAddress.setText(setData.ipAddress);
		tNetInterv.setText(""+setData.netTimerInterv);
		tCmdInterv.setText(""+setData.cmdTimerInterv);
		tSqlUser.setText(setData.sqlUser);
		tSqlPass.setText(setData.sqlPassw);
		tFtpUser.setText(setData.ftpUser);
		tFtpPass.setText(setData.ftpPassw);
		tFtpDir.setText(setData.ftpDir);
		tCmdPass.setText(setData.cmdPassw);
	}
	
	/**
	 * Initialisiert die Felder des Dialogs
	 */
	private void storeValuesAndClose(){
		SettingsData setData = setHandler.getData();
		try{
			setData.netTimerInterv = Integer.parseInt(tNetInterv.getText());
			setData.cmdTimerInterv = Integer.parseInt(tCmdInterv.getText());
		}catch(Exception e){
			MsgBox.showError("Bitte für die Timer-Intervalle gültige Zahlen eingeben!");
			return;
		}
		setData.startMaximized = chMaximized.getSelection();
		setData.ipAddress = tIpAddress.getText();
		setData.sqlUser = tSqlUser.getText();
		setData.sqlPassw = tSqlPass.getText();
		setData.ftpUser = tFtpUser.getText();
		setData.ftpPassw = tFtpPass.getText();
		setData.ftpDir = tFtpDir.getText();
		setData.cmdPassw = tCmdPass.getText();
		setHandler.writeObject();
		sShell.close();
	}
	
}
