package com.xaraya.wizard;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
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.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

public class XarayaModuleFilePage extends WizardPage implements Listener
{

	IStructuredSelection selection;
	
	//Xaraya stuff
	private Text modversionid;
	private Text modversionversion;
	private Text modversiondescription;
	private Text modversionofficial;
	private Text modversionauthor;
	private Text modversioncontact;
	private Text modversionsecurityschema;
	private Text modversionclass;
	private Text modversioncategory;

	private Button officialButton;
	private Button xarUserApi;
	private Button xarUserGui;
	private Button xarAdminApi;
	private Button xarAdminGui;
	
	public XarayaModuleFilePage(String argument) {
		super(argument);
		setTitle(XarayaModuleMessages.getString("Xaraya.label.module"));
		setDescription(XarayaModuleMessages.getString("Xaraya.label.details"));
		setPageComplete(false);
	}

	public void createControl(Composite parent) {
	    // create the composite to hold the widgets
		GridData gd;
		Composite composite =  new Composite(parent, SWT.NULL);
	    // create the desired layout for this wizard page
		GridLayout gl = new GridLayout();
		int ncol = 4;
		gl.numColumns = ncol;
		composite.setLayout(gl);
		Group detailsgrp = new Group(composite, SWT.NULL);
		detailsgrp.setText(XarayaModuleMessages.getString("Xaraya.label.detailsgrp")/*"Module details"*/);
		detailsgrp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		detailsgrp.setLayout(new GridLayout());

		new Label (detailsgrp, SWT.NONE).setText(XarayaModuleMessages.getString("Xaraya.label.modversionid")/*"modversionid"*/);				
		modversionid = new Text(detailsgrp, SWT.BORDER);
		modversionid.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		modversionid.addListener(SWT.KeyUp, this);
		modversionid.setFocus();
		new Label (detailsgrp, SWT.NONE).setText(XarayaModuleMessages.getString("Xaraya.label.modversionversion")/*"modversionversion"*/);				
		modversionversion = new Text(detailsgrp, SWT.BORDER);
		modversionversion.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		modversionversion.addListener(SWT.KeyUp, this);
		new Label (detailsgrp, SWT.NONE).setText(XarayaModuleMessages.getString("Xaraya.label.modversionDescription")/*"modversionDescription"*/);				
		modversiondescription = new Text(detailsgrp, SWT.BORDER);
		modversiondescription.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		modversiondescription.addListener(SWT.KeyUp, this);
	//	new Label (detailsgrp, SWT.NONE).setText(XarayaModuleMessages.getString("Xaraya.label.modversionofficial")/*"modversionofficial"*/);				
	//	modversionofficial = new Text(detailsgrp, SWT.BORDER);
	//	modversionofficial.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
	//	modversionofficial.addListener(SWT.KeyUp, this);
		new Label (detailsgrp, SWT.NONE).setText(XarayaModuleMessages.getString("Xaraya.label.modversionauthor")/*"modversionauthor"*/);				
		modversionauthor = new Text(detailsgrp, SWT.BORDER);
		modversionauthor.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		modversionauthor.addListener(SWT.KeyUp, this);
		new Label (detailsgrp, SWT.NONE).setText(XarayaModuleMessages.getString("Xaraya.label.modversioncontact")/*"modversioncontact"*/);				
		modversioncontact = new Text(detailsgrp, SWT.BORDER);
		modversioncontact.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		modversioncontact.addListener(SWT.KeyUp, this);
		new Label (detailsgrp, SWT.NONE).setText(XarayaModuleMessages.getString("Xaraya.label.modversionsecurityschema")/*"modversionsecurityschema"*/);				
		modversionsecurityschema = new Text(detailsgrp, SWT.BORDER);
		modversionsecurityschema.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		modversionsecurityschema.addListener(SWT.KeyUp, this);
		new Label (detailsgrp, SWT.NONE).setText(XarayaModuleMessages.getString("Xaraya.label.modversionclass")/*"modversionclass"*/);				
		modversionclass = new Text(detailsgrp, SWT.BORDER);
		modversionclass.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		modversionclass.addListener(SWT.KeyUp, this);
		new Label (detailsgrp, SWT.NONE).setText(XarayaModuleMessages.getString("Xaraya.label.modversioncategory")/*"modversioncategory"*/);				
		modversioncategory = new Text(detailsgrp, SWT.BORDER);
		modversioncategory.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		modversioncategory.addListener(SWT.KeyUp, this);
		
		Composite compo = new Composite(composite, SWT.NONE);
		gl = new GridLayout();
		ncol = 1;
		gl.numColumns = ncol;
		compo.setLayout(gl);
		
		//Group officialgrp = new Group(composite, SWT.NONE);
		Group officialgrp = new Group(compo, SWT.NONE);
		officialgrp.setText(XarayaModuleMessages.getString("Xaraya.label.officialgrp")/*"Module details"*/);
		officialgrp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		officialgrp.setLayout(new GridLayout());

		officialButton = new Button(officialgrp, SWT.CHECK);
		officialButton.setText(XarayaModuleMessages.getString("Xaraya.label.official"));
		gd = new GridData(GridData.FILL_HORIZONTAL);
		//gd.horizontalSpan = ncol;
		officialButton.setLayoutData(gd);
		officialButton.setSelection(false);
		
		//Group autoGensgrp = new Group(composite, SWT.NONE);
		Group autoGensgrp = new Group(compo, SWT.NONE);
		autoGensgrp.setText(XarayaModuleMessages.getString("Xaraya.label.autoGensgrp"));
		autoGensgrp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		autoGensgrp.setLayout(new GridLayout());

		xarUserApi = new Button(autoGensgrp, SWT.CHECK);
		xarUserApi.setText(XarayaModuleMessages.getString("Xaraya.label.xarUserApi"));
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = ncol;
		xarUserApi.setLayoutData(gd);
		xarUserApi.setSelection(false);
		xarUserGui = new Button(autoGensgrp, SWT.CHECK);
		xarUserGui.setText(XarayaModuleMessages.getString("Xaraya.label.xarUserGui"));
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = ncol;
		xarUserGui.setLayoutData(gd);
		xarUserGui.setSelection(false);
		xarAdminApi = new Button(autoGensgrp, SWT.CHECK);
		xarAdminApi.setText(XarayaModuleMessages.getString("Xaraya.label.xarAdminApi"));
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = ncol;
		xarAdminApi.setLayoutData(gd);
		xarAdminApi.setSelection(false);
		xarAdminGui = new Button(autoGensgrp, SWT.CHECK);
		xarAdminGui.setText(XarayaModuleMessages.getString("Xaraya.label.xarAdminGui"));
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = ncol;
		xarAdminGui.setLayoutData(gd);
		xarAdminGui.setSelection(false);

	    // set the composite as the control for this page
		setControl(composite);		
		addListeners();
	}
	
