package org.csanchez.jenkins.plugins.kubernetes;

import hudson.Extension;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.model.Label;
import hudson.model.labels.LabelAtom;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import com.google.common.base.Preconditions;
import org.csanchez.jenkins.plugins.kubernetes.PodVolumes.PodVolume;

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

/**
 * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
 */
public class PodTemplate extends AbstractDescribableImpl<PodTemplate> {

    private String name;

    private final String image;

    private boolean privileged;

    private String command;

    private String args;

    private String remoteFs;

    private int instanceCap;

    private String label;

    private String nodeSelector;

    private final List<PodVolume> volumes;

    private final List<PodEnvVar> envVars;

    @DataBoundConstructor
    public PodTemplate(String image, List<? extends PodVolume> volumes, List<PodEnvVar> envVars) {
        Preconditions.checkArgument(!StringUtils.isBlank(image));
        this.image = image;
        this.volumes = (volumes == null) ? new ArrayList<PodVolume>() :
                new ArrayList<PodVolume>(volumes);
        this.envVars = (envVars == null) ? new ArrayList<PodEnvVar>() :
                new ArrayList<PodEnvVar>(envVars);
    }

    @DataBoundSetter
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String getImage() {
        return image;
    }

    @DataBoundSetter
    public void setCommand(String command) {
        this.command = command;
    }

    public String getCommand() {
        return command;
    }

    @DataBoundSetter
    public void setArgs(String args) {
        this.args = args;
    }

    public String getArgs() {
        return args;
    }

    public String getDisplayName() {
        return "Kubernetes Pod Template";
    }

    @DataBoundSetter
    public void setRemoteFs(String remoteFs) {
        this.remoteFs = StringUtils.isBlank(remoteFs) ? "/home/jenkins" : remoteFs;
    }

    public String getRemoteFs() {
        return remoteFs;
    }

    public void setInstanceCap(int instanceCap) {
        this.instanceCap = instanceCap;
    }

    public int getInstanceCap() {
        return instanceCap;
    }

    @DataBoundSetter
    public void setInstanceCapStr(String instanceCapStr) {
        if ("".equals(instanceCapStr)) {
            setInstanceCap(Integer.MAX_VALUE);
        } else {
            setInstanceCap(Integer.parseInt(instanceCapStr));
        }
    }

    public String getInstanceCapStr() {
        if (getInstanceCap() == Integer.MAX_VALUE) {
            return "";
        } else {
            return String.valueOf(instanceCap);
        }
    }

    public Set<LabelAtom> getLabelSet() {
        return Label.parse(label);
    }

    @DataBoundSetter
    public void setLabel(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

    @DataBoundSetter
    public void setNodeSelector(String nodeSelector) {
        this.nodeSelector = nodeSelector;
    }

    public String getNodeSelector() {
        return nodeSelector;
    }

    @DataBoundSetter
    public void setPrivileged(boolean privileged) {
        this.privileged = privileged;
    }

    public boolean isPrivileged() {
        return privileged;
    }

    public List<PodEnvVar> getEnvVars() {
        return envVars;
    }

    @Extension
    public static class DescriptorImpl extends Descriptor<PodTemplate> {

        @Override
        public String getDisplayName() {
            return "Kubernetes Pod Template";
        }
    }

    public List<PodVolume> getVolumes() {
        return volumes;
    }
}

@@ -1045,16 +1045,45 @@
  envVars
+ = new ArrayList%3CPodEnvVar%3E()
 ;%0A%0A    @
@@ -1177,33 +1177,8 @@
 umes
-, List%3CPodEnvVar%3E envVars
 ) %7B%0A
@@ -1398,131 +1398,8 @@
 s);%0A
-        this.envVars = (envVars == null) ? new ArrayList%3CPodEnvVar%3E() :%0A                new ArrayList%3CPodEnvVar%3E(envVars);%0A
     
@@ -3568,24 +3568,144 @@
 ars;%0A    %7D%0A%0A
+    @DataBoundSetter%0A    public void setEnvVars(List%3CPodEnvVar%3E envVars) %7B%0A        this.envVars.addAll(envVars);%0A    %7D%0A%0A
     @Extensi

Use @DataBoundSetter for passing env vars into the pod template.