package io.fabric8.jenkins.openshiftsync;

import hudson.model.Job;
import hudson.util.XStream2;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.openshift.api.model.BuildConfig;
import jenkins.model.Jenkins;
import org.apache.tools.ant.filters.StringInputStream;

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

import static io.fabric8.jenkins.openshiftsync.BuildConfigToJobMapper.mapBuildConfigToJob;

public class BuildConfigWatcher implements Watcher<BuildConfig> {

  private final Logger logger = Logger.getLogger(getClass().getName());

  @Override
  public void onClose(KubernetesClientException e) {
    if (e != null) {
      logger.warning(e.toString());
    }
  }

  @Override
  public void eventReceived(Watcher.Action action, BuildConfig buildConfig) {
    try {
      switch (action) {
        case ADDED:
          createJob(buildConfig);
          break;
        case DELETED:
          deleteJob(buildConfig);
          break;
        case MODIFIED:
          modifyJob(buildConfig);
          break;
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private void createJob(BuildConfig buildConfig) throws IOException {
    Job job = mapBuildConfigToJob(buildConfig);
    Jenkins.getInstance().createProjectFromXML(
      job.getName(),
      new StringInputStream(new XStream2().toXML(job))
    );
  }

  private void modifyJob(BuildConfig buildConfig) {

  }

  private void deleteJob(BuildConfig buildConfig) {
  }

}

@@ -36,16 +36,42 @@
 tsync;%0A%0A
+import hudson.model.Item;%0A
 import h
@@ -87,16 +87,16 @@
 el.Job;%0A
-
 import h
@@ -423,16 +423,95 @@
 ogger;%0A%0A
+import static io.fabric8.jenkins.openshiftsync.BuildConfigToJobMapper.jobName;%0A
 import s
@@ -1234,32 +1234,55 @@
 ch (IOException 
+%7C InterruptedException 
 e) %7B%0A      e.pri
@@ -1671,17 +1671,176 @@
 Config) 
-%7B
+throws IOException, InterruptedException %7B%0A    String jobName = jobName(buildConfig);%0A    Item item = Jenkins.getInstance().getItem(jobName);%0A    item.delete();
 %0A  %7D%0A%0A%7D%0A

Delete jobs when BuildConfigs are deleted