package com.nincraft.modpackdownloader.util;

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

import org.apache.commons.io.FileUtils;

import lombok.val;
import lombok.experimental.UtilityClass;
import lombok.extern.log4j.Log4j2;

@Log4j2
@UtilityClass
public final class FileSystemHelper {

	public static void createFolder(final String folder) {
		if (folder != null) {
			final File dir = new File(folder);
			if (!dir.exists()) {
				dir.mkdirs();
			}
		}
	}

	public static void copyToLocalRepo(final String projectName, final File downloadedFile) {
		val newProjectName = getProjectNameOrDefault(projectName);

		try {
			FileUtils.copyFileToDirectory(downloadedFile, new File(Reference.userhome + newProjectName));
		} catch (final IOException e) {
			log.error(String.format("Could not copy %s to local repo.", newProjectName), e);
		}
	}

	public static void copyToLocalRepo(final String projectName, File downloadedFile, String originalName) {
		val newProjectName = getProjectNameOrDefault(projectName);

		try {
			File copyFile = getLocalFile(downloadedFile.getName(), newProjectName);
			FileUtils.copyFileToDirectory(downloadedFile, new File(Reference.userhome + newProjectName));
			copyFile.renameTo(new File(copyFile.getParent() + File.separator + originalName));
		} catch (final IOException e) {
			log.error(String.format("Could not copy %s to local repo.", newProjectName), e);
		}
	}

	public static void copyFromLocalRepo(final String projectName, final String fileName, String folder) {
		copyFromLocalRepo(projectName, fileName, folder, null);
	}

	public static void copyFromLocalRepo(final String projectName, final String fileName, String folder, String rename) {
		val newProjectName = getProjectNameOrDefault(projectName);
		if (folder == null) {
			folder = Reference.modFolder;
		}
		try {
			File copyFile = getLocalFile(fileName, newProjectName);
			FileUtils.copyFileToDirectory(copyFile, new File(folder));
			if (rename != null) {
				copyFile.renameTo(new File(copyFile.getParent() + File.separator + rename));
			}
		} catch (final IOException e) {
			log.error(String.format("Could not copy %s from local repo.", newProjectName), e);
		}
	}

	public static boolean isInLocalRepo(final String projectName, final String fileName) {
		return getLocalFile(fileName, getProjectNameOrDefault(projectName)).exists();
	}

	public static File getDownloadedFile(final String fileName) {
		return getDownloadedFile(fileName, null);
	}

	public static String getProjectNameOrDefault(final String projectName) {
		return projectName != null ? projectName : "thirdParty";
	}

	public static File getLocalFile(final String fileName, final String newProjectName) {
		return new File(Reference.userhome + newProjectName + File.separator + fileName);
	}

	public static File getDownloadedFile(String fileName, String folder) {
		if (folder != null) {
			createFolder(folder);
			return new File(folder + File.separator + fileName);
		} else if (Reference.modFolder != null) {
			createFolder(Reference.modFolder);
			return new File(Reference.modFolder + File.separator + fileName);
		} else {
			return new File(fileName);
		}
	}
}

@@ -1822,33 +1822,38 @@
 ry %7B%0A%09%09%09File
- 
+Utils.
 copyFile
  = getLocalF
@@ -1832,35 +1832,44 @@
 leUtils.copyFile
- = 
+ToDirectory(
 getLocalFile(fil
@@ -1894,69 +1894,80 @@
 ame)
-;%0A%09%09%09FileUtils.copyFileToDirectory(copyFile, new File(folder)
+, new File(folder));%0A%09%09%09File downloadedFile = getDownloadedFile(fileName
 );%0A%09
@@ -1994,20 +1994,26 @@
 ) %7B%0A%09%09%09%09
-copy
+downloaded
 File.ren
@@ -2019,36 +2019,42 @@
 nameTo(new File(
-copy
+downloaded
 File.getParent()

Fix renaming local repo files