package org.sagebionetworks.web.client.widget.entity;

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

import org.gwtbootstrap3.client.ui.Anchor;
import org.gwtbootstrap3.client.ui.ButtonGroup;
import org.gwtbootstrap3.client.ui.InlineRadio;
import org.gwtbootstrap3.client.ui.Panel;
import org.gwtbootstrap3.client.ui.constants.IconType;
import org.gwtbootstrap3.client.ui.html.Div;
import org.sagebionetworks.evaluation.model.Evaluation;
import org.sagebionetworks.web.client.DisplayUtils;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class EvaluationListViewImpl extends Panel implements EvaluationListView {
	List<InlineRadio> evaluationCheckboxes;
	
	private Presenter presenter;
	
	@Inject
	public EvaluationListViewImpl() {
		evaluationCheckboxes = new ArrayList<InlineRadio>();
	}
	
	@Override
	public void configure(List<Evaluation> list) {
		clear();
		
		if(list == null || list.size() == 0){
			addNoAttachmentRow();
		} else {
			populateTable(list);			
		}
	}

	private void addNoAttachmentRow() {
		add(new InlineHTML("No evaluations found"));
	}
	
	private void populateTable(List<Evaluation> list) {		
		ButtonGroup group = new ButtonGroup();
		for(final Evaluation data: list){
			Div row = new Div();
			final InlineRadio selectBox = new InlineRadio("evaluationButtons", data.getName());
			selectBox.addStyleName("margin-left-10");
			row.add(selectBox);
			evaluationCheckboxes.add(selectBox);
			if (DisplayUtils.isDefined(data.getSubmissionInstructionsMessage())) {
				Anchor moreInfoButton = new Anchor();
				moreInfoButton.setIcon(IconType.INFO_CIRCLE);
				moreInfoButton.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						DisplayUtils.showInfoDialog(data.getName(), data.getSubmissionInstructionsMessage(), null);
					}
				});
				moreInfoButton.addStyleName("margin-left-10 greyText-imp");
				row.add(moreInfoButton);
			}
			group.add(row);
		}
		add(group);
	}

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

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void showLoading() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void clear() {
		super.clear();
		evaluationCheckboxes.clear();
	}

	@Override
	public void showInfo(String title, String message) {
		DisplayUtils.showInfo(title, message);
	}

	@Override
	public void showErrorMessage(String message) {
		DisplayUtils.showErrorMessage(message);
	}

	@Override
	public Integer getSelectedEvaluationIndex() {
		for (int i = 0; i < evaluationCheckboxes.size(); i++) {
			if (evaluationCheckboxes.get(i).getValue()) {
				return i;
			}
		}
		return null;
	}
	@Override
	public void setSelectedEvaluationIndex(int i) {
		evaluationCheckboxes.get(i).setValue(true, true);
	}
}

@@ -104,51 +104,8 @@
 t;%0A%0A
-import org.gwtbootstrap3.client.ui.Anchor;%0A
 impo
@@ -242,63 +242,8 @@
 el;%0A
-import org.gwtbootstrap3.client.ui.constants.IconType;%0A
 impo
@@ -395,60 +395,66 @@
 ls;%0A
-%0A
 import 
-com.google.gwt.event.dom.client.ClickEven
+org.sagebionetworks.web.client.widget.HelpWidge
 t;%0A
+%0A
 impo
@@ -475,37 +475,37 @@
 gwt.
-event.dom.client.ClickHandler
+safehtml.shared.SafeHtmlUtils
 ;%0Aim
@@ -1597,345 +1597,168 @@
 %09%09%09%09
-Anchor moreInfoButton = new Anchor();%0A%09%09%09%09moreInfoButton.setIcon(IconType.INFO_CIRCLE);%0A%09%09%09%09moreInfoButton.addClickHandler(new ClickHandler() %7B%0A%09%09%09%09%09@Override%0A%09%09%09%09%09public void onClick(ClickEvent event) %7B%0A%09%09%09%09%09%09DisplayUtils.showInfoDialog(data.getName(), data.getSubmissionInstructionsMessage(), null);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D);%0A%09%09%09%09moreInfoButton.a
+HelpWidget helpWidget = new HelpWidget();%0A%09%09%09%09helpWidget.setHelpMarkdown(SafeHtmlUtils.htmlEscape(data.getSubmissionInstructionsMessage()));%0A%09%09%09%09helpWidget.setA
 ddSt
@@ -1756,32 +1756,33 @@
 .setAddStyleName
+s
 (%22margin-left-10
@@ -1814,22 +1814,29 @@
 add(
-moreInfoButton
+helpWidget.asWidget()
 );%0A%09

use HelpWidgets in the evaluation submitter dialog (to show SubmissionInstructionsMessage)