package authoritymanager.client;

import java.util.Collection;
import java.util.Iterator;
import java.util.Stack;
import java.util.Vector;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SearchDetailsPage extends Page {
	private DialogBox dlgDetails ;
	private FlexTable pnlDetails ;
	private AsyncCallback<DataSerial> callback ;
	String [] [] conf ;
	
	public SearchDetailsPage(AuthorityManagerView view) {
		super(view) ;
		conf = null ;
		dlgDetails = new DialogBox();
		dlgDetails.setPixelSize(400, 600);
		dlgDetails.setText("details");
		dlgDetails.setAnimationEnabled(true);
		dlgDetails.setModal(true);
		Button btnClose = new Button("Close", new ClickHandler() {
			public void onClick(ClickEvent event) {
				dlgDetails.hide();
			}
		});
		pnlDetails = new FlexTable();
		pnlDetails.setWidth("390px") ;
		pnlDetails.setCellSpacing(2) ;
		
		dlgDetails.add(pnlDetails);
		pnlDetails.setWidget(0, 0, btnClose);
		
		callback = new AsyncCallback<DataSerial>() {
			public void onFailure(Throwable caught) {}
			public void onSuccess(DataSerial result) {
				fillDetails(result) ;
			};
		} ;
	}
	
	private void fillDetails(DataSerial result) {
		if (conf == null) {
			return ;
		}
		for (int iRow = pnlDetails.getRowCount() - 1 ; iRow > 0 ; iRow --) {
			pnlDetails.removeRow(iRow) ;
		}
		Stack<Collection<DataSerial>> stack = new Stack<Collection<DataSerial>>() ;
		Vector<DataSerial> vector = new Vector<DataSerial>(1) ;
		vector.add(result) ;
		stack.add(vector) ;
		int iRow = 1, iCol = 0;
		pnlDetails.setText(iRow, iCol, "") ;
		for (int i = 1 ; i < conf.length ; i ++) {
			for (int j = 0 ; j < conf [i].length ; j ++) {
				String [] st = conf [i] [j].split("#") ;
				for (int iSt = 0 ; iSt < st.length ; iSt ++) {
					String current = st [iSt] ;
					if (current.length() == 0) {
						continue ;
					}
					if (current.equals("\n")) {
						iRow ++ ;
						iCol = 0 ;
						pnlDetails.setText(iRow, iCol, "") ;
					}
					else {
						if (current.charAt(0) == '@') {
							String fieldName = current.substring(1) ;
							if (fieldName.length() == 0) {
								stack.pop() ;
							}
							else {
								Collection<DataSerial> collection = stack.peek() ;
								if (collection.size() > 0) {

									Collection<DataSerial> nextCollection = new Vector<DataSerial>(2) ;
								for (Iterator<DataSerial> it = collection.iterator() ; it.hasNext() ; ) {
									DataSerial curData = it.next() ;
									Collection<?> colNext = curData.get(fieldName) ;
									if (colNext != null) {
										if (colNext.iterator().next() instanceof DataSerial) {
											nextCollection.addAll((Collection<DataSerial>) colNext) ;
										}
									}
								}
								
								stack.push(nextCollection) ;
								}
							}
						}
						else {
							if (current.charAt(0) == '$') {
								String fieldName = current.substring(1) ;
								if (fieldName.length() == 0) {
									continue ;
								}
								Collection<DataSerial> collection = stack.peek() ;
								for (Iterator<DataSerial> itData = collection.iterator() ; itData.hasNext() ; ) {
									DataSerial currentData = itData.next() ;
									Collection<String> values = (Collection<String>) currentData.get(fieldName) ;
									if (values != null) {

										for (Iterator<String> itValues = values.iterator() ; itValues.hasNext() ; ) {
											String curValue = itValues.next() ;
											pnlDetails.setText(iRow, iCol, pnlDetails.getText(iRow, iCol) + " " + curValue) ;
										}
									}									
								}
								
							}
							else {
								
								pnlDetails.setText(iRow, iCol, pnlDetails.getText(iRow, iCol) + current) ;
							}
						}
					}		
				}
			}
		}
	}
	public void show(String itemType, String itemID) {
		conf = ClientConfiguration.getSearchDetailView(itemType) ;
		view.getController().getItem(itemType, itemID, callback) ;
		dlgDetails.center() ;
	}
	
	public void setDetails(String itemType, String itemID) {
		
	}
}
