/**
 * Copyright (c) 2010-2014, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 *  @author Victor Belov
 *  @since 1.4.0
 *
 */

package org.openhab.habdroid.model;

import java.util.ArrayList;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.util.Log;

/**
 * This class provides datasource for openHAB widgets from sitemap page.
 * It uses a sitemap page XML document to create a list of widgets
 */

public class OpenHABWidgetDataSource {
	private static final String TAG = "OpenHABWidgetDataSource";
	private OpenHABWidget rootWidget;
	private String title;
	private String id;
	private String icon;
	private String link;

	public OpenHABWidgetDataSource() {
		
	}
	
	public OpenHABWidgetDataSource(Node rootNode) {
		setSourceNode(rootNode);
	}
	
	public void setSourceNode(Node rootNode) {
		Log.i(TAG, "Loading new data");
		rootWidget = new OpenHABWidget();
		rootWidget.setType("root");
		if (rootNode.hasChildNodes()) {
			NodeList childNodes = rootNode.getChildNodes();
			for (int i = 0; i < childNodes.getLength(); i ++) {
				Node childNode = childNodes.item(i);
				if (childNode.getNodeName().equals("widget")) {
					new OpenHABWidget(rootWidget, childNode);
				} else if (childNode.getNodeName().equals("title")) {
					this.setTitle(childNode.getTextContent());
				} else if (childNode.getNodeName().equals("id")) {
					this.setId(childNode.getTextContent());
				} else if (childNode.getNodeName().equals("icon")) {
					this.setIcon(childNode.getTextContent());
				} else if (childNode.getNodeName().equals("link")) {
					this.setLink(childNode.getTextContent());
				}
			}
		}
	}
	
	public OpenHABWidget getRootWidget() {
		return this.rootWidget;
	}

	public OpenHABWidget getWidgetById(String widgetId) {
		ArrayList<OpenHABWidget> widgets = this.getWidgets();
		for (int i = 0; i < widgets.size(); i++) {
			if (widgets.get(i).getId().equals(widgetId))
				return widgets.get(i);
		}
		return null;
	}
	
	public ArrayList<OpenHABWidget> getWidgets() {
		ArrayList<OpenHABWidget> result = new ArrayList<OpenHABWidget>();
		if (rootWidget != null)
		if (this.rootWidget.hasChildren()) {
			for (int i = 0; i < rootWidget.getChildren().size(); i++) {
				OpenHABWidget openHABWidget = this.rootWidget.getChildren().get(i);
				result.add(openHABWidget);
				if (openHABWidget.hasChildren()) {
					for (int j = 0; j < openHABWidget.getChildren().size(); j++) {
						result.add(openHABWidget.getChildren().get(j));
					}
				}
			}
		}
		return result;
	}

    public ArrayList<OpenHABWidget> getLinkWidgets() {
        ArrayList<OpenHABWidget> result = new ArrayList<OpenHABWidget>();
        if (rootWidget != null)
            if (this.rootWidget.hasChildren()) {
                for (int i = 0; i < rootWidget.getChildren().size(); i++) {
                    OpenHABWidget openHABWidget = this.rootWidget.getChildren().get(i);
                    if (openHABWidget.hasLinkedPage() || openHABWidget.childrenHasLinkedPages())
                    result.add(openHABWidget);
                    if (openHABWidget.hasChildren()) {
                        for (int j = 0; j < openHABWidget.getChildren().size(); j++) {
                            if (openHABWidget.getChildren().get(j).hasLinkedPage())
                                result.add(openHABWidget.getChildren().get(j));
                        }
                    }
                }
            }
        return result;
    }

    public ArrayList<OpenHABWidget> getNonlinkWidgets() {
        ArrayList<OpenHABWidget> result = new ArrayList<OpenHABWidget>();
        if (rootWidget != null)
            if (this.rootWidget.hasChildren()) {
                for (int i = 0; i < rootWidget.getChildren().size(); i++) {
                    OpenHABWidget openHABWidget = this.rootWidget.getChildren().get(i);
                    if ((openHABWidget.getType().equals("Frame") && openHABWidget.childrenHasNonlinkedPages()) ||
                            (!openHABWidget.getType().equals("Frame") && !openHABWidget.hasLinkedPage()))
                        result.add(openHABWidget);
                    if (openHABWidget.hasChildren()) {
                        for (int j = 0; j < openHABWidget.getChildren().size(); j++) {
                            if (!openHABWidget.getChildren().get(j).hasLinkedPage())
                                result.add(openHABWidget.getChildren().get(j));
                        }
                    }
                }
            }
        return result;
    }


    public void logWidget(OpenHABWidget widget) {
		Log.i(TAG, "Widget <" + widget.getLabel() + "> (" + widget.getType() + ")");
		if (widget.hasChildren()) {
			for (int i = 0; i < widget.getChildren().size(); i++) {
				logWidget(widget.getChildren().get(i));
			}
		}
	}

	public String getTitle() {
		String[] splitString;
        if (title != null) {
    		splitString = title.split("\\[|\\]");
		    return splitString[0];
        }
        return "";
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}
	
}

@@ -5134,36 +5134,151 @@
 %22);%0A
-%09%09    return splitString%5B0%5D;
+            if (splitString.length%3E0) %7B%0A                return splitString%5B0%5D;%0A            %7D else %7B%0A                return title;%0A            %7D
 %0A   

Fix a bug in getTitle which led to an out of bound crash