package com.sumologic.jenkins.jenkinssumologicplugin;

import com.google.gson.Gson;
import com.sumologic.jenkins.jenkinssumologicplugin.model.ModelFactory;
import com.sumologic.jenkins.jenkinssumologicplugin.sender.LogSender;
import hudson.Launcher;
import hudson.model.*;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
import hudson.tasks.Publisher;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;

/**
 * This publisher will sendLogs build metadata to a Sumo Logic HTTP collector.
 *
 * Created by deven on 7/6/15.
 */
public class SumoBuildNotifier extends Notifier {

  private final static Logger LOG = Logger.getLogger(SumoBuildNotifier.class.getName());
  private static LogSender logSender = LogSender.getInstance();

  @DataBoundConstructor
  public SumoBuildNotifier() {
    super();
  }

  @SuppressWarnings("unchecked")
  public static SumoBuildNotifier getNotifier(AbstractProject project) {
    Map<Descriptor<Publisher>, Publisher> map = project.getPublishersList().toMap();
    for (Publisher publisher : map.values()) {
      if (publisher instanceof SumoBuildNotifier) {
        return (SumoBuildNotifier) publisher;
      }
    }
    return null;
  }

  @Override
  public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
    send(build, listener);
    return true;
  }

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

  @Override
  public BuildStepMonitor getRequiredMonitorService() {
    return BuildStepMonitor.NONE;
  }

  protected void send(AbstractBuild build, TaskListener listener) {
    Gson gson = new Gson();
    String json = gson.toJson(ModelFactory.createBuildModel(build));

    PluginDescriptorImpl descriptor = PluginDescriptorImpl.getInstance();

    LOG.info("Uploading build status to sumologic: " + json);

    String url = descriptor.getUrl();
    String sourceName = descriptor.getSourceNameJobStatus();
    String category = descriptor.getSourceCategoryJobStatus();
    byte[] bytes = json.getBytes();

    logSender.sendLogs(url, bytes, sourceName, category);
  }
}

@@ -215,24 +215,48 @@
 .LogSender;%0A
+import hudson.FilePath;%0A
 import hudso
@@ -385,24 +385,62 @@
 .Publisher;%0A
+import jenkins.tasks.SimpleBuildStep;%0A
 import org.a
@@ -536,19 +536,81 @@
 java
-.io.IOExcep
+x.annotation.Nonnull;%0Aimport java.io.IOException;%0Aimport java.util.Collec
 tion
@@ -823,24 +823,48 @@
 ier 
-extends Notifier
+implements jenkins.tasks.SimpleBuildStep
  %7B%0A%0A
@@ -1092,16 +1092,18 @@
 %0A  %7D%0A%0A  
+/*
 @Suppres
@@ -1438,24 +1438,25 @@
     %7D%0A    %7D%0A
+%0A
     return n
@@ -1460,16 +1460,145 @@
 n null;%0A
+  %7D*/%0A%0A  @Override%0A  public boolean prebuild(AbstractBuild%3C?, ?%3E abstractBuild, BuildListener buildListener) %7B%0A    return false;%0A
   %7D%0A%0A  @
@@ -1818,102 +1818,219 @@
 lic 
-PluginDescriptorImpl getDescriptor() %7B%0A    return (PluginDescriptorImpl) super.getDescriptor()
+Action getProjectAction(AbstractProject%3C?, ?%3E abstractProject) %7B%0A    return null;%0A  %7D%0A%0A  @Override%0A  public Collection%3C? extends Action%3E getProjectActions(AbstractProject%3C?, ?%3E abstractProject) %7B%0A    return null
 ;%0A  
@@ -2703,11 +2703,210 @@
 ry);%0A  %7D
+%0A%0A  @Override%0A  public void perform(@Nonnull Run%3C?, ?%3E run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener taskListener) throws InterruptedException, IOException %7B%0A%0A  %7D
 %0A%7D%0A

change SumoBuildNotifier endpoind type