package org.sakaiproject.gradebookng.tool.panels;

import java.util.Arrays;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.sakaiproject.gradebookng.business.GradebookNgBusinessService;
import org.sakaiproject.gradebookng.business.model.GbStudentGradeInfo;
import org.sakaiproject.gradebookng.business.util.FormatHelper;
import org.sakaiproject.gradebookng.tool.model.ScoreChangedEvent;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 * 
 * Cell panel for the students average score in a category
 * 
 * @author Steve Swinsburg (steve.swinsburg@gmail.com)
 *
 */
public class CategoryColumnCellPanel extends Panel {

	private static final long serialVersionUID = 1L;

	@SpringBean(name="org.sakaiproject.gradebookng.business.GradebookNgBusinessService")
	protected GradebookNgBusinessService businessService;


	IModel<Map<String,Object>> model;

	public CategoryColumnCellPanel(String id, IModel<Map<String,Object>> model) {
		super(id, model);
		this.model = model;
	}
	
	@Override
	public void onInitialize() {
		super.onInitialize();
		
		//unpack model
		Map<String,Object> modelData = (Map<String,Object>) this.model.getObject();
		
		Double score = (Double) modelData.get("score");		
		final String studentUuid = (String) modelData.get("studentUuid");
		final Long categoryId = (Long) modelData.get("categoryId");
						
		//score label
		Label scoreLabel = new Label("score", Model.of(getCategoryScore(score))) {
			@Override
			public void onEvent(IEvent<?> event) {
				super.onEvent(event);
				if (event.getPayload() instanceof ScoreChangedEvent) {
					ScoreChangedEvent scoreChangedEvent = (ScoreChangedEvent) event.getPayload();
					if (studentUuid.equals(scoreChangedEvent.getStudentUuid()) && 
							categoryId.equals(scoreChangedEvent.getCategoryId())) {

						GbStudentGradeInfo studentGradeInfo = businessService.buildGradeMatrix(businessService.getGradebookAssignments(), Arrays.asList(new String[]{studentUuid}), null, null).get(0);
						Double categoryAverage = studentGradeInfo.getCategoryAverages().get(categoryId);
						String newCategoryAverage = (categoryAverage == null) ? getString("label.nocategoryscore") : FormatHelper.formatDoubleAsPercentage(categoryAverage);
						((Model<String>) getDefaultModel()).setObject(newCategoryAverage);
						scoreChangedEvent.getTarget().add(this);
					}
				}
			}
		};
		scoreLabel.setOutputMarkupId(true);
		add(scoreLabel);
		
		//accessibility
		getParent().add(new AttributeModifier("scope", "row"));
		getParent().add(new AttributeModifier("role", "rowheader"));

	}
	
	
	/**
	 * Helper to format a category score
	 * 
	 * The value is a double (ie 12.34) that needs to be formatted as a percentage with two decimal places precision.
	 * If null, it should return 'N/A' or equivalent translated string.
	 * 
	 * @param score
	 * @return 12.34% type string or N/A if null
	 */
	private String getCategoryScore(Double score) {
		
		if(score == null){
			return getString("label.nocategoryscore");
		}
		
		return FormatHelper.formatDoubleAsPercentage(score);
	}
}

@@ -469,16 +469,80 @@
 ervice;%0A
+import org.sakaiproject.gradebookng.business.model.GbGradeInfo;%0A
 import o
@@ -793,16 +793,77 @@
 ingBean;
+%0Aimport org.sakaiproject.service.gradebook.shared.Assignment;
 %0A%0A/**%0A *
@@ -2212,271 +2212,205 @@
 %09%09%09%09
-GbStudentGradeInfo studentGradeInfo = businessService.buildGradeMatrix(businessService.getGradebookAssignments(), Arrays.asList(new String%5B%5D%7BstudentUuid%7D), null, null).get(0);%0A%09%09%09%09%09%09Double categoryAverage = studentGradeInfo.getCategoryAverages().get(categoryId
+Map%3CAssignment, GbGradeInfo%3E grades = businessService.getGradesForStudent(studentUuid);%0A%09%09%09%09%09%09Double categoryAverage = businessService.getCategoryScoreForStudent(categoryId, studentUuid, grades
 );%0A
+%0A
 %09%09%09%09

Use new businessService.getCategoryScoreForStudent endpoint for calculating category score for student