package eu.dnetlib.ui.gwt.client.relpage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

import eu.dnetlib.ui.gwt.client.DorotyServiceAsync;
import eu.dnetlib.ui.gwt.client.DorotyUIClient;
import eu.dnetlib.ui.gwt.client.util.DorotyPage;
import eu.dnetlib.ui.gwt.client.util.DragList;
import eu.dnetlib.ui.gwt.client.util.DragWidget;
import eu.dnetlib.ui.gwt.client.util.SetWidgetDropController;


public class RelationPage extends AbsolutePanel implements DorotyPage {
	private PickupDragController dragController; 

	public RelationPage() {
		super();
		dragController = new PickupDragController(this, false);
		dragController.setBehaviorMultipleSelection(false);
		dragController.setBehaviorDragProxy(false);
		dragController.setBehaviorConstrainedToBoundaryPanel(true);
	}

	private List<DragWidget> generateList(List<?> list) {
		List<DragWidget> res = new ArrayList<DragWidget>();
		for (Object o : list) {
			DragWidget dw = new DragWidget("<fieldset>" + o + "</fieldset>", o.getClass());
			res.add(dw);
		}
		return res;
	}

	private class DragRelationBox  extends AbsolutePanel {
		private static final int DEFAULT_BOX_SIZE = 200;
		private SimplePanel leftPanel = new SimplePanel();
		private SimplePanel centerPanel = new SimplePanel();
		private SimplePanel rightPanel = new SimplePanel();

		private FlexTable table = new FlexTable();

		public DragRelationBox(Class<?> cl1, Class<?> cl2, PickupDragController dragController) {
			super();


			leftPanel.setPixelSize(DEFAULT_BOX_SIZE, DEFAULT_BOX_SIZE);
			centerPanel.setPixelSize(DEFAULT_BOX_SIZE, DEFAULT_BOX_SIZE);
			rightPanel.setPixelSize(DEFAULT_BOX_SIZE, DEFAULT_BOX_SIZE);

			table.setWidget(0, 0, leftPanel);
			table.setWidget(0, 1, centerPanel);
			table.setWidget(0, 2,rightPanel);

			table.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasAlignment.ALIGN_CENTER);
			table.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasAlignment.ALIGN_CENTER);
			table.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasAlignment.ALIGN_CENTER);
			table.getFlexCellFormatter().setVerticalAlignment(0, 0, HasAlignment.ALIGN_BOTTOM);
			table.getFlexCellFormatter().setVerticalAlignment(0, 1, HasAlignment.ALIGN_BOTTOM);
			table.getFlexCellFormatter().setVerticalAlignment(0, 2, HasAlignment.ALIGN_BOTTOM);

			add(table);
			table.setBorderWidth(1);

			leftPanel.setWidget(new HTML("<i>Drag here a<br /><b>" + cl1 + "</b></i>"));
			rightPanel.setWidget(new HTML("<i>Drag here a<br /><b>" + cl2 + "</b></i>"));
			centerPanel.setWidget(new Image("img/rel_arrow.png"));
			
			SetWidgetDropController dropControllerLeft = new SetWidgetDropController(leftPanel, cl1, true);
			dragController.registerDropController(dropControllerLeft);
			SetWidgetDropController dropControllerRight = new SetWidgetDropController(rightPanel, cl2, true);
			dragController.registerDropController(dropControllerRight);
		}
	}

	@Override
	public String getHistoryToken() {
		return "addrel";
	}



	@Override
	public String getMenuItem() {
		return "Add relations";
	}



	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void refresh() {
		clear();
		FlexTable mainTable = new FlexTable();
		mainTable.setBorderWidth(0);
		mainTable.setCellSpacing(10);
		
		setPixelSize(1000, 2000);

		final ScrollPanel sc1 = new ScrollPanel();
		final ScrollPanel sc2 = new ScrollPanel();
		sc1.setStyleName("box");
		sc2.setStyleName("box");
		
		mainTable.setWidget(0, 0, sc1);
		mainTable.setWidget(0, 1, sc2);
		mainTable.setWidget(1, 0, new DragRelationBox(List.class, Map.class, dragController));

		mainTable.getFlexCellFormatter().setColSpan(1, 0 ,2);
		mainTable.getFlexCellFormatter().setWidth(0, 0, "50%");
		mainTable.getFlexCellFormatter().setWidth(0, 1, "50%");
		mainTable.getFlexCellFormatter().setVerticalAlignment(0, 0, HasAlignment.ALIGN_TOP);
		mainTable.getFlexCellFormatter().setVerticalAlignment(0, 1, HasAlignment.ALIGN_TOP);
		mainTable.getFlexCellFormatter().setVerticalAlignment(1, 0, HasAlignment.ALIGN_TOP);
		mainTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasAlignment.ALIGN_LEFT);
		mainTable.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasAlignment.ALIGN_LEFT);
		mainTable.getFlexCellFormatter().setHorizontalAlignment(1, 0, HasAlignment.ALIGN_CENTER);

		add(mainTable);
		
		DorotyUIClient.dorotyService.listAtoms(new AsyncCallback<List<String>>() {
			@Override
			public void onSuccess(List<String> list) {
				sc1.setWidget(new DragList(generateList(list), dragController));
			}
			
			@Override
			public void onFailure(Throwable arg0) {
				Window.alert("ERROR 1: " + arg0.getMessage());
			}
		});		
		
		DorotyUIClient.dorotyService.listAtoms(new AsyncCallback<List<String>>() {
			@Override
			public void onSuccess(List<String> list) {
				sc2.setWidget(new DragList(generateList(list), dragController));
			}
			
			@Override
			public void onFailure(Throwable arg0) {
				Window.alert("ERROR 2: " + arg0.getMessage());
			}
		});		
		
		


		
	}
}
