package org.gcube.portlets.user.reportgenerator.client.dialog;

import java.util.LinkedList;
import java.util.List;

import org.gcube.portlets.d4sreporting.common.client.CommonConstants;
import org.gcube.portlets.d4sreporting.common.shared.TimeSeriesFilter;
import org.gcube.portlets.d4sreporting.common.shared.TimeSeriesinfo;
import org.gcube.portlets.d4sreporting.common.shared.RepTimeSeries;
import org.gcube.portlets.user.reportgenerator.client.targets.TSArea;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author massi 
 *
 */
public class TimeSeriesFilterDialog extends DialogBox {
	ScrollPanel scroller = new ScrollPanel();
	TextBox from = new TextBox();
	TextBox to = new TextBox();
	FlexTable flexTable = new FlexTable();

	List<TSHeader> compoundHeaders = new LinkedList<TSHeader>(); 
	/**
	 * 
	 * @param sts a ts
	 * @param caller .
	 */
	public TimeSeriesFilterDialog(final TSArea caller, final RepTimeSeries sts) {

		final TimeSeriesinfo droppedTS = sts.getTsMetadata();

		setText(droppedTS.getTitle() + " Filter");
		String name = droppedTS.getTitle();
		long rowsNo = droppedTS.getDimension();


		Grid metadata = new Grid(2, 2);
		metadata.setWidget(0, 0, new HTML("Name:", true));
		metadata.setWidget(0, 1, new HTML(name));
		metadata.setWidget(1, 0, new HTML("Total rows : ", true));
		metadata.setWidget(1, 1, new HTML(""+rowsNo));


		final VerticalPanel main_panel = new VerticalPanel();
		main_panel.addStyleName("bgBlank p8 font_family font_12");

		main_panel.add(metadata);			

		List<String> headersString = droppedTS.getHeaderLabels();

		/**
		 */
		for (int i = 0; i < headersString.size(); i++) {
			CheckBox toAdd = new CheckBox(); 
			toAdd.setChecked(true);
			compoundHeaders.add(new TSHeader(this, flexTable, toAdd, i, new HTML(headersString.get(i) )));			
		}


		main_panel.add(new HTML("Selected fields: (click to remove)"));

		flexTable = getTableHeader(compoundHeaders);
		main_panel.add(flexTable);			

		main_panel.add(new HTML("Select rows interval:"));

		from.setText("1");
		to.setText("10");
		to.setMaxLength(6);
		from.setMaxLength(6);
		from.addChangeListener(textBoxListener);
		to.addChangeListener(textBoxListener);


		Grid rowsInterval = new Grid(1, 4);
		rowsInterval.setWidget(0, 0, new HTML("From row:"));
		rowsInterval.setWidget(0, 1, from);
		rowsInterval.setWidget(0, 2, new HTML("&nbsp;&nbsp;To row:"));
		rowsInterval.setWidget(0, 3, to);

		main_panel.add(rowsInterval);

		main_panel.add(new HTML("<hr align=\"left\" size=\"1\" width=\"100%\" color=\"gray\" noshade>"));
		HorizontalPanel buttonspanel = new HorizontalPanel();
		buttonspanel.setHeight("50");
		buttonspanel.setSpacing(3);




		// PopupPanel is a SimplePanel, so you have to set it's widget property to
		// whatever you want its contents to be.
		Button close = new Button("Close");
		close.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();			
			}    	  
		});

		Button apply = new Button("Apply");
		apply.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				List<TSHeader> selectedCols = compoundHeaders;

				List<Integer> colsToShow = new LinkedList<Integer>();

				for (TSHeader head : selectedCols) {
					colsToShow.add(new Integer(head.getMyColNo()));
				}
				int fromInt = Integer.parseInt(from.getText());
				int toInt = Integer.parseInt(to.getText());	

				TimeSeriesFilter toSet = new TimeSeriesFilter(colsToShow, null, null, fromInt, toInt);
				caller.setNewFilter(toSet);
				caller.refreshHeaders();				
				hide();

			}    	  
		});

		buttonspanel.add(close);
		buttonspanel.add(apply);
		main_panel.add(buttonspanel);

		scroller.setPixelSize(550, 280);
		main_panel.setPixelSize(550, 260);

		scroller.add(main_panel);
		setWidget(scroller);		
	}


	/**
	 * 
	 * @param toRemove .
	 * @return .
	 */
	public boolean removeHeader(TSHeader toRemove) {
		return compoundHeaders.remove(toRemove);
	}


	/**
	 * display the header of the ts
	 * @param droppedTS
	 * @return
	 */
	private FlexTable getTableHeader(List<TSHeader> headers) {
		flexTable.clear();
		int n = headers.size();	

		for (int i = 0; i < n ; i++) {
			flexTable.getCellFormatter().setStyleName(0, i, "timeSeries_header");
			flexTable.setWidget(0, i, headers.get(i));
			flexTable.getCellFormatter().setStyleName(1, i, "timeSeries_td");
			flexTable.setWidget(1, i, new HTML("..."));
		}

		return flexTable;
	}

	ChangeListener textBoxListener = new ChangeListener() {

		public void onChange(Widget sender) {
			TextBox tb = (TextBox) sender;
			String text = tb.getText();
			String checkedText = text;
			text = text.replaceAll(CommonConstants.ACCEPTED_CHARS_JUST_NUM, "");
			if (! text.equals(checkedText) ) {
				Window.alert("Only numbers are accepted");
				tb.setText(text);
			}
			int start = Integer.parseInt(from.getText());
			int end = Integer.parseInt(to.getText());

			if (start >= end) {
				Window.alert("From must be greater than to");
				to.setText(""+(start+10));
			}

		}


	};
	/**
	 * 
	 */
	public void show() {
		super.show();
		center();
	}

}
