package lemming.ui;

import lemming.data.Tuple;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.Loop;
import org.apache.wicket.markup.html.list.LoopItem;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import java.util.ArrayList;
import java.util.List;

/**
 * A dropdown panel based on Bootstrap’s dropdown button.
 *
 * @param <T> data type
 */
public class DropdownButtonPanel<T> extends Panel {
    /**
     * Label of the button.
     */
    private String buttonLabel;

    /**
     * String representation of the select event.
     */
    private String event;

    /**
     * Hidden text field the selected value is applied to.
     */
    private TextField<String> hiddenTextField;

    /**
     * Labels of the dropdown list.
     */
    private ArrayList<String> labels;

    /**
     * Values of the dropdown list.
     */
    private ArrayList<String> values;

    /**
     * Creates a dropdown button panel.
     *
     * @param buttonLabel label of the button
     * @param hiddenTextField hiddent text field
     * @param data labels and values of the dropdown list
     */
    public DropdownButtonPanel(String buttonLabel, TextField<String> hiddenTextField,
                               ArrayList<Tuple<String, String>> data) {
        super("dropdownButtonPanel");
        this.buttonLabel = buttonLabel;
        this.hiddenTextField = hiddenTextField;

        for (Tuple<String, String> tuple : data) {
            this.labels.add(tuple.getKey());
            this.values.add(tuple.getValue());
        }
    }

    /**
     * Creates a dropdown button panel from a columns list.
     *
     * @param buttonLabel label of the button
     * @param hiddenTextField hiddent text field
     * @param columns columns of type IColumn
     */
    public DropdownButtonPanel(String buttonLabel, TextField<String> hiddenTextField,
                               List<IColumn<T, String>> columns) {
        super("dropdownButtonPanel");
        this.buttonLabel = buttonLabel;
        this.hiddenTextField = hiddenTextField;
        this.labels = new ArrayList<>();
        this.values = new ArrayList<>();

        for (IColumn<T, String> column : columns) {
            if (column instanceof AbstractColumn) {
                String label = ((AbstractColumn<T, String>) column).getDisplayModel().getObject();
                String value = column.getSortProperty();

                if (label != null && !label.isEmpty()) {
                    labels.add(label);
                    values.add(value);
                }
            }
        }
    }

    /**
     * Sets the event that is fired on select.
     *
     * @param event select event
     */
    public void setSelectEvent(String event) {
        this.event = event;
    }

    /**
     * Called when a dropdown button panel is configured.
     */
    @Override
    protected void onConfigure() {
        super.onConfigure();

        Button button = new Button("button");
        IModel<String> labelModel = Model.of(buttonLabel);
        Label label = new Label("buttonLabel", labelModel);

        add(new AttributeModifier("class", "input-group-btn"));
        button.add(label.setOutputMarkupId(true));
        add(button);
        add(new Loop("list", labels.size()) {
            @Override
            protected void populateItem(LoopItem item) {
                item.add(new AjaxLink<T>("link") {
                    @Override
                    public void onClick(AjaxRequestTarget target) {
                        hiddenTextField.getModel().setObject(values.get(item.getIndex()));
                        labelModel.setObject(labels.get(item.getIndex()));

                        target.add(hiddenTextField);
                        target.add(label);

                        if (event != null) {
                            String javascript = String.format("jQuery('#%s').trigger('input');",
                                    hiddenTextField.getMarkupId());
                            target.appendJavaScript(javascript);
                        }
                    }
                }.add(new Label("label", labels.get(item.getIndex()))));
            }
        });
    }
}

@@ -1396,24 +1396,132 @@
 g%3E values;%0A%0A
+    /**%0A     * Initialized state set in onConfigure();%0A     */%0A    private Boolean wasInitialized = false;%0A%0A
     /**%0A    
@@ -3534,24 +3534,59 @@
 nfigure();%0A%0A
+        if (!wasInitialized) %7B%0A    
         Butt
@@ -3615,24 +3615,28 @@
 (%22button%22);%0A
+    
         IMod
@@ -3690,16 +3690,20 @@
         
+ 
+   
 Label la
@@ -3747,32 +3747,36 @@
 odel);%0A%0A        
+  
+  
 add(new Attribut
@@ -3811,24 +3811,28 @@
 oup-btn%22));%0A
+    
         butt
@@ -3874,24 +3874,28 @@
 ));%0A        
+    
 add(button);
@@ -3895,32 +3895,36 @@
 utton);%0A        
+ 
+   
 add(new Loop(%22li
@@ -3949,32 +3949,36 @@
 ) %7B%0A            
+    
 @Override%0A      
@@ -3963,32 +3963,36 @@
       @Override%0A
+    
             prot
@@ -4048,16 +4048,20 @@
         
+    
 item.add
@@ -4099,32 +4099,36 @@
                 
+    
 @Override%0A      
@@ -4137,24 +4137,28 @@
             
+   
+ 
 public void 
@@ -4189,24 +4189,28 @@
 t target) %7B%0A
+    
             
@@ -4308,24 +4308,28 @@
             
+    
 labelModel.s
@@ -4384,32 +4384,36 @@
                 
+  
+  
 target.add(hidde
@@ -4425,16 +4425,20 @@
 Field);%0A
+    
         
@@ -4493,24 +4493,28 @@
             
+    
 if (event !=
@@ -4546,24 +4546,28 @@
             
+ 
+   
 String javas
@@ -4651,32 +4651,36 @@
                 
+    
 hiddenTextField.
@@ -4691,24 +4691,28 @@
 arkupId());%0A
+    
             
@@ -4780,34 +4780,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -4812,32 +4812,36 @@
               %7D%0A
+    
                 
@@ -4901,32 +4901,36 @@
 ));%0A            
+    
 %7D%0A        %7D);%0A  
@@ -4927,11 +4927,61 @@
     
+ 
-%7D);
+   %7D);%0A%0A            wasInitialized = true;%0A        %7D
 %0A   

Check if the dropdown button panel was initialized before.