package authoritymanager.client;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

public class DataSerial implements Serializable {
	private static final long serialVersionUID = -5367070944283145276L;
	private String itemType ;
	private Map<String, Collection> fields ;
	
	public DataSerial() {
		fields = new HashMap<String, Collection>(3) ;		
	}
	
	public DataSerial(Map<String, Collection> fields) {
		super();
		this.fields = fields;
	}

	public void setType(String itemType) {
		this.itemType = itemType ;
	}
	public String getType() {
		return this.itemType ;
	}
	public Collection<String> keySet() {
		return fields.keySet() ;
	}
	public Collection getPath(String path) {
		
		if (path.contains("|")) {
			String [] st = path.split("|") ;
			Collection result = new Vector(1) ;
			for (int i = 0 ; i < st.length ; i ++) {
				result.addAll(getPath(st [i].trim())) ;
			}
			return result ;
		}
		int index = path.indexOf(".") ;
		if (index == -1) {
			return fields.get(path) ;
		}
		else {
			Collection result = new Vector(1) ;
			String first = path.substring(0, index) ;
			String last = path.substring(index + 1) ;
			Collection col = get(first) ;
			if (col != null) {
				for (Iterator it = col.iterator() ; it.hasNext() ; ) {
					Object val = it.next() ;
					if (val instanceof String) {
						result.add(val) ;
					}
					if (val instanceof DataSerial) {
						result.addAll(((DataSerial) val).getPath(last)) ;
					}
				}
			}
			return result ;
		}
	}
	public Collection<String> getFields() {
		return fields.keySet() ;
	}
	public Collection get(String key) {
		return fields.get(key) ;
	}
	public void put(String key, Object value) {
		if (fields.containsKey(key)) {
			fields.get(key).add(value) ;
		}
		else {
			Collection values = new Vector(1) ;
			values.add(value) ;
			fields.put(key, values) ;
		}
	}
	/*
	public DataSerial(HashMap<String, Object> values) {
		fields = new HashMap<String, Object>() ;
		Set<String> keys = values.keySet() ;
		for (Iterator<String> it = keys.iterator() ; it.hasNext() ; ) {
			String key = it.next() ;
			Object value = values.get(key) ;
			if (value instanceof String) {
				putValue(key, value) ;
			}
			if (value instanceof ElementData) {
				putValue(key, new DataSerial(((ElementData) value).data)) ;
			}
			if (value instanceof Collection) {
				for (Iterator itVal = ((Collection) value).iterator() ; itVal.hasNext() ; ) {
					Object curVal = itVal.next() ;
					if (value instanceof String) {
						putValue(key, value) ;
					}
					if (value instanceof ElementData) {
						putValue(key, new DataSerial(((ElementData) value).data)) ;
					}
				}
			}
			
		}
	}
	*/
	
	public String toString() {
		String res = "" ;
		Set<String> keys = fields.keySet() ;
		for (Iterator<String> it = keys.iterator() ; it.hasNext() ; ) {
			String key = it.next() ;
			Collection col = fields.get(key) ;
			res += key + ": {" ;
			boolean start = true ;
			for (Iterator itValue = col.iterator() ; itValue.hasNext() ; ) {
				Object value = itValue.next() ;
				if (start) start = false ;
				else res += ", " ;
				if (value instanceof String) {					
					res += (String) value ;
				}
				if (value instanceof DataSerial) {
					res += (DataSerial) value ;
				}
			}
			res += "}\n" ;
			
		}
		return res ;
	}

}
