package org.jenkinsci.plugins.practitest;
import hudson.Launcher;
import hudson.Extension;
import hudson.FilePath;
import hudson.util.FormValidation;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.Result;
import hudson.model.TaskListener;
import hudson.tasks.Builder;
import hudson.tasks.BuildStepDescriptor;
import hudson.util.ListBoxModel;
import hudson.util.ListBoxModel.Option;
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.QueryParameter;

import java.util.HashMap;
import java.util.Map;

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

/**
 * Sample {@link Builder}.
 *
 * <p>
 * When the user configures the project and enables this builder,
 * {@link DescriptorImpl#newInstance(StaplerRequest)} is invoked
 * and a new {@link BuildToTestRun} is created. The created
 * instance is persisted to the project configuration XML by using
 * XStream, so this allows you to use instance fields (like )
 * to remember the configuration.
 *
 * <p>
 * When a build is performed, the {@link #perform} method will be invoked.
 */
public class BuildToTestRun extends Builder implements SimpleBuildStep {

    private final String instanceUrl;

    @DataBoundConstructor
    public BuildToTestRun(String instanceUrl) {
      this.instanceUrl = instanceUrl;
    }

    public String getInstanceUrl() {
        return "";
    }

    @Override
    public void perform(Run<?,?> build, FilePath workspace, Launcher launcher, TaskListener listener) {
        PractitestApi ptClient = new PractitestApi(getDescriptor().getBaseUrl(),
          getDescriptor().getApiToken());
        String exitCode = Result.SUCCESS.equals(build.getResult()) ? "0" : "1";
        String buildUrl = build.getAbsoluteUrl();
        ptClient.createRun(instanceUrl, exitCode, buildUrl);
    }

    @Override
    public DescriptorImpl getDescriptor() {
        return (DescriptorImpl)super.getDescriptor();
    }

    @Extension
    public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
        private String apiToken;
        private String baseUrl = "https://api.practitest.com";

        public DescriptorImpl() {
            load();
        }

        private static ListBoxModel convertToListBoxModel(Map<String,String> data){
          ListBoxModel listBox = new ListBoxModel();
          for (String id : data.keySet()) {
            listBox.add(data.get(id), id);
          }
          return listBox;
        }

        public FormValidation doCheckBaseUrl(@QueryParameter String value)
                throws IOException, ServletException {
            if (value.length() == 0)
                return FormValidation.error("Please set a URL");
            return FormValidation.ok();
        }

        public boolean isApplicable(Class<? extends AbstractProject> aClass) {
            return true;
        }

        public String getDisplayName() {
            return "PractiTest";
        }

        @Override
        public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
            apiToken = formData.getString("apiToken");
            baseUrl = formData.getString("baseUrl");
            save();
            return super.configure(req,formData);
        }

        public String getApiToken() {
          return apiToken;
        }

        public String getBaseUrl(){
          return baseUrl;
        }
    }
}

@@ -163,31 +163,17 @@
 n.model.
-AbstractProject
+*
 ;%0Aimport
@@ -184,17 +184,21 @@
 son.
-model.Run
+tasks.Builder
 ;%0Aim
@@ -213,20 +213,22 @@
 son.
-model.Result
+tasks.Notifier
 ;%0Aim
@@ -243,25 +243,29 @@
 son.
-model.TaskListene
+tasks.BuildStepMonito
 r;%0Ai
@@ -275,37 +275,39 @@
 rt hudson.tasks.
-Build
+Publish
 er;%0Aimport hudso
@@ -734,859 +734,571 @@
 n;%0A%0A
-/**%0A * Sample %7B@link Builder%7D.%0A *%0A * %3Cp%3E%0A * When the user configures the project and enables this builder,%0A * %7B@link DescriptorImpl#newInstance(StaplerRequest)%7D is invoked%0A * and a new %7B@link BuildToTestRun%7D is created. The created%0A * instance is persisted to the project configuration XML by using%0A * XStream, so this allows you to use instance fields (like )%0A * to remember the configuration.%0A *%0A * %3Cp%3E%0A * When a build is performed, the %7B@link #perform%7D method will be invoked.%0A */%0Apublic class BuildToTestRun extends Builder implements SimpleBuildStep %7B%0A%0A    private final String instanceUrl;%0A%0A    @DataBoundConstructor%0A    public BuildToTestRun(String instanceUrl) %7B%0A      this.instanceUrl = instanceUrl;%0A    %7D%0A%0A    public String getInstanceUrl() %7B%0A        return %22%22;%0A    %7D%0A%0A    @Override%0A    public void perform(Run%3C?,?%3E build, FilePath workspace
+public class BuildToTestRun extends Notifier %7B%0A%0A    private final String instanceUrl;%0A%0A    @DataBoundConstructor%0A    public BuildToTestRun(String instanceUrl) %7B%0A      this.instanceUrl = instanceUrl;%0A    %7D%0A%0A    public String getInstanceUrl() %7B%0A        return %22%22;%0A    %7D%0A%0A    @Override%0A    public DescriptorImpl getDescriptor() %7B%0A        return (DescriptorImpl)super.getDescriptor();%0A    %7D%0A%0A    @Override%0A    public BuildStepMonitor getRequiredMonitorService() %7B%0A      return BuildStepMonitor.NONE;%0A    %7D%0A%0A    @Override%0A    public boolean perform(AbstractBuild build
 , La
@@ -1318,12 +1318,13 @@
 er, 
-Task
+Build
 List
@@ -1662,121 +1662,23 @@
     
-%7D%0A%0A    @Override%0A    public DescriptorImpl getDescriptor() %7B%0A        return (DescriptorImpl)super.getDescriptor()
+    return true
 ;%0A  
@@ -1770,21 +1770,23 @@
 criptor%3C
-Build
+Publish
 er%3E %7B%0A  

move to post-build step