package org.lodder.subtools.multisubdownloader.actions;

import java.io.File;
import java.io.IOException;

import org.lodder.subtools.multisubdownloader.lib.library.FilenameLibraryBuilder;
import org.lodder.subtools.multisubdownloader.lib.library.LibraryActionType;
import org.lodder.subtools.multisubdownloader.lib.library.LibraryOtherFileActionType;
import org.lodder.subtools.multisubdownloader.lib.library.PathLibraryBuilder;
import org.lodder.subtools.multisubdownloader.settings.model.LibrarySettings;
import org.lodder.subtools.sublibrary.DetectLanguage;
import org.lodder.subtools.sublibrary.Manager;
import org.lodder.subtools.sublibrary.logging.Logger;
import org.lodder.subtools.sublibrary.model.Release;
import org.lodder.subtools.sublibrary.util.Files;

public class RenameAction {

  private LibrarySettings librarySettings;
  private Manager manager;

  public RenameAction(LibrarySettings librarySettings, Manager manager) {
    this.librarySettings = librarySettings;
    this.manager =manager;
  }

  public void rename(File f, Release release) {
    Logger.instance
        .trace("Actions", "rename", "LibraryAction" + librarySettings.getLibraryAction());
    String filename = "";

    switch (librarySettings.getLibraryAction()) {
      case MOVE:
        filename = f.getName();
        break;
      case MOVEANDRENAME:
        filename = getNewFilename(f, release);
        break;
      case NOTHING:
        filename = f.getName();
        break;
      case RENAME:
        filename = getNewFilename(f, release);
        break;
      default:
        break;
    }
    Logger.instance.trace("Actions", "rename", "filename" + filename);

    PathLibraryBuilder pathLibraryBuilder = new PathLibraryBuilder(librarySettings, manager);
    final File newDir = new File(pathLibraryBuilder.build(release));
    boolean status = true;
    if (!newDir.exists()) {
      Logger.instance.debug("Creating dir: " + newDir.getAbsolutePath());
      status = newDir.mkdirs();
    }

    Logger.instance.trace("Actions", "rename", "newDir" + newDir);

    if (status) {
      final File file = new File(release.getPath(), release.getFilename());

      try {

        if (librarySettings.getLibraryAction().equals(LibraryActionType.MOVE)
            || librarySettings.getLibraryAction().equals(LibraryActionType.MOVEANDRENAME)) {
          Logger.instance.log("Moving " + filename + " to the library folder " + newDir
              + " , this might take a while... ");
          Files.move(file, new File(newDir, filename));
        } else {
          Logger.instance.log("Moving " + filename + " to the library folder " + release.getPath()
              + " , this might take a while... ");
          Files.move(file, new File(release.getPath(), filename));
        }
        if (!librarySettings.getLibraryOtherFileAction().equals(LibraryOtherFileActionType.NOTHING)) {
          CleanAction cleanAction = new CleanAction(librarySettings);
          cleanAction.cleanUpFiles(release, newDir, filename);
        }
        File[] listFiles = release.getPath().listFiles();
        if (librarySettings.isLibraryRemoveEmptyFolders() && listFiles != null
            && listFiles.length == 0) {
          boolean isDeleted = release.getPath().delete();
          if (isDeleted){
            //do nothing
          }
        }
      } catch (IOException e) {
        Logger.instance.error("Unsuccessfull in moving the file to the libary");
      }

    }
  }

  private String getNewFilename(File f, Release release) {
    String filename = "";
    FilenameLibraryBuilder filenameLibraryBuilder = new FilenameLibraryBuilder(librarySettings, manager);
    filename = filenameLibraryBuilder.build(release);
    if (release.getExtension().equals("srt")) {
      String languageCode = "";
      try {
        if (librarySettings.isLibraryIncludeLanguageCode()) {
          languageCode = DetectLanguage.execute(f);
        }
      } catch (final Exception e) {
        Logger.instance.error("Unable to detect language, leaving language code blank");
      }

      filename = filenameLibraryBuilder.buildSubtitle(release, filename, languageCode, 0);
    }
    return filename;
  }
}

@@ -606,62 +606,8 @@
 er;%0A
-import org.lodder.subtools.sublibrary.logging.Logger;%0A
 impo
@@ -704,16 +704,73 @@
 l.Files;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Apublic
@@ -861,16 +861,103 @@
 manager;
+%0A  %0A  private static final Logger LOGGER = LoggerFactory.getLogger(RenameAction.class);
 %0A%0A  publ
@@ -1152,119 +1152,8 @@
 ) %7B%0A
-    Logger.instance%0A        .trace(%22Actions%22, %22rename%22, %22LibraryAction%22 + librarySettings.getLibraryAction());%0A
     
@@ -1570,59 +1570,37 @@
    L
-ogger.instance.trace(%22Actions%22, 
+OGGER.trace(
 %22rename
-%22, %22
+: 
 filename
 %22 + 
@@ -1595,19 +1595,23 @@
 filename
-%22 +
+ %5B%7B%7D%5D%22,
  filenam
@@ -1840,30 +1840,21 @@
 %0A      L
-ogger.instance
+OGGER
 .debug(%22
@@ -1869,13 +1869,15 @@
  dir
-: %22 +
+ %5B%7B%7D%5D%22,
  new
@@ -1948,60 +1948,42 @@
    L
-ogger.instance.trace(%22Actions%22, 
+OGGER.trace(
 %22rename
-%22, %22
+: 
 newDir
-%22 +
+ %5B%7B%7D%5D%22,
  new
@@ -2283,102 +2283,58 @@
    L
-ogger.instance.log(%22Moving %22 + filename + %22 to the library folder %22 + newDir%0A              + %22
+OGGER.info(%22Moving %5B%7B%7D%5D to the library folder %5B%7B%7D%5D
  , t
@@ -2352,32 +2352,50 @@
 ake a while... %22
+, filename, newDir
 );%0A          Fil
@@ -2469,113 +2469,58 @@
    L
-ogger.instance.log(%22Moving %22 + filename + %22 to the library folder %22 + release.getPath()%0A              + %22
+OGGER.info(%22Moving %5B%7B%7D%5D to the library folder %5B%7B%7D%5D
  , t
@@ -2546,16 +2546,45 @@
 ile... %22
+, filename, release.getPath()
 );%0A     
@@ -3246,38 +3246,29 @@
  %7B%0A        L
-ogger.instance
+OGGER
 .error(%22Unsu
@@ -3310,16 +3310,19 @@
  libary%22
+, e
 );%0A     
@@ -3846,22 +3846,13 @@
    L
-ogger.instance
+OGGER
 .err
@@ -3910,16 +3910,19 @@
 e blank%22
+, e
 );%0A     

Convert RenameAction from old logger to new LOGGER slf4j