package nak.nakloidGUI.actions.files;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;

import nak.nakloidGUI.actions.AbstractAction;
import nak.nakloidGUI.coredata.CoreData;
import nak.nakloidGUI.gui.MainWindow;

public class ExportVocalAction extends AbstractAction {
	final String[] ext = {"*.zip"};
	final String [] filterNames = {"ZIP (*.zip)"};

	public ExportVocalAction(MainWindow mainWindow, CoreData coreData) {
		super(mainWindow, coreData);
		setText("ボーカルをエクスポート");
	}

	@Override
	public void run() {
		FileDialog openDialog = new FileDialog(mainWindow.getShell(), SWT.SAVE);
		openDialog.setFilterExtensions(ext);
		openDialog.setFilterNames(filterNames);
		String strPath = openDialog.open();
		if (strPath==null || strPath.isEmpty()) {
			return;
		}
		Path pathExportVocal = Paths.get(strPath);

		System.out.println("ボーカルのエクスポートを開始します...");
		List<File> vocalFiles = null;
		try(Stream<Path> is = Files.walk(coreData.nakloidIni.input.path_singer)) {
			vocalFiles = is.map(Path::toFile).filter(File::isFile).collect(Collectors.toList());
		} catch (IOException e) {
			return;
		}
		int numParentDirectory = coreData.nakloidIni.input.path_singer.toString().length();
		try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(pathExportVocal.toFile())), Charset.forName("Shift_JIS"))) {
			zos.setLevel(0);
			byte[] buf = new byte[1024];
			for (File vocalFile : vocalFiles) {
				System.out.println(vocalFile.toString());
				ZipEntry entry = new ZipEntry(vocalFile.toString().substring(numParentDirectory));
				zos.putNextEntry(entry);
				try (FileInputStream fis = new FileInputStream(vocalFile)) {
					int len = 0;
					while ((len = fis.read(buf)) != -1) {
						zos.write(buf, 0, len);
					}
				}
			}
		} catch (IOException e) {
			ErrorDialog.openError(mainWindow.getShell(), "NakloidGUI",
					"ボーカルのエクスポートに失敗しました。",
					new MultiStatus(".", IStatus.ERROR,
							Stream.of(e.getStackTrace())
									.map(s->new Status(IStatus.ERROR, ".", "at "+s.getClassName()+": "+s.getMethodName()))
									.collect(Collectors.toList()).toArray(new Status[]{}),
							e.getLocalizedMessage(), e));
			return;
		}
		MessageDialog.openInformation(mainWindow.getShell(), "NakloidGUI", (coreData.getVocalInfo()==null)?"ボーカル":coreData.getVocalInfo().getName()+"をエクスポートしました。");
	}
}

@@ -1344,16 +1344,45 @@
 %09String 
+strPath;%0A%09%09while (true) %7B%0A%09%09%09
 strPath 
@@ -1402,16 +1402,17 @@
 open();%0A
+%09
 %09%09if (st
@@ -1446,32 +1446,33 @@
 pty()) %7B%0A%09%09%09
+%09
 return;%0A
 %09%09%7D%0A%09%09Path p
@@ -1455,24 +1455,206 @@
 %09%09%09%09return;%0A
+%09%09%09%7D%0A%09%09%09File tmpFile = new File(strPath);%0A%09%09%09if (!tmpFile.exists() %7C%7C MessageDialog.openConfirm(mainWindow.getShell(), %22NakloidGUI%22, %22%E9%81%B8%E6%8A%9E%E3%81%95%E3%82%8C%E3%81%9F%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%AF%E6%97%A2%E3%81%AB%E5%AD%98%E5%9C%A8%E3%81%97%E3%81%BE%E3%81%99%E3%80%82%E4%B8%8A%E6%9B%B8%E3%81%8D%E3%81%97%E3%81%BE%E3%81%99%E3%81%8B%EF%BC%9F%22)) %7B%0A%09%09%09%09break;%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%09Path p

improve vocal export dialog