	private void addListeners()
	{
		officialButton.addListener(SWT.Selection, this);
		xarUserApi.addListener(SWT.Selection, this);
		xarUserGui.addListener(SWT.Selection, this);
		xarAdminApi.addListener(SWT.Selection, this);
		xarAdminGui.addListener(SWT.Selection, this);
	}

	public void handleEvent(Event event) {
		setPageComplete(validatePage());
	}

//	public boolean canFlipToNextPage()
//	{
//		if (getErrorMessage() != null) return false;
//		if (isPageComplete() && validatePage()) {
//			saveDataToModel();
//			return true;
//		} 
//		return false;
//	}
	public boolean canFinish(){
		if (isPageComplete() /*&& validatePage()*/) {
			saveDataToModel();
			return true;
		} 
		return false;		
	}



	private boolean validateText(Text input){
		String buf = input.getText();
		if ((buf!=null) 
			&&  (buf.trim().length() >0)) return true;
		else return false;	
	}
	public boolean isNumeric(Text input) {
		//there is something in the field to check..
		StringBuffer buf = new StringBuffer(input.getText());
		for (int i=0; i < buf.length(); i++){
			if (!( Character.isDigit(buf.charAt(i)) //is it a number
				|| (buf.charAt(i) == '.')  //is it a decimal point
				|| (buf.charAt(i) == '_'))) //is it an under_score (incase of 1_2)
				return false;
		}
		return true;
	}
			
	private boolean validatePage() 	{
		StringBuffer buf = new StringBuffer();
		if (!validateText(modversionid)) buf.append("Id field incomplete");
		else if (!isNumeric(modversionid)) buf.append("Id field must be numeric");
		else if (!validateText(modversionversion)) buf.append("Version field incomplete");
		else if (!isNumeric(modversionversion)) buf.append("Version must be numeric");
		else if (!validateText(modversiondescription)) buf.append("Description field incomplete");
		else if (!validateText(modversionauthor)) buf.append("Author field incomplete");
		else if (!validateText(modversioncontact)) buf.append("Contact field incomplete");
		else if (!validateText(modversionsecurityschema)) buf.append("Security schema field incomplete");
		else if (!validateText(modversionclass)) buf.append("Class field incomplete");
		else if (!validateText(modversioncategory)) buf.append("Category field incomplete");
		if (buf.length() == 0) {
			//setErrorMessage("Module ready for creation");
			setErrorMessage(null); 
			return true;
		} 	
		setErrorMessage(buf.toString());
		return false;		
	}

	public void saveDataToModel()
	{
		XarayaVersionModel.setModversionid(modversionid.getText());
		XarayaVersionModel.setModversionversion(modversionversion.getText());
		XarayaVersionModel.setModversiondescription(modversiondescription.getText());
		XarayaVersionModel.setModversionauthor(modversionauthor.getText());
		XarayaVersionModel.setModversioncontact(modversioncontact.getText());
		XarayaVersionModel.setModversionsecurityschema(modversionsecurityschema.getText());
		XarayaVersionModel.setModversionclass(modversionclass.getText());
		XarayaVersionModel.setModversioncategory(modversioncategory.getText());
		XarayaVersionModel.setUserApi(xarUserApi.getSelection());
		XarayaVersionModel.setUserGui(xarUserGui.getSelection());
		XarayaVersionModel.setAdminApi(xarAdminApi.getSelection());
		XarayaVersionModel.setAdminGui(xarAdminGui.getSelection());
		XarayaVersionModel.setModversionofficial(
			(officialButton.getSelection()) ? "1" : "0");
		XarayaVersionModel.setModversionadmin(
			(xarAdminGui.getSelection()) ? "1" : "0" );
		XarayaVersionModel.setModversionuser( 
			(xarUserGui.getSelection()) ? "1" : "0"	);
		}
}