package authoritymanager.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.xml.client.Attr;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.NamedNodeMap;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

public class Utilities {
	public static char BIG_DELIMITER	=	(char) 161 ;
	public static char DELIMITER		=	(char) 162 ;
	public static char SMALL_DELIMITER	=	(char) 163 ;
	
	private static void startMainScreen() {
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(String result) {
				Label lblXML = new Label("<html>");
				// Label lblXML = new Label("Result: " + result) ;
				// pnlMain.add(lblXML) ;

				Document doc = XMLParser.parse(result);

				Element root = doc.getDocumentElement();
				visit(lblXML, null, root);
			}
		};
		AuthorityManagerController controller = new AuthorityManagerController();
		controller.getConfiguration(callback);

	}

	public static String getWidgetContent(Widget widget) {
		String res = "";
		if (widget instanceof TextBox) {
			TextBox txtWidget = (TextBox) widget;
			res += txtWidget.getName() + ":" + txtWidget.getText() + "$";
		}
		if (widget instanceof ListBox) {
			ListBox lstWidget = (ListBox) widget;
			res += lstWidget.getName() + ":"
					+ lstWidget.getItemText(lstWidget.getSelectedIndex()) + "$";
		}
		if (widget instanceof ComplexPanel) {
			res += getPanelContent((ComplexPanel) widget);
		}
		if (widget instanceof FlexTable) {
			FlexTable tblWidget = (FlexTable) widget;
			for (int iRow = 0; iRow < tblWidget.getRowCount(); iRow++) {
				for (int iCol = 0; iCol < tblWidget.getCellCount(iRow); iCol++) {
					Widget nextWidget = tblWidget.getWidget(iRow, iCol);
					if (nextWidget != null) {
						res += getWidgetContent(nextWidget);
					}
				}

			}
		}
		return res;
	}

	public static String getPanelContent(ComplexPanel panel) {
		String res = "";
		for (int iWidget = 0; iWidget < panel.getWidgetCount(); iWidget++) {
			Widget widget = panel.getWidget(iWidget);
			res += getWidgetContent(widget);
		}
		return res;
	}

	static String nl = "<br>";

	private static void visit(Label label, Element prev, Element node) {
		if (prev != null) {
			String s = label.getText() + nl;
			s += "Element " + prev.getTagName() + " has element: " + nl;
			label.setText(s);
		}
		if (node.hasAttributes()) {
			String s = label.getText() + nl;
			s += "Element " + node.getPrefix() + " * " + node.getNodeName()
					+ " has attributes: " + nl;
			NamedNodeMap attributes = node.getAttributes();
			for (int i = 0; i < attributes.getLength(); i++) {
				Attr attribute = (Attr) attributes.item(i);
				s += "Attribute: " + attribute.getName() + " with value: "
						+ attribute.getValue();
			}
			label.setText(s);
		}
		String s = label.getText() + nl + "ELEMENT NODE: " + node.getNodeName();
		NodeList children = node.getChildNodes();
		s += " has " + children.getLength() + " children." + nl;
		label.setText(s);

		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Element.ELEMENT_NODE) {
				visit(label, node, (Element) child);
			} else {
				s = label.getText() + nl + "ELEMENT NODE: "
						+ node.getNodeName() + " has text: ";
				String val = child.getNodeValue().trim();
				if (val.length() > 0) {
					s += val;
					label.setText(s);
				}
			}
		}
	}

	public void loadXML() {
		XMLExample xml = new XMLExample();
		/*
		 * Viewport viewport = new Viewport() ; viewport.setBorders(true) ;
		 * viewport.add(xml) ;
		 * 
		 * //VerticalPanel pnl = new VerticalPanel() ; //pnl.add(xml) ;
		 * RootPanel.get().add(viewport) ;
		 */
	}

	public static String getWidgetValue(Widget widget) {
		if (widget.getClass() == TextBox.class) {
			return ((TextBox) widget).getText();
		}
		if (widget.getClass() == ListBox.class) {
			int iSelected = ((ListBox) widget).getSelectedIndex();
			return ((ListBox) widget).getItemText(iSelected);
		}
		return null;
	}

	public static void setWidget( String [] conf, FlexTable table) {
		for (int iConf = 0 ; iConf < conf.length ; iConf ++) {
			setWidget(conf [iConf], table) ;
		}
	}
	public static void setWidget(String conf, FlexTable table) {
		String[] st = conf.split(":");

		String title = st.length > 0 ? st[0] : "";
		String type = st.length > 1 ? st[1] : "";
		String path = st.length > 2 ? st[2] : "";
		String name = st.length > 3 ? st[3] : "";
		String position = st.length > 4 ? st[4] : "newline";

		int nrow = table.getRowCount();
		int ncol = nrow > 0 ? table.getCellCount(nrow - 1) : 0;
		if (position.equals("newline")) {
			ncol = 0;
		}
		if (position.equals("sameline")) {
			nrow--;
			if (nrow < 0) nrow = 0 ;
		}

		QueryWidget widget = new QueryWidget() ;
		widget.setName(name) ;
		widget.setPath(path) ;
		if (type.startsWith("label")) {
			widget.setWidget(new Label(title));
			((Label) widget.getWidget()).setWordWrap(false);
		}
		if (type.startsWith("editbox")) {
			widget.setWidget(new TextBox());
		}
		if (type.startsWith("listbox")) {
			widget.setWidget(new ListBox());
			String set = type.substring(type.indexOf('{') + 1, type
					.indexOf('}'));
			String[] stset = set.split(",");
			for (int iSt = 0; iSt < stset.length; iSt++) {
				((ListBox) widget.getWidget()).addItem(stset[iSt].trim());
			}
		}
		
		table.setWidget(nrow, ncol++, widget);
		table.getCellFormatter().setAlignment(nrow, ncol - 1,
				HasHorizontalAlignment.ALIGN_RIGHT,
				HasVerticalAlignment.ALIGN_MIDDLE);
	}
}
