package org.lodder.subtools.sublibrary.data;

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

import org.lodder.subtools.sublibrary.control.ReleaseParser;
import org.lodder.subtools.sublibrary.logging.Logger;
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;
import org.lodder.subtools.sublibrary.privateRepo.PrivateRepoIndex;
import org.lodder.subtools.sublibrary.privateRepo.model.IndexSubtitle;
import org.lodder.subtools.sublibrary.util.Files;
import org.lodder.subtools.sublibrary.util.http.DropBoxClient;

public class PrivateRepo {

  private List<IndexSubtitle> index = new ArrayList<IndexSubtitle>();
  private String indexUrl = "/Ondertitels/PrivateRepo/index";
  private String indexVersionUrl = "/Ondertitels/PrivateRepo/index.version";
  private static PrivateRepo privateRepo;
  private final File path = new File(System.getProperty("user.home"), ".MultiSubDownloader");
  private final File rVersion = new File(path, "rVersion");
  private final File rIndex = new File(path, "rIndex");

  @SuppressWarnings("unused")
  PrivateRepo() {
    int localVersion = 0;
    String strIndex = "";
    int indexVersion = 0;

    if (!path.exists()) {
      boolean isCreated = path.mkdir();
    }

    try {
      if (rVersion.exists()) localVersion = Integer.parseInt(Files.read(rVersion).trim());
      indexVersion =
          Integer.parseInt(DropBoxClient.getDropBoxClient().getFile(indexVersionUrl).trim());
      if (indexVersion > localVersion || !rIndex.exists()) {
        strIndex = DropBoxClient.getDropBoxClient().getFile(indexUrl);
        Files.write(rIndex, strIndex);
        Files.write(rVersion, Integer.toString(indexVersion).trim());
      } else {
        strIndex = Files.read(rIndex);
      }
    } catch (Exception e) {
      Logger.instance.log(Logger.stack2String(e));
    }
    index = PrivateRepoIndex.getIndex(strIndex);
  }

  public static PrivateRepo getPrivateRepo() {
    if (privateRepo == null) privateRepo = new PrivateRepo();
    return privateRepo;
  }

  public List<Subtitle> searchSubtitles(TvRelease tvRelease, String languageCode)
      throws UnsupportedEncodingException {
    List<Subtitle> results = new ArrayList<Subtitle>();
    for (IndexSubtitle indexSubtitle : index) {
      if (indexSubtitle.getVideoType() == tvRelease.getVideoType()) {
        if (indexSubtitle.getTvdbid() == tvRelease.getTvdbid()) {
          if (indexSubtitle.getSeason() == tvRelease.getSeason()
              && indexSubtitle.getEpisode() == tvRelease.getEpisodeNumbers().get(0)) {
            if (indexSubtitle.getLanguage().equalsIgnoreCase(languageCode)) {
              String location =
                  "/" + indexSubtitle.getName() + "/" + indexSubtitle.getSeason() + "/"
                      + indexSubtitle.getEpisode() + "/" + indexSubtitle.getLanguage() + "/"
                      + PrivateRepoIndex.getFullFilename(indexSubtitle);

              Subtitle tempSub =
                  new Subtitle(Subtitle.SubtitleSource.LOCAL, indexSubtitle.getFilename(),
                      location, indexSubtitle.getLanguage(), "", SubtitleMatchType.EVERYTHING,
                      ReleaseParser.extractReleasegroup(indexSubtitle.getFilename()), "", false);
              results.add(tempSub);
            }
          }
        }
      }
    }
    return results;
  }

  public List<Subtitle> searchSubtitles(MovieRelease movieRelease, String languageCode) {
    List<Subtitle> results = new ArrayList<Subtitle>();
    for (IndexSubtitle indexSubtitle : index) {
      if (indexSubtitle.getVideoType() == movieRelease.getVideoType()) {
        if (indexSubtitle.getImdbid() == movieRelease.getImdbid()) {
          if (indexSubtitle.getYear() == movieRelease.getYear()) {
            if (indexSubtitle.getLanguage().equalsIgnoreCase(languageCode)) {
              String location =
                  "/movies/" + indexSubtitle.getName() + " " + indexSubtitle.getYear() + "/"
                      + indexSubtitle.getLanguage() + "/"
                      + PrivateRepoIndex.getFullFilename(indexSubtitle);

              Subtitle tempSub =
                  new Subtitle(Subtitle.SubtitleSource.LOCAL, indexSubtitle.getFilename(),
                      location, indexSubtitle.getLanguage(), "", SubtitleMatchType.EVERYTHING,
                      ReleaseParser.extractReleasegroup(indexSubtitle.getFilename()), "", false);
              results.add(tempSub);
            }
          }
        }
      }
    }
    return results;
  }
}

@@ -222,117 +222,8 @@
 er;%0A
-import org.lodder.subtools.sublibrary.logging.Logger;%0Aimport org.lodder.subtools.sublibrary.model.TvRelease;%0A
 impo
@@ -393,16 +393,71 @@
 chType;%0A
+import org.lodder.subtools.sublibrary.model.TvRelease;%0A
 import o
@@ -704,36 +704,179 @@
 nt;%0A
-%0Apublic class PrivateRepo %7B%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A%0Apublic class PrivateRepo %7B%0A%0A  private static final Logger LOGGER = LoggerFactory.getLogger(PrivateRepo.class);%0A  
 %0A  p
@@ -2092,49 +2092,60 @@
    L
-ogger.instance.log(Logger.stack2String(e)
+OGGER.error(%22Unable to get latest version number%22, e
 );%0A 

Convert PrivateRepo from old logger to new LOGGER slf4j