package org.lodder.subtools.multisubdownloader.gui.workers;

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

import javax.swing.SwingWorker;

import org.lodder.subtools.multisubdownloader.actions.RenameAction;
import org.lodder.subtools.multisubdownloader.gui.dialog.Cancelable;
import org.lodder.subtools.multisubdownloader.gui.extra.progress.StatusMessenger;
import org.lodder.subtools.multisubdownloader.lib.ReleaseFactory;
import org.lodder.subtools.multisubdownloader.settings.model.LibrarySettings;
import org.lodder.subtools.sublibrary.Manager;
import org.lodder.subtools.sublibrary.control.VideoPatterns;
import org.lodder.subtools.sublibrary.logging.Logger;
import org.lodder.subtools.sublibrary.model.Release;
import org.lodder.subtools.sublibrary.model.VideoType;
import org.lodder.subtools.sublibrary.util.FilenameExtensionFilter;
import org.lodder.subtools.sublibrary.util.StringUtils;

public class TypedRenameWorker extends SwingWorker<Void, String> implements Cancelable {


  private File dir;
  private VideoType videoType;
  private final FilenameExtensionFilter patterns;
  private boolean isRecursive;
  private ReleaseFactory releaseFactory;
  private RenameAction renameAction;

  public TypedRenameWorker(File dir, LibrarySettings librarySettings, VideoType videoType,
      boolean isRecursive, Manager manager) {
    setParameters(dir, librarySettings, videoType, isRecursive, manager);
    patterns =
        new FilenameExtensionFilter(
            StringUtils.join(VideoPatterns.EXTENSIONS, new String[] {"srt"}));
  }

  public void setParameters(File dir, LibrarySettings librarySettings, VideoType videoType,
      boolean isRecursive, Manager manager) {
    this.dir = dir;
    this.videoType = videoType;
    this.isRecursive = isRecursive;
    this.renameAction = new RenameAction(librarySettings, manager);
  }

  public void setReleaseFactory(ReleaseFactory releaseFactory) {
    this.releaseFactory = releaseFactory;
  }

  @Override
  protected Void doInBackground() throws Exception {
    rename(dir);
    return null;
  }

  private void rename(File dir) {
    File[] contents = dir.listFiles();
    if (contents == null) return;
    
    for (final File file : contents) {
      if (file.isFile() && !file.getName().contains("sample")
          && patterns.accept(file.getAbsoluteFile(), file.getName())) {
        Release release;
        try {
          release = releaseFactory.createRelease(file);
          if (release != null) {
            publish(release.getFilename());
            if (release.getVideoType() == videoType)
              renameAction.rename(file, release);
          }
        } catch (Exception e) {
          Logger.instance.log("Series Rename " + e.getMessage());
        }
      } else if (file.isDirectory() && isRecursive) {
        rename(file);
      }
    }
  }

  protected void process(List<String> data) {
    for (String s : data)
      StatusMessenger.instance.message("Bestand hernoemen: " + s);
  }
}

@@ -607,62 +607,8 @@
 ns;%0A
-import org.lodder.subtools.sublibrary.logging.Logger;%0A
 impo
@@ -656,16 +656,16 @@
 elease;%0A
+
 import o
@@ -834,16 +834,73 @@
 ngUtils;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Apublic
@@ -1157,16 +1157,16 @@
 actory;%0A
-
   privat
@@ -1193,16 +1193,108 @@
 eAction;
+%0A  %0A  private static final Logger LOGGER = LoggerFactory.getLogger(TypedRenameWorker.class);
 %0A%0A  publ
@@ -2768,26 +2768,19 @@
    L
-ogger.instance.log
+OGGER.error
 (%22Se
@@ -2794,27 +2794,13 @@
 name
- 
 %22 
-+
+,
  e
-.getMessage()
 );%0A 

Convert TypedRenameWorker from old logger to new LOGGER slf4j