package com.openshift.jenkins.plugins.pipeline;

import com.openshift.jenkins.plugins.pipeline.model.IOpenShiftDeleterLabels;
import com.openshift.jenkins.plugins.pipeline.model.IOpenShiftPluginDescriptor;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Item;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.IOException;

public class OpenShiftDeleterLabels extends OpenShiftBaseStep implements
        IOpenShiftDeleterLabels {

    protected final String types;
    protected final String keys;
    protected final String values;

    // Fields in config.jelly must match the parameter names in the
    // "DataBoundConstructor"
    @DataBoundConstructor
    public OpenShiftDeleterLabels(String apiURL, String namespace,
            String authToken, String verbose, String types, String keys,
            String values) {
        super(apiURL, namespace, authToken, verbose);
        this.types = types != null ? types.trim() : null;
        this.keys = keys != null ? keys.trim() : null;
        this.values = values != null ? values.trim() : null;
    }

    // generically speaking, Jenkins will always pass in non-null field values.
    // However, as we have periodically
    // added new fields, jobs created with earlier versions of the plugin get
    // null for the new fields. Hence,
    // we have introduced the generic convention (even for fields that existed
    // in the initial incarnations of the plugin)
    // of insuring nulls are not returned for field getters
    public String getTypes() {
        return types;
    }

    public String getKeys() {
        return keys;
    }

    public String getValues() {
        return values;
    }

    // Overridden for better type safety.
    // If your plugin doesn't really define any property on Descriptor,
    // you don't have to do this.
    @Override
    public DescriptorImpl getDescriptor() {
        return (DescriptorImpl) super.getDescriptor();
    }

    /**
     * Descriptor for {@link OpenShiftCreator}. Used as a singleton. The class
     * is marked as public so that it can be accessed from views.
     *
     */
    @Extension
    // This indicates to Jenkins that this is an implementation of an extension
    // point.
    public static final class DescriptorImpl extends
            BuildStepDescriptor<Builder> implements IOpenShiftPluginDescriptor {
        /**
         * To persist global configuration information, simply store it in a
         * field and call save().
         *
         * <p>
         * If you don't want fields to be persisted, use <tt>transient</tt>.
         */

        /**
         * In order to load the persisted global configuration, you have to call
         * load() in the constructor.
         */
        public DescriptorImpl() {
            load();
        }

        public FormValidation doCheckType(@QueryParameter String value)
                throws IOException, ServletException {
            return ParamVerify.doCheckType(value);
        }

        public FormValidation doCheckKey(@QueryParameter String value)
                throws IOException, ServletException {
            return ParamVerify.doCheckKey(value);
        }

        public ListBoxModel doFillInputTypeItems(@AncestorInPath Item item,
                @QueryParameter String key, @QueryParameter String type) {
            ListBoxModel ret = new ListBoxModel();
            ret.add("Json contents", "json");
            ret.add("API Object type : API Object key", "kv");
            ret.add("Label", "label");
            return ret;
        }

        public boolean isApplicable(Class<? extends AbstractProject> aClass) {
            // Indicates that this builder can be used with all kinds of project
            // types
            return true;
        }

        /**
         * This human readable name is used in the configuration screen.
         */
        public String getDisplayName() {
            return DISPLAY_NAME;
        }

        @Override
        public boolean configure(StaplerRequest req, JSONObject formData)
                throws FormException {
            // To persist global configuration information,
            // pull info from formData, set appropriate instance field (which
            // should have a getter), and call save().
            save();
            return super.configure(req, formData);
        }

    }
}