package com.github.davidcarboni.dylan;

import com.github.davidcarboni.cryptolite.KeyExchange;
import com.github.davidcarboni.dylan.filesystem.CryptoFS;
import com.github.davidcarboni.dylan.filesystem.CryptoPath;
import com.github.davidcarboni.dylan.notify.Notifier;
import com.github.davidcarboni.dylan.sshd.SSHServer;
import com.github.davidcarboni.restolino.framework.Startup;
import org.apache.commons.io.FilenameUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


public class CSDBImport implements Startup {

	private FileSystem cryptoFileSystem = FileSystems.getFileSystem(CryptoFS.uri());

	private Consumer<Path> setScpFileReceivedHandler = (Path path) -> {
		File csdbFile = new File(Store.files.toString());

		if (!csdbFile.exists() && !csdbFile.mkdirs()) {
			throw new RuntimeException("csdb path not found");
		}

		try {
			Path csdbPath = cryptoFileSystem.getPath(csdbFile.getPath());

			ZipInputStream zis = new ZipInputStream(Files.newInputStream(path));
			ZipEntry entry;

			while ((entry = zis.getNextEntry()) != null) {
				System.out.println("Received CSDB file: " + entry.getName());

				if (!entry.isDirectory()) {
					Path dest = resolveFile(csdbPath, entry.getName());

					Files.copy(zis, dest);

					String encryptedKey = new KeyExchange().encryptKey(CryptoPath.getKey(dest), Store.getRecipientKey());
					Store.saveKey(keyName(dest), encryptedKey);

					Notifier.notify(dest);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				// TODO is this necessary? Should this be kept/moved/archived?
				Files.delete(path);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	};

	private Path resolveFile(Path path, String name) {
		// TODO REMOVE the UUID from the name - this is for local dev only. Allows us to upload the same file multiple times.
		return path.resolve(String.format("%s-%s.%s", FilenameUtils.getBaseName(name), UUID.randomUUID(),
				FilenameUtils.getExtension(name)));
	}

	private String keyName(Path p) {
		return String.format("%s.%s", FilenameUtils.getBaseName(CryptoPath.unwrap(p).getFileName().toString()), "key");
	}

	@Override
	public void init() {
		try {
			new SSHServer(setScpFileReceivedHandler).start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

@@ -492,130 +492,9 @@
 ile.
-FileSystem;%0Aimport java.nio.file.FileSystems;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.util.UUID
+*
 ;%0Aim
@@ -1355,16 +1355,53 @@
 is, dest
+, StandardCopyOption.REPLACE_EXISTING
 );%0A%0A%09%09%09%09
@@ -1526,20 +1526,22 @@
 Key(
-key
+entry.get
 Name(
-dest
 ), e
@@ -1920,263 +1920,32 @@
 %7B%0A%09%09
-// TODO REMOVE the UUID from the name - this is for local dev only. Allows us to upload the same file multiple times.%0A%09%09return path.resolve(String.format(%22%25s-%25s.%25s%22, FilenameUtils.getBaseName(name), UUID.randomUUID(),%0A%09%09%09%09FilenameUtils.getExtension(name))
+return path.resolve(name
 );%0A%09

Use the name of the csdb file instead of random name.