/*
 * Copyright 2012 s_wolff.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aegee.runanddine.util.forms;

import java.io.Serializable;
import java.util.List;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.IChoiceRenderer;

/**
 * Class displaying a selections of model instances.
 *
 * @author s_wolff
 * @param <T> type of the object that backends the
 * <code>Selection</code> instance. This is the type of the object that is
 * modified by a
 * <code>PropertyModel</code>
 * @param <R> type of the objects that are dispalyed as choices
 */
public class Selection<T extends Serializable, R extends Serializable> extends AbstractFormField<T> {

    /**
     * Creates a drop down selection. The drop down menu displays a list of
     * values that the the field with the name
     * <code>fieldName</code> of the provided object
     * <code>obj</code> is set to.
     *
     * @param id the component markup id
     * @param obj the object thats field specified by
     * <code>fieldName</code> will be set to the selected value
     * @param objectList a list of possible choices for the drop down selection
     * @param fieldName the name of the field that shall be set to the selected
     * value
     * @param labelText the label of that form field
     * @param required whether this field is required
     */
    public Selection(String id, T obj, List<R> objectList, String fieldName, String labelText, boolean required) {
        super(id, obj, createComponent(objectList), fieldName, labelText, required);
    }

    /**
     * Creates a drop down selection. The drop down menu displays a list of
     * values that the the field with the name
     * <code>fieldName</code> of the provided object
     * <code>obj</code> is set to.
     *
     * @param id the component markup id
     * @param obj the object thats field specified by
     * <code>fieldName</code> will be set to the selected value
     * @param objectList a list of possible choices for the drop down selection
     * @param fieldName the name of the field that shall be set to the selected
     * value
     * @param labelText the label of that form field
     * @param required whether this field is required
     * @param formatter a object that formats the displayed values of the provided list
     */
    public Selection(String id, T obj, List<R> objectList, String fieldName, String labelText, boolean required, SelectionValueFormatter<R> formatter) {
        super(id, obj, createComponent(objectList, formatter), fieldName, labelText, required);
    }

    private static <R extends Serializable> FormComponent createComponent(List<R> list) {
        return createComponent(list, new SelectionValueFormatter<R>() {

            public String format(R obj) {
                return obj.toString();
            }
        });
    }

    private static <R extends Serializable> FormComponent createComponent(List<R> list, final SelectionValueFormatter<R> test) {
        DropDownChoice<R> dropDownChoice = new DropDownChoice<R>(AbstractFormField.ID_INPUT, list, new IChoiceRenderer<R>() {

            public String getDisplayValue(R object) {
                return test.format(object);
            }

            public String getIdValue(R object, int index) {
                return "" + object.hashCode();
            }
        });
        return dropDownChoice;
    }

    protected String getDisplayValue(R obj) {
        return obj.toString();
    }
}