package org.sagebionetworks.web.client.view.bootstrap.table;

import java.util.Locale;
import org.gwtbootstrap3.client.ui.html.Div;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.ui.RootPanel;

public class TableHeaderResizeGrip extends Div {
	public static Element curCol = null, nxtCol = null;
	public static int curColWidth = -1, nxtColWidth = -1, pageX = -1;
	// add MOUSE_MOVE_HANDLER and MOUSE_UP_HANDLER to Document!
	public static final MouseMoveHandler MOUSE_MOVE_HANDLER = event -> {
		if (curCol != null) {
			int diffX = event.getClientX() - pageX;
			if (nxtCol != null && diffX > 0) {
				updateWidth(nxtCol, nxtColWidth - diffX);
			}
			updateWidth(curCol, curColWidth + diffX);
		}
	};
	
	public static void updateWidth(Element thElement, int newWidth) {
		if (thElement == null) {
			return;
		}
		String newWidthPx = newWidth + "px;";
		// update the current table header element
		thElement.setAttribute("style", "width: " + newWidthPx + "min-width: " + newWidthPx);
		// we also need to update the style for every td for this column!
		// so, what index is this column?
		int colIndex = -1;
		Element prevSiblingElement = thElement;
		while( prevSiblingElement != null ) {
			colIndex++;
			prevSiblingElement = prevSiblingElement.getPreviousSiblingElement();
		}
		// find ALL <trs> in the table
		// td -> tr -> thead -> table
		Element tableElement = thElement.getParentElement().getParentElement().getParentElement();
		NodeList<Element> allTableRows = tableElement.getElementsByTagName("tr");
		for (int i = 0; i < allTableRows.getLength(); i++) {
			Element tr = allTableRows.getItem(i);
			Element cellElement = (Element)tr.getChild(colIndex);
			if ("td".equals(cellElement.getNodeName().toLowerCase(Locale.ROOT))) {
				cellElement.setAttribute("style", "max-width: " + newWidthPx );	
			}			
		}
	}
	
	public static final MouseUpHandler MOUSE_UP_HANDLER = event -> {
		curCol = null;
		nxtCol = null;
		pageX = -1;
		nxtColWidth = -1;
		curColWidth = -1;
	};
	
	static {
		RootPanel.get().addDomHandler(MOUSE_MOVE_HANDLER, MouseMoveEvent.getType());
		RootPanel.get().addDomHandler(MOUSE_UP_HANDLER, MouseUpEvent.getType());
	}
	
	public TableHeaderResizeGrip() {
		makeResizable();
	}

	public static final MouseDownHandler MOUSE_DOWN_HANDLER = event -> {
		// get the parent th element
		Div sourceDiv = (Div)event.getSource();
		curCol = sourceDiv.getParent().getElement();
		// and the next th element over
		nxtCol = curCol.getNextSiblingElement();
		pageX = event.getClientX();
		curColWidth = curCol.getOffsetWidth();
		if (nxtCol != null) {
			nxtColWidth = nxtCol.getOffsetWidth();
		}
	};
	
	public void makeResizable() {
		addStyleName("th-resizer");
		// get th and make position-relative
		addDomHandler(MOUSE_DOWN_HANDLER, MouseDownEvent.getType());
	}
}

@@ -59,33 +59,8 @@
 e;%0A%0A
-import java.util.Locale;%0A
 impo
@@ -1689,17 +1689,31 @@
 he table
+ under %3Ctbody%3E
 %0A
-
 %09%09// td 
@@ -1835,53 +1835,240 @@
 ;%0A%09%09
-NodeList%3CElement%3E allTableRows = tableElement
+// modify the max-width of the target %3Ctd%3E elements%0A%09%09NodeList%3CElement%3E tbodyElements = tableElement.getElementsByTagName(%22tbody%22);%0A%09%09if (tbodyElements.getLength() == 1) %7B%0A%09%09%09NodeList%3CElement%3E allTableRows = tbodyElements.getItem(0)
 .get
@@ -2092,16 +2092,17 @@
 (%22tr%22);%0A
+%09
 %09%09for (i
@@ -2148,16 +2148,17 @@
  i++) %7B%0A
+%09
 %09%09%09Eleme
@@ -2193,16 +2193,17 @@
 (i);%0A%09%09%09
+%09
 Element 
@@ -2252,82 +2252,8 @@
 x);%0A
-%09%09%09if (%22td%22.equals(cellElement.getNodeName().toLowerCase(Locale.ROOT))) %7B%0A
 %09%09%09%09
@@ -2321,15 +2321,12 @@
 );%09%0A
+
 %09%09%09%7D
-%09%09%09
 %0A%09%09%7D

remove string compare (since cell could have been a th or td, but can now only be td)