package org.sakaiproject.gradebookng.tool.panels;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.extensions.ajax.markup.html.AjaxEditableLabel;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.sakaiproject.gradebookng.business.GradebookNgBusinessService;
import org.sakaiproject.gradebookng.tool.model.GradeInfo;
import org.sakaiproject.gradebookng.tool.model.StudentGradeInfo;

import java.util.Map;

/**
 * The panel for the cell of a grade item
 * 
 * @author Steve Swinsburg (steve.swinsburg@gmail.com)
 *
 */
public class GradeItemCellPanel extends Panel {

	private static final long serialVersionUID = 1L;
	
	@SpringBean(name="org.sakaiproject.gradebookng.business.GradebookNgBusinessService")
	protected GradebookNgBusinessService businessService;

	//todo wrap this in a model as per http://wicket.apache.org/guide/guide/bestpractices.html#bestpractices_6
	public GradeItemCellPanel(String id, final Long assignmentId, final StudentGradeInfo studentGrades) {
		super(id);
		
		//TODO harden this
		GradeInfo gradeInfo = studentGrades.getGrades().get(assignmentId);
						
		AjaxEditableLabel<String> grade = new AjaxEditableLabel<String>("grade", new Model<String>(gradeInfo.getGrade())) {
			
			private static final long serialVersionUID = 1L;
			

			@Override
			protected String defaultNullLabel() {
				return "-"; //TODO this is temporary, they need something to click
			}
			
			@Override
			protected void onSubmit(final AjaxRequestTarget target) {
				super.onSubmit(target);
				String newGrade = this.getEditor().getValue();
				
				
				
				System.out.println("newGrade: " + newGrade);
				
				boolean result = businessService.saveGrade(assignmentId, studentGrades.getStudentUuid(), newGrade);
				System.out.println("result: " + result);

			}
			
			@Override
			protected void updateLabelAjaxAttributes(AjaxRequestAttributes attributes) {
				//when switching from editor to label
				Map<String,Object> extraParameters = attributes.getExtraParameters();
				extraParameters.put("assignmentId", assignmentId);
				extraParameters.put("studentUuid", studentGrades.getStudentUuid());
			}
			
			@Override
			protected void updateEditorAjaxAttributes(AjaxRequestAttributes attributes) {
				//when switching from label to editor
				Map<String,Object> extraParameters = attributes.getExtraParameters();
				extraParameters.put("assignmentId", assignmentId);
				extraParameters.put("studentUuid", studentGrades.getStudentUuid());
			}
			
			
		};
		
		
		add(grade);
		
		//TODO since we are using a custom column panel here we still need this to be editable
		
		//menu
		

	}

}

@@ -44,16 +44,60 @@
 anels;%0A%0A
+import org.apache.commons.lang.StringUtils;%0A
 import o
@@ -724,16 +724,74 @@
 eInfo;%0A%0A
+import java.text.SimpleDateFormat;%0Aimport java.util.Date;%0A
 import j
@@ -1473,24 +1473,107 @@
 ignmentId);%0A
+%09%09%0A%09%09//get grade%0A%09%09String formattedGrade = this.formatGrade(gradeInfo.getGrade());%0A
 %09%09%09%09%09%09%0A%09%09Aja
@@ -1660,36 +1660,30 @@
 String%3E(
-gradeInfo.get
+formatted
 Grade
-()
 )) %7B%0A%09%09%09
@@ -3085,12 +3085,195 @@
 u%0A%09%09%0A%0A%09%7D
+%0A%09%0A%09/**%0A%09 * Format a grade to remove the .0 if present.%0A%09 * @param grade%0A%09 * @return%0A%09 */%0A%09private String formatGrade(String grade) %7B%0A%09%09return StringUtils.removeEnd(grade, %22.0%22);%09%09%0A%09%7D
 %0A%0A%7D%0A

Format the grade to remove .0