package authoritymanager.client;

import java.util.Collection;
import java.util.Date;
import java.util.Vector;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Timer;
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.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SearchResultsPage extends Page {
	public static final int RESULTS_DELTA = 20;

	private SearchResults results;
	private FlexTable tblResults;
	private Button btnPrevResults;
	private Button btnNextResults;
	private HandlerRegistration hndlPrevResults;
	private HandlerRegistration hndlNextResults;
	private Button btnClose;
	private ArrayWindow arrayWindow;
	private Button [] btnDetails, btnEdit ;
	private HandlerRegistration [] hndDetails, hndEdit ;
	private String [] id ;
	
	private Label lblStatus ;
	
	public SearchResultsPage(AuthorityManagerView authorityManagerView) {
		super(authorityManagerView);
		results = null;

		tblResults = new FlexTable();
		// tblResults.setBorderWidth(2);
		// tblResults.setWidth("600px");

		add(tblResults);

		btnPrevResults = new Button("&#171;");
		btnNextResults = new Button("&#187;");
		btnClose = new Button("Close", new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (results != null) {
					results.clear() ;
				}
				view.showSearchPage();
			}
		});
		HorizontalPanel pnlButtons = new HorizontalPanel();
		pnlButtons.setSpacing(2);
		// pnlButtons.setBorderWidth(2);
		pnlButtons.add(btnPrevResults);
		pnlButtons.add(btnClose);
		pnlButtons.add(btnNextResults);
		add(pnlButtons);
		
		btnDetails = new Button [RESULTS_DELTA] ;
		hndDetails = new HandlerRegistration [RESULTS_DELTA] ;
		btnEdit = new Button [RESULTS_DELTA] ;
		hndEdit = new HandlerRegistration [RESULTS_DELTA] ;
		id = new String [RESULTS_DELTA] ;
		for (int i = 0 ; i < RESULTS_DELTA ; i ++) {
			btnDetails [i] = new Button("details") ;
			btnDetails [i].setSize("60px", "25px");
			hndDetails [i] = btnDetails [i].addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {}}) ;
			btnEdit [i] = new Button("edit") ;
			btnEdit [i].setSize("60px", "25px");
			hndEdit [i] = btnEdit [i].addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {}}) ;
		}
		lblStatus = new Label() ;
		add(lblStatus) ;
		
		arrayWindow = new ArrayWindow();
	}

	private void addStatus(String status) {
		lblStatus.setText(lblStatus.getText() + "  " + status) ;
	}
	public void setResults(SearchResults results) {
		this.results = results;

		if (results == null) {
			return ;
		}
		if (results.getResults().size() > 0) {
			arrayWindow.setWindow(0, results.getResults().size() - 1);
			fillResults(RESULTS_DELTA);
		}
		addStatus("results.size = " + results.getResults().size()) ;
	}

	private void fillResults(int resultsDelta) {
		Date date = new Date() ;
		long time = date.getTime() ;
		for (int iRow = tblResults.getRowCount() - 1; iRow >= 0; iRow--) {
			for (int iCol = 0; iCol < tblResults.getCellCount(iRow); iCol++) {
				tblResults.clearCell(iRow, iCol);
			}
			tblResults.removeRow(iRow);
		}
		addStatus("Clearing " + (date.getTime() - time)) ;
		time = date.getTime() ;
		Vector<String> fieldName = new Vector<String>(6);
		Vector<String> fieldTitle = new Vector<String>(6);
		Vector<String> fieldWidth = new Vector<String>(6);
		String[][] searchBasicView = ClientConfiguration
				.getSearchBasicView(results.getItemType());
		for (int iSearchBasicView = 1; iSearchBasicView < searchBasicView.length; iSearchBasicView++) {
			String[] currentView = searchBasicView[iSearchBasicView];
			for (int iCurrentView = 0; iCurrentView < currentView.length; iCurrentView++) {
				String[] st = currentView[iCurrentView].split(":");
				fieldName.add(st[0]);
				fieldTitle.add(st[1]);
				fieldWidth.add(st [2]) ;
				tblResults.setText(0, iCurrentView, st[1]);
				//tblResults.getCellFormatter().setWidth(0, iCurrentView, st[2]);
				tblResults.getCellFormatter().setWidth(0, iCurrentView, st[2]);
				tblResults.getCellFormatter().setStyleName(0, iCurrentView,
						"searchPage-column");
			}
		}
		addStatus("Setting title " + (date.getTime() - time)) ;
		time = date.getTime() ;
		int iRow = 1;
		for (int iResult = arrayWindow.getFirst(); iResult < (int) Math.min(
				arrayWindow.getFirst() + resultsDelta, results.getResults()
						.size()); iResult++) {
			final DataSerial curData = results.getResults().get(iResult);
			for (int iField = 0; iField < fieldName.size(); iField++) {
				Collection result = curData.getPath(fieldName.get(iField));
				if (result != null) {
					tblResults.setText(iRow, iField, (String) result.iterator()
							.next());
				} else {
					tblResults.setText(iRow, iField, "");
				}
				tblResults.getCellFormatter().setStyleName(iRow, iField,
						"searchPage-results");
				tblResults.getCellFormatter().setWordWrap(iRow, iField, true);
				tblResults.getCellFormatter().setWidth(iRow, iField, fieldWidth.get(iField)) ;
				tblResults.getCellFormatter().setHorizontalAlignment(iRow,
						iField, HasHorizontalAlignment.ALIGN_LEFT);
			}
			
			final String id =(String) curData.getPath(searchBasicView [0] [1]).iterator().next() ;
			hndDetails [iRow - 1].removeHandler() ;
			hndDetails [iRow - 1] = btnDetails [iRow - 1].addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					view.showSearchDetailsPage(results.getItemType(), id) ;
				}
			}) ;
			tblResults.setWidget(iRow, tblResults.getCellCount(0), btnDetails [iRow - 1]);
			tblResults.getCellFormatter().setHeight(iRow, searchBasicView.length, "30px");
			
			hndEdit [iRow - 1].removeHandler() ;
			hndEdit [iRow - 1] = btnEdit [iRow - 1].addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					view.showEditPage(id) ;					
				}
			}) ;
			tblResults.setWidget(iRow, tblResults.getCellCount(0) + 1, btnEdit [iRow - 1]);
			tblResults.getCellFormatter().setHeight(iRow,
					searchBasicView.length + 1, "30px");
			iRow++;

		}
		addStatus("Adding results " + (date.getTime() - time)) ;
		time = date.getTime() ;
		for (int i = 0 ; i < fieldWidth.size() ; i ++) {
			tblResults.getColumnFormatter().setWidth(i, fieldWidth.get(i)) ;
		}
		
		if (hndlPrevResults != null) {
			hndlPrevResults.removeHandler();
		}
		hndlPrevResults = btnPrevResults.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				arrayWindow.setFirst(Math.max(0, arrayWindow.getFirst()
						- RESULTS_DELTA));
				fillResults(RESULTS_DELTA);
			}
		});
		if (hndlNextResults != null) {
			hndlNextResults.removeHandler();
		}
		hndlNextResults = btnNextResults.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (arrayWindow.getFirst() + RESULTS_DELTA < arrayWindow
						.getLast()) {
					arrayWindow
							.setFirst(arrayWindow.getFirst() + RESULTS_DELTA);
					fillResults(RESULTS_DELTA);
				}
			}
		});
	}
};
