package org.sakaiproject.gradebookng.tool.panels;

import java.util.Map;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.sakaiproject.gradebookng.business.model.GbStudentNameSortOrder;
import org.sakaiproject.gradebookng.tool.pages.GradebookPage;

/**
 * 
 * Cell panel for the student name and eid. Link shows the student grade summary
 * 
 * @author Steve Swinsburg (steve.swinsburg@gmail.com)
 *
 */
public class StudentNameCellPanel extends Panel {

	private static final long serialVersionUID = 1L;
		
	IModel<Map<String,Object>> model;

	public StudentNameCellPanel(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();
		String eid = (String) modelData.get("eid");
		String firstName = (String) modelData.get("firstName");
		String lastName = (String) modelData.get("lastName");
		GbStudentNameSortOrder nameSortOrder = (GbStudentNameSortOrder) modelData.get("nameSortOrder");
		
		
		//link
		AjaxLink<String> link = new AjaxLink<String>("link") {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				
				GradebookPage gradebookPage = (GradebookPage) this.getPage();
				final ModalWindow window = gradebookPage.getStudentGradeSummaryWindow();

				window.setOutputMarkupId(true);

				window.setContent(new StudentGradeSummaryPanel(window.getContentId(), model, window));
				window.show(target);

				target.appendJavaScript("new GradebookGradeSummary($(\"#"+window.getMarkupId()+"\"));");
			}
			
		};
		
		//name label
		link.add(new Label("name", getFormattedStudentName(firstName, lastName, nameSortOrder)));
		
		//eid label, configurable
		link.add(new Label("eid", eid){
		
			private static final long serialVersionUID = 1L;

			public boolean isVisible() {
				return true; //TODO use config, will need to be passed in the model map
			}
			
		});
		
		add(link);
		
		getParent().add(new AttributeModifier("scope", "row"));
		getParent().add(new AttributeModifier("role", "rowheader"));

	}
	
	
	/**
	 * Helper to format a student name based on the sort type.
	 * 
	 * Sorted by Last Name = Smith, John (jsmith26)
   	 * Sorted by First Name = John Smith (jsmith26)
   	 * 
	 * @param firstName
	 * @param lastName
	 * @param sortOrder
	 * @return
	 */
	private String getFormattedStudentName(String firstName, String lastName, GbStudentNameSortOrder sortOrder) {
		
		String msg = "formatter.studentname." + sortOrder.name();
		if(GbStudentNameSortOrder.LAST_NAME == sortOrder) {
			return String.format(getString(msg), lastName, firstName);
		}
		if(GbStudentNameSortOrder.FIRST_NAME == sortOrder) {
			return String.format(getString(msg), firstName, lastName);
		}
		return firstName;
	}
	
	
	
}

@@ -107,24 +107,60 @@
 teModifier;%0A
+import org.apache.wicket.Component;%0A
 import org.a
@@ -1782,63 +1782,28 @@
 %09%09%09%09
-window.setOutputMarkupId(true);%0A%0A%09%09%09%09window.setC
+Component c
 ontent
-(
+ = 
 new 
@@ -1868,36 +1868,263 @@
 dow)
-);%0A%09%09%09%09window.show(target);%0A
+;%0A%0A%09%09%09%09if (window.isShown() && window.isVisible()) %7B%0A%09%09%09%09%09window.replace(content);%0A%09%09%09%09%09content.setVisible(true);%0A%09%09%09%09%09target.add(content);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09window.setContent(content);%0A%09%09%09%09%09window.show(target);%0A%09%09%09%09%7D%0A%0A%09%09%09%09content.setOutputMarkupId(true);
 %0A%09%09%09
@@ -2182,22 +2182,23 @@
 ($(%5C%22#%22+
-window
+content
 .getMark
@@ -2401,20 +2401,19 @@
 d%22, eid)
+ 
 %7B%0A
-%09%09
 %0A%09%09%09priv
@@ -2567,27 +2567,24 @@
 el map%0A%09%09%09%7D%0A
-%09%09%09
 %0A%09%09%7D);%0A%09%09%0A%09%09

Replace studentGradeSummaryWindow content when next/previous student clicked rather than opening a new popup