package org.lodder.subtools.multisubdownloader.lib.control;

import java.util.List;

import org.lodder.subtools.multisubdownloader.lib.JTVRageAdapter;
import org.lodder.subtools.multisubdownloader.settings.model.Settings;
import org.lodder.subtools.multisubdownloader.settings.model.SettingsProcessEpisodeSource;
import org.lodder.subtools.sublibrary.JTheTVDBAdapter;
import org.lodder.subtools.sublibrary.Manager;
import org.lodder.subtools.sublibrary.data.thetvdb.model.TheTVDBEpisode;
import org.lodder.subtools.sublibrary.data.thetvdb.model.TheTVDBSerie;
import org.lodder.subtools.sublibrary.data.tvrage.model.TVRageEpisode;
import org.lodder.subtools.sublibrary.data.tvrage.model.TVRageShowInfo;
import org.lodder.subtools.sublibrary.exception.ReleaseControlException;
import org.lodder.subtools.sublibrary.logging.Logger;
import org.lodder.subtools.sublibrary.model.TvRelease;
import org.lodder.subtools.sublibrary.settings.model.MappingTvdbScene;

public class TvReleaseControl extends ReleaseControl {

  private JTheTVDBAdapter jtvdba;
  private final JTVRageAdapter tvra;

  public TvReleaseControl(TvRelease tvRelease, Settings settings, Manager manager) {
    super(tvRelease, settings, manager);
    jtvdba = JTheTVDBAdapter.getAdapter(manager);
    tvra = new JTVRageAdapter(manager);
  }

  public void processTvdb(List<MappingTvdbScene> dict) throws ReleaseControlException {
    setTvdbID(dict);
    if (((TvRelease) release).getTvdbid() > 0) {
      TheTVDBEpisode thetvdbepisode = jtvdba.getEpisode(((TvRelease) release));
      if (thetvdbepisode != null) {
        ((TvRelease) release).updateTvdbEpisodeInfo(thetvdbepisode);
      } else {
        throw new ReleaseControlException("Season " + ((TvRelease) release).getSeason()
            + " Episode " + ((TvRelease) release).getEpisodeNumbers().toString()
            + "not found, check file", release);
      }
    } else {
      throw new ReleaseControlException("Show not found, check file", release);
    }
  }

  public void processTVRage() throws ReleaseControlException {
    setTvrageID();
    TVRageEpisode tvrEpisode =
        tvra.getEpisodeInfo(((TvRelease) release).getTvrageid(), ((TvRelease) release).getSeason(),
            ((TvRelease) release).getEpisodeNumbers().get(0));
    if (tvrEpisode != null) {
      ((TvRelease) release).updateTVRageEpisodeInfo(tvrEpisode);
    } else {
      throw new ReleaseControlException("Season " + ((TvRelease) release).getSeason() + " Episode "
          + ((TvRelease) release).getEpisodeNumbers().toString() + "not found, check file", release);
    }
  }

  public void process(List<MappingTvdbScene> dict) throws ReleaseControlException {
    Logger.instance.trace("EpisodeFileControl", "process", "");

    TvRelease tvRelease = (TvRelease) release;
    // return episodeFile;
    if (tvRelease.getShow().equals("")) {
      throw new ReleaseControlException("Unable to extract episode details, check file", release);
    } else {
      Logger.instance.debug("Showname: " + tvRelease.getShow());
      Logger.instance.debug("Season: " + tvRelease.getSeason());
      Logger.instance.debug("Episode: " + tvRelease.getEpisodeNumbers());

      if (tvRelease.isSpecial()) {
        processSpecial(dict);
      } else {
        if (settings.getProcessEpisodeSource().equals(SettingsProcessEpisodeSource.TVRAGE)) {
          processTVRage();
        }
        processTvdb(dict);
      }
    }
  }

  /**
   * @param dict
   * @throws ReleaseControlException
   * 
   */
  private void processSpecial(List<MappingTvdbScene> dict) throws ReleaseControlException {
    TVRageEpisode tvrEpisode = null;
    TheTVDBEpisode thetvdbepisode = null;
    setTvrageID();
    if (((TvRelease) release).getTvrageid() > 0) {
      tvrEpisode =
          tvra.getEpisodeInfo(((TvRelease) release).getTvrageid(),
              ((TvRelease) release).getSeason(), ((TvRelease) release).getEpisodeNumbers().get(0));
      if (tvrEpisode != null
          && settings.getProcessEpisodeSource() == SettingsProcessEpisodeSource.TVRAGE)
        ((TvRelease) release).updateTVRageEpisodeInfo(tvrEpisode);
    }
    setTvdbID(dict);
    if (((TvRelease) release).getTvdbid() > 0) {
      thetvdbepisode = jtvdba.getEpisode(((TvRelease) release));
      if (thetvdbepisode != null
          && settings.getProcessEpisodeSource() == SettingsProcessEpisodeSource.TVDB)
        ((TvRelease) release).updateTvdbEpisodeInfo(thetvdbepisode);
    }
  }

  private void setTvdbID(List<MappingTvdbScene> dict) throws ReleaseControlException {
    int tvdbid = 0;
    for (MappingTvdbScene mapping : dict) {
      if (mapping.getSceneName().replaceAll("[^A-Za-z]", "")
          .equalsIgnoreCase(((TvRelease) release).getShow().replaceAll("[^A-Za-z]", ""))) {
        tvdbid = mapping.getTvdbId();
      }
    }

    TheTVDBSerie thetvdbserie = null;
    if (tvdbid == 0) {
      thetvdbserie = jtvdba.getSerie((TvRelease) release);
    } else {
      thetvdbserie = jtvdba.getSerie(tvdbid);
    }

    if (thetvdbserie == null)
      throw new ReleaseControlException("Tvdb API, returned no result", release);
    ((TvRelease) release).setOriginalShowName(thetvdbserie.getSerieName());
    ((TvRelease) release).setTvdbid(Integer.parseInt(thetvdbserie.getId()));
  }

  private void setTvrageID() {
    TVRageShowInfo tvrShowInfo = tvra.searchShow((TvRelease) release);
    if (tvrShowInfo != null) ((TvRelease) release).setTvrageid(tvrShowInfo.getShowID());
  }
}

@@ -772,62 +772,8 @@
 on;%0A
-import org.lodder.subtools.sublibrary.logging.Logger;%0A
 impo
@@ -823,16 +823,16 @@
 elease;%0A
+
 import o
@@ -893,16 +893,73 @@
 dbScene;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Apublic
@@ -1042,16 +1042,16 @@
 jtvdba;%0A
-
   privat
@@ -1080,16 +1080,105 @@
  tvra;%0A%0A
+  private static final Logger LOGGER = LoggerFactory.getLogger(TvReleaseControl.class);%0A%0A
   public
@@ -2763,73 +2763,8 @@
 n %7B%0A
-    Logger.instance.trace(%22EpisodeFileControl%22, %22process%22, %22%22);%0A%0A
     
@@ -2998,43 +2998,72 @@
    L
-ogger.instance.debug(%22Showname: %22 +
+OGGER.debug(%22process: showname %5B%7B%7D%5D, season %5B%7B%7D%5D, episode %5B%7B%7D%5D%22,
  tvR
@@ -3082,117 +3082,42 @@
 ow()
-);
+,
 %0A      
-Logger.instance.debug(%22Season: %22 + tvRelease.getSeason());%0A      Logger.instance.debug(%22Episode: %22 +
+    tvRelease.getSeason(),
  tvR

Convert TvReleaseControl from old logger to new LOGGER slf4j