package org.sakaiproject.gradebookng.tool.panels;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.extensions.markup.html.form.DateTextField;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.sakaiproject.gradebookng.business.GradebookNgBusinessService;
import org.sakaiproject.service.gradebook.shared.Assignment;
import org.sakaiproject.service.gradebook.shared.CategoryDefinition;

import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

/**
 * The panel for the add grade item window
 * @author Steve Swinsburg (steve.swinsburg@gmail.com)
 *
 */
public class AddGradeItemPanelContent extends Panel {

	private static final long serialVersionUID = 1L;

    @SpringBean(name="org.sakaiproject.gradebookng.business.GradebookNgBusinessService")
    protected GradebookNgBusinessService businessService;
  
    public AddGradeItemPanelContent(String id, Model<Assignment> assignment) {
        super(id, assignment);

        add(new TextField<String>("title", new PropertyModel<String>(assignment, "name")));
        add(new TextField<Double>("points", new PropertyModel<Double>(assignment, "points")));
        add(new DateTextField("duedate", new PropertyModel<Date>(assignment, "dueDate"), "MM/dd/yyyy")); //TODO needs to come from i18n

        List<CategoryDefinition> categories = businessService.getGradebookCategories();

        final Map<Long, String> categoryMap = new HashMap<>();
        for (CategoryDefinition category : categories) {
            categoryMap.put(category.getId(), category.getName());
        }

        DropDownChoice<Long> categoryDropDown = new DropDownChoice<Long>("category", new PropertyModel<Long>(assignment, "categoryId"), new ArrayList<Long>(categoryMap.keySet()), new IChoiceRenderer<Long>() {
			private static final long serialVersionUID = 1L;

			public Object getDisplayValue(Long value) {
                return categoryMap.get(value);
            }

            public String getIdValue(Long object, int index) {
                return object.toString();
            }
        });
        categoryDropDown.setNullValid(true);
        add(categoryDropDown);

        add(new CheckBox("extraCredit", new PropertyModel<Boolean>(assignment, "extraCredit")));
       
        final AjaxCheckBox released = new AjaxCheckBox("released", new PropertyModel<Boolean>(assignment, "released")) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				//nothing required
			}
        };
        released.setOutputMarkupId(true);
        add(released);
        
        //if checked, release must also be checked
        final AjaxCheckBox counted = new AjaxCheckBox("counted", new PropertyModel<Boolean>(assignment, "counted")) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
				if(this.getModelObject()) {
					released.setModelObject(true);
					target.add(released);
				}
			}
        };
        
        add(counted);
        
    }
}

@@ -3141,16 +3141,34 @@
  checked
+ and then disabled
 %0A       
@@ -3472,16 +3472,99 @@
 );%0A%09%09%09%09%09
+released.setEnabled(false);%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09released.setEnabled(true);%0A%09%09%09%09%7D%0A%09%09%09%09
 target.a
@@ -3576,22 +3576,16 @@
 leased);
-%0A%09%09%09%09%7D
 %0A%09%09%09%7D%0A  

Fix up the link between the 'include item in course grade' and 'release item to students' so that you cannot deselect the 'release item' if the 'include in course grade' checkbox is checked.