package org.lodder.subtools.multisubdownloader.subtitleproviders.adapters;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.lodder.subtools.multisubdownloader.subtitleproviders.SubtitleProvider;
import org.lodder.subtools.multisubdownloader.subtitleproviders.opensubtitles.OpenSubtitlesHasher;
import org.lodder.subtools.multisubdownloader.subtitleproviders.podnapisi.JPodnapisiApi;
import org.lodder.subtools.multisubdownloader.subtitleproviders.podnapisi.model.PodnapisiSubtitleDescriptor;
import org.lodder.subtools.sublibrary.JSubAdapter;
import org.lodder.subtools.sublibrary.Manager;
import org.lodder.subtools.sublibrary.control.ReleaseParser;
import org.lodder.subtools.sublibrary.logging.Logger;
import org.lodder.subtools.sublibrary.model.Release;
import org.lodder.subtools.sublibrary.model.TvRelease;
import org.lodder.subtools.sublibrary.model.MovieRelease;
import org.lodder.subtools.sublibrary.model.Subtitle;
import org.lodder.subtools.sublibrary.model.SubtitleMatchType;

public class JPodnapisiAdapter implements JSubAdapter, SubtitleProvider {

  private static JPodnapisiApi jpapi;
  public JPodnapisiAdapter(Manager manager) {
    try {
      if (jpapi == null) jpapi = new JPodnapisiApi("JBierSubDownloader", manager);
    } catch (Exception e) {
      Logger.instance.error("API PODNAPISI INIT: " + e.getCause());
    }
  }
  
  @Override
  public String getName() {
    return "Podnapisi";
  }

  @Override
  public List<Subtitle> search(Release release, String languageCode) {
    if (release instanceof MovieRelease) {
      return this.searchSubtitles((MovieRelease) release, languageCode);
    } else if (release instanceof TvRelease){
      return this.searchSubtitles((TvRelease) release, languageCode);
    }
    return new ArrayList<Subtitle>();
  }

  @Override
  public List<Subtitle> searchSubtitles(MovieRelease movieRelease, String... sublanguageid) {
    List<PodnapisiSubtitleDescriptor> lSubtitles = new ArrayList<PodnapisiSubtitleDescriptor>();
    if (!movieRelease.getFilename().equals("")) {
      File file = new File(movieRelease.getPath(), movieRelease.getFilename());
      if (file.exists())
        try {
          lSubtitles =
              jpapi.searchSubtitles(new String[] {OpenSubtitlesHasher.computeHash(file)},
                  sublanguageid[0]);
        } catch (Exception e) {
          Logger.instance.error("API PODNAPISI searchSubtitles using file hash: " + e);
        }
    }
    if (lSubtitles.size() == 0) {
      try {
        lSubtitles.addAll(jpapi.searchSubtitles(movieRelease.getTitle(), movieRelease.getYear(), 0, 0,
            sublanguageid[0]));
      } catch (Exception e) {
        Logger.instance.error("API PODNAPISI searchSubtitles using title: " + e);
      }
    }
    return buildListSubtitles(sublanguageid[0], lSubtitles);
  }

  @Override
  public List<Subtitle> searchSubtitles(TvRelease tvRelease, String... sublanguageid) {
    List<PodnapisiSubtitleDescriptor> lSubtitles = new ArrayList<PodnapisiSubtitleDescriptor>();

    try {
      String showName = "";
      if (tvRelease.getOriginalShowName().length() > 0) {
        showName = tvRelease.getOriginalShowName();
      } else {
        showName = tvRelease.getShow();
      }

      if (showName.length() > 0) {
        for (int episode : tvRelease.getEpisodeNumbers()) {
          lSubtitles =
              jpapi
                  .searchSubtitles(showName, 0, tvRelease.getSeason(), episode, sublanguageid[0]);
        }
      }
    } catch (Exception e) {
      Logger.instance.error("API PODNAPISI searchSubtitles: " + e);
    }
    return buildListSubtitles(sublanguageid[0], lSubtitles);
  }

  private List<Subtitle> buildListSubtitles(String sublanguageid,
      List<PodnapisiSubtitleDescriptor> lSubtitles) {
    List<Subtitle> listFoundSubtitles = new ArrayList<Subtitle>();
    for (PodnapisiSubtitleDescriptor ossd : lSubtitles) {
      if (!ossd.getReleaseString().equals("")) {
        final String downloadlink = getDownloadLink(ossd.getSubtitleId());
        if (downloadlink != null) {
          for (String release : ossd.getReleaseString().split(" ")) {
            listFoundSubtitles.add(new Subtitle(Subtitle.SubtitleSource.PODNAPISI, release,
                downloadlink, sublanguageid, "", SubtitleMatchType.EVERYTHING, ReleaseParser
                    .extractReleasegroup(release), ossd.getUploaderName(), ossd.getFlagsString().equals(
                    "nhu")));
          }
        }
      }
    }
    return listFoundSubtitles;
  }

  private String getDownloadLink(String subtitleId) {
    try {
      return jpapi.downloadUrl(subtitleId);
    } catch (Exception e) {
      Logger.instance.error("API PODNAPISI getdownloadlink: " + e);
    }
    return null;
  }
}

@@ -722,22 +722,26 @@
 ary.
-logging.Logger
+model.MovieRelease
 ;%0Aim
@@ -795,121 +795,8 @@
 se;%0A
-import org.lodder.subtools.sublibrary.model.TvRelease;%0Aimport org.lodder.subtools.sublibrary.model.MovieRelease;%0A
 impo
@@ -912,83 +912,284 @@
 pe;%0A
-%0Apublic class JPodnapisiAdapter implements JSubAdapter, SubtitleProvider %7B%0A
+import org.lodder.subtools.sublibrary.model.TvRelease;%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Apublic class JPodnapisiAdapter implements JSubAdapter, SubtitleProvider %7B%0A%0A  private static final Logger LOGGER = LoggerFactory.getLogger(JPodnapisiAdapter.class);
 %0A  p
@@ -1393,38 +1393,29 @@
 e) %7B%0A      L
-ogger.instance
+OGGER
 .error(%22API 
@@ -1428,21 +1428,18 @@
 ISI INIT
-: %22 +
+%22,
  e.getCa
@@ -2453,38 +2453,29 @@
 %0A          L
-ogger.instance
+OGGER
 .error(%22API 
@@ -2515,21 +2515,18 @@
 ile hash
-: %22 +
+%22,
  e);%0A   
@@ -2754,38 +2754,29 @@
  %7B%0A        L
-ogger.instance
+OGGER
 .error(%22API 
@@ -2812,21 +2812,18 @@
 ng title
-: %22 +
+%22,
  e);%0A   
@@ -3595,38 +3595,29 @@
 e) %7B%0A      L
-ogger.instance
+OGGER
 .error(%22API 
@@ -3641,21 +3641,18 @@
 ubtitles
-: %22 +
+%22,
  e);%0A   
@@ -4734,22 +4734,13 @@
    L
-ogger.instance
+OGGER
 .err
@@ -4776,13 +4776,10 @@
 link
-: %22 +
+%22,
  e);

Convert JPodnapisiAdapter from old logger to new LOGGER slf4j