package authoritymanager.client;

import java.util.Collection;

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.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ManageRecordPage extends Page {
	private Panel pnlDisplay ;
	private FlexTable tblDisplay ;
	private DataSerial winnerRecord, loserRecord ;
	private String winnerID, loserID ;
	
	public ManageRecordPage(AuthorityManagerView _view) {
		super(_view) ;
		
		this.winnerRecord = null ;
		this.loserRecord = null ;
		this.winnerID = null ;
		this.loserID = null ;
		
		pnlDisplay = new VerticalPanel() ;
		add(pnlDisplay) ;
		
		tblDisplay = new FlexTable() ;
		pnlDisplay.add(tblDisplay) ;
		
		Button btnMerge = new Button("Merge", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				AsyncCallback<Void> callback = new AsyncCallback<Void>() {
					@Override
					public void onFailure(Throwable caught) {
					}
					@Override
					public void onSuccess(Void result) {
						PopupPanel pnlResult = new PopupPanel(true) ;
						Label lblResult = new Label("Records " + winnerID + " and " + loserID + " merged sucessfully!") ;
						pnlResult.add(lblResult) ;
						pnlResult.center() ;
						view.showManagePage() ;
					}
				} ;
				view.getController().merge(winnerID, loserID, winnerRecord, callback) ;		
			}
		}) ;
		HorizontalPanel pnlButtons = new HorizontalPanel() ;
		pnlButtons.add(btnMerge) ;
		Button btnCancel = new Button("Cancel", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				view.showManagePage() ;
			}
		}) ;
		pnlButtons.add(btnCancel) ;
		
		pnlDisplay.add(pnlButtons) ;
	}
	
	public void setData() {
		tblDisplay.clear(true) ;
		tblDisplay.removeAllRows() ;
		if (winnerRecord == null || loserRecord == null) {
			return ;
		}
		Collection<String> winnerKeys = winnerRecord.keySet() ;
		int iRow = 0 ;
		for (final String winnerKey: winnerKeys) {
			Collection<?> winnerValues = winnerRecord.get(winnerKey) ;
			if (winnerValues != null) {
				for (final Object objValue: winnerValues) {
					tblDisplay.setText(iRow, 0, winnerKey) ;
					tblDisplay.setText(iRow, 1, objValue.toString()) ;
					Button btnAdd = new Button("*", new ClickHandler() {
						public void onClick(ClickEvent event) {
							winnerRecord.remove(winnerKey, objValue) ;
							loserRecord.put(winnerKey, objValue) ;
							setData() ;
						}
					}) ;
					tblDisplay.setWidget(iRow, 2, btnAdd) ;
					iRow ++ ;
				}
			}
		}
		iRow = 0;
		Collection<String> loserKeys = loserRecord.keySet() ;
		for (final String loserKey: loserKeys) {
			Collection<?> loserValues = loserRecord.get(loserKey) ;
			if (loserValues != null) {
				for (final Object objValue: loserValues) {
					tblDisplay.setText(iRow, 3, loserKey) ;
					tblDisplay.setText(iRow, 4, objValue.toString()) ;
					Button btnAdd = new Button("*", new ClickHandler() {
						public void onClick(ClickEvent event) {
							loserRecord.remove(loserKey, objValue) ;
							winnerRecord.put(loserKey, objValue) ;
							setData() ;
						}
					}) ;
					tblDisplay.setWidget(iRow, 5, btnAdd) ;
					iRow ++ ;
				}
			}
		}
		
		
	}
	public void setData(final DataSerial winnerRecord, final DataSerial loserRecord) {
		this.winnerRecord = winnerRecord ;
		this.loserRecord = loserRecord ;
		Collection values = winnerRecord.get("efgIdentifier") ;
		this.winnerID = values == null ? null : (String) values.iterator().next() ;
		values = loserRecord.get("efgIdentifier") ;
		this.loserID = values == null ? null : (String) values.iterator().next() ;
		setData() ;
		
	}
}
