package org.lodder.subtools.sublibrary;

import java.util.List;

import org.lodder.subtools.sublibrary.data.thetvdb.TheTVDBApi;
import org.lodder.subtools.sublibrary.data.thetvdb.TheTVDBException;
import org.lodder.subtools.sublibrary.data.thetvdb.model.TheTVDBEpisode;
import org.lodder.subtools.sublibrary.data.thetvdb.model.TheTVDBSerie;
import org.lodder.subtools.sublibrary.logging.Logger;
import org.lodder.subtools.sublibrary.model.TvRelease;

public class JTheTVDBAdapter {

  private TheTVDBApi jtvapi;
  private static JTheTVDBAdapter adapter;
  private String exceptions = "";
  private final static String splitValue = ": '";

  JTheTVDBAdapter(Manager manager) {
    this.jtvapi = null;
    try {
      this.jtvapi = new TheTVDBApi("A1720D2DDFDCE82D", manager);
      exceptions =
          manager
              .downloadText("http://midgetspy.github.io/sb_tvdb_scene_exceptions/exceptions.txt");
    } catch (TheTVDBException e) {
      Logger.instance.error(Logger.stack2String(e));
    } catch (ManagerException e) {
      Logger.instance.error(Logger.stack2String(e));
    }

  }

  public TheTVDBSerie searchSerie(TvRelease episode) {
    int tvdbid;
    try {
      tvdbid = this.jtvapi.searchSerie(episode.getShow(), null);
      if (tvdbid == 0) {
        Logger.instance.error("Unknown serie name in tvdb: " + episode.getShow());
        return null;
      }
      return this.jtvapi.getSerie(tvdbid, null);
    } catch (TheTVDBException e) {
      Logger.instance.error(Logger.stack2String(e));
    }
    return null;
  }

  public TheTVDBEpisode getEpisode(TvRelease episode) {
    try {
      return this.jtvapi.getEpisode(episode.getTvdbid(), episode.getSeason(), episode
          .getEpisodeNumbers().get(0), "en");
    } catch (TheTVDBException e) {
      Logger.instance.error(Logger.stack2String(e));
    }
    return null;
  }

  public TheTVDBSerie getSerie(TvRelease episode) {
    try {
      if (episode.getTvdbid() > 0) {
        return this.jtvapi.getSerie(episode.getTvdbid(), null);
      } else {
        int tvdbid = sickbeardTVDBSceneExceptions(episode.getShow());
        if (tvdbid > 0) return this.jtvapi.getSerie(tvdbid, null);
        return searchSerie(episode);
      }
    } catch (TheTVDBException e) {
      Logger.instance.error(Logger.stack2String(e));
    }
    return null;
  }

  public TheTVDBSerie getSerie(int tvdbid) {
    try {
      return this.jtvapi.getSerie(tvdbid, null);
    } catch (TheTVDBException e) {
      Logger.instance.error(Logger.stack2String(e));
    }
    return null;
  }

  public List<TheTVDBEpisode> getAllEpisodes(int tvdbid, String language) {
    try {
      return this.jtvapi.getAllEpisodes(tvdbid, language);
    } catch (TheTVDBException e) {
      Logger.instance.error(Logger.stack2String(e));
    }
    return null;
  }

  public int sickbeardTVDBSceneExceptions(String serieName) {
    if (exceptions.isEmpty()) return 0;
    for (String exception : exceptions.split("\n")) {
      int tvdbid = Integer.parseInt(exception.split(splitValue)[0].trim());
      String[] names = exception.split(splitValue)[1].split(",");
      for (String name : names) {
        String a = name.replaceAll("[^A-Za-z]", "");
        String b = serieName.replaceAll("[^A-Za-z]", "");
        if (!a.isEmpty() && a.equals(b)) {
          return tvdbid;
        }
      }
    }
    return 0;
  }

  /**
   * @return JTheTVDBAdapter
   */
  public synchronized static JTheTVDBAdapter getAdapter(Manager manager) {
    if (adapter == null) adapter = new JTheTVDBAdapter(manager);
    return adapter;
  }
}

@@ -376,77 +376,80 @@
 ary.
-logging.Logger;%0Aimport org.lodder.subtools.sublibrary.model.TvRelease
+model.TvRelease;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0Ap
@@ -633,16 +633,103 @@
 = %22: '%22;
+%0A  private static final Logger LOGGER = LoggerFactory.getLogger(JTheTVDBAdapter.class);
 %0A%0A  JThe
@@ -1040,51 +1040,37 @@
    L
-ogger.instance.error(Logger.stack2String(e)
+OGGER.error(e.getMessage(), e
 );%0A 
@@ -1114,51 +1114,37 @@
    L
-ogger.instance.error(Logger.stack2String(e)
+OGGER.error(e.getMessage(), e
 );%0A 
@@ -1330,38 +1330,29 @@
  %7B%0A        L
-ogger.instance
+OGGER
 .error(%22Unkn
@@ -1523,51 +1523,36 @@
    L
-ogger.instance.error(Logger.stack2String(e)
+OGGER.error(e.getMessage(),e
 );%0A 
@@ -1822,51 +1822,36 @@
    L
-ogger.instance.error(Logger.stack2String(e)
+OGGER.error(e.getMessage(),e
 );%0A 
@@ -2283,51 +2283,36 @@
    L
-ogger.instance.error(Logger.stack2String(e)
+OGGER.error(e.getMessage(),e
 );%0A 
@@ -2488,51 +2488,42 @@
    L
-ogger.instance.error(Logger.stack2String(e)
+OGGER.error(%22getSerie exception%22,e
 );%0A 
@@ -2740,51 +2740,49 @@
    L
-ogger.instance.error(Logger.stack2String(e)
+OGGER.error(%22getAllEpisodes exception%22, e
 );%0A 

Convert JTheTVDBAdapter from old logger to new LOGGER slf4j