package net.nemerosa.ontrack.jenkins;

import hudson.Extension;
import hudson.model.ParameterValue;
import hudson.model.StringParameterValue;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

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

public class OntrackMultiChoiceParameterDefinition extends AbstractOntrackMultipleParameterDefinition {

    @DataBoundConstructor
    public OntrackMultiChoiceParameterDefinition(String name, String description, String dsl, String valueProperty) {
        super(name, description, dsl, valueProperty);
    }

    @Override
    public ParameterValue createValue(StaplerRequest req, JSONObject jo) {
        JSONArray jsonArray = (JSONArray)jo.get("value");
        List<String> choices = getChoices();
        List<String> selectionList = new ArrayList<>();
        for(int i=0;i<choices.size();i++){
            String choice = choices.get(i);
            if((Boolean)jsonArray.get(i)){
                selectionList.add(choice);
            }
        }
        String selections = StringUtils.join(selectionList, ',');
        StringParameterValue value = new StringParameterValue(getName(), selections, getDescription());
        return value;
    }

    @Override
//    @Deprecated ???
    public ParameterValue createValue(StaplerRequest req) {
        String[] value = req.getParameterValues(getName());
        String selections = StringUtils.join(value, ',');
        return new StringParameterValue(getName(), selections, getDescription());
    }

    @Override
    public StringParameterValue getDefaultParameterValue() {
        return new StringParameterValue(getName(), "", getDescription());
    }

    @Extension
    public static class DescriptorImpl extends ParameterDescriptor {

        @Override
        public String getDisplayName() {
            return "Ontrack: Multi Parameter choice";
        }

        @Override
        public String getHelpFile() {
            return "/help/ontrack/parameter-choice.html";
        }
    }
}

@@ -796,41 +796,24 @@
     
-JSONArray jsonArray = (JSONArray)
+Object object = 
 jo.g
@@ -938,22 +938,383 @@
     
-for(int i=0;i%3C
+if(object instanceof Boolean && !choices.isEmpty())%7B%0A            Boolean selected = (Boolean) object;%0A            String firstOption = choices.get(0);%0A            if(selected)%7B%0A                selectionList.add(firstOption);%0A            %7D%0A        %7D else if (object instanceof JSONArray) %7B%0A            JSONArray jsonArray = (JSONArray) object;%0A            for (int i = 0; i %3C 
 choi
@@ -1328,14 +1328,20 @@
 e();
+ 
 i++)
+ 
 %7B%0A
+    
     
@@ -1392,18 +1392,23 @@
         
+    
 if
+ 
 ((Boolea
@@ -1409,16 +1409,17 @@
 Boolean)
+ 
 jsonArra
@@ -1427,18 +1427,23 @@
 .get(i))
+ 
 %7B%0A
+    
         
@@ -1477,16 +1477,34 @@
 hoice);%0A
+                %7D%0A
         

support Boolean as well as JSONArray of Booleans as input