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.logging.Logger;
import org.lodder.subtools.sublibrary.model.Release;
import org.lodder.subtools.sublibrary.util.Files;

public class RenameAction {

  private LibrarySettings librarySettings;

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

  public void rename(File f, Release release) {
    Logger.instance
        .trace("Actions", "rename", "LibraryAction" + librarySettings.getLibraryAction());
    String filename = "";
    if (librarySettings.getLibraryAction().equals(LibraryActionType.RENAME)
        || librarySettings.getLibraryAction().equals(LibraryActionType.MOVEANDRENAME)) {
      FilenameLibraryBuilder filenameLibraryBuilder = new FilenameLibraryBuilder(librarySettings);
      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);
      }
    } else {
      filename = f.getName();
    }
    Logger.instance.trace("Actions", "rename", "filename" + filename);

    PathLibraryBuilder pathLibraryBuilder = new PathLibraryBuilder(librarySettings);
    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);
        }
        if (librarySettings.isLibraryRemoveEmptyFolders()
            && release.getPath().listFiles().length == 0) {
          release.getPath().delete();
        }
      } catch (IOException e) {
        Logger.instance.error("Unsuccessfull in moving the file to the libary");
      }

    }
  }
}

@@ -892,16 +892,46 @@
 s;%0A  %7D%0A%0A
+  @SuppressWarnings(%22unused%22)%0A
   public
@@ -1107,22 +1107,27 @@
 e = %22%22;%0A
+%0A
     
-if
+switch
  (librar
@@ -1158,796 +1158,340 @@
 on()
-.equals(LibraryActionType.RENAME)%0A        %7C%7C librarySettings.getLibraryAction().equals(LibraryActionType.MOVEANDRENAME)) %7B%0A      FilenameLibraryBuilder filenameLibraryBuilder = new FilenameLibraryBuilder(librarySettings);%0A      filename = filenameLibraryBuilder.build(release);%0A      if (release.getExtension().equals(%22srt%22)) %7B%0A        String languageCode = %22%22;%0A        try %7B%0A          if (librarySettings.isLibraryIncludeLanguageCode()) %7B%0A            languageCode = DetectLanguage.execute(f);%0A          %7D%0A        %7D catch (final Exception e) %7B%0A          Logger.instance.error(%22Unable to detect language, leaving language code blank%22);%0A        %7D%0A%0A        filename = filenameLibraryBuilder.buildSubtitle(release, filename, languageCode, 0);%0A      %7D%0A    %7D else %7B%0A      filename = f.getName()
+) %7B%0A      case MOVE:%0A        filename = f.getName();%0A        break;%0A      case MOVEANDRENAME:%0A        filename = getNewFilename(f, release);%0A        break;%0A      case NOTHING:%0A        filename = f.getName();%0A        break;%0A      case RENAME:%0A        filename = getNewFilename(f, release);%0A        break;%0A      default:%0A        break
 ;%0A  
@@ -2912,32 +2912,90 @@
 ame);%0A        %7D%0A
+        File%5B%5D listFiles = release.getPath().listFiles();%0A
         if (libr
@@ -3039,42 +3039,45 @@
 rs()
-%0A            && release.getPath().
+ && listFiles != null%0A            && 
 list
@@ -3081,18 +3081,16 @@
 istFiles
-()
 .length 
@@ -3106,16 +3106,36 @@
         
+ boolean isDeleted =
  release
@@ -3296,11 +3296,720 @@
    %7D%0A  %7D
+%0A%0A  private String getNewFilename(File f, Release release) %7B%0A    String filename = %22%22;%0A    FilenameLibraryBuilder filenameLibraryBuilder = new FilenameLibraryBuilder(librarySettings);%0A    filename = filenameLibraryBuilder.build(release);%0A    if (release.getExtension().equals(%22srt%22)) %7B%0A      String languageCode = %22%22;%0A      try %7B%0A        if (librarySettings.isLibraryIncludeLanguageCode()) %7B%0A          languageCode = DetectLanguage.execute(f);%0A        %7D%0A      %7D catch (final Exception e) %7B%0A        Logger.instance.error(%22Unable to detect language, leaving language code blank%22);%0A      %7D%0A%0A      filename = filenameLibraryBuilder.buildSubtitle(release, filename, languageCode, 0);%0A    %7D%0A    return filename;%0A  %7D
 %0A%7D%0A

Improve readabilty