package nak.nakloidGUI.actions.executors;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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.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 ExportWavAction extends AbstractAction {
	final String[] ext = {"*.wav"};
	final String [] filterNames = {"RIFF waveform Audio Format (*.wav)"};

	public ExportWavAction(MainWindow mainWindow, CoreData coreData) {
		super(mainWindow, coreData);
		setText("書き出し(&E)@Ctrl+E");
		setAccelerator(SWT.CTRL+'E');
	}

	@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;
		}
		mainWindow.buildAction.run();
		Path pathExportWav = Paths.get(strPath);
		Path pathWav = coreData.nakloidIni.output.path_song;
		try {
			Files.deleteIfExists(pathExportWav);
			Files.copy(pathWav, pathExportWav, StandardCopyOption.REPLACE_EXISTING);
		} catch (IOException e) {
			ErrorDialog.openError(mainWindow.getShell(), "NakloidGUI",
					"wavファイル出力のためのファイル操作に失敗しました。",
					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));
		}
	}
}

@@ -36,16 +36,37 @@
 utors;%0A%0A
+import java.io.File;%0A
 import j
@@ -450,16 +450,64 @@
 Dialog;%0A
+import org.eclipse.jface.dialogs.MessageDialog;%0A
 import o
@@ -1217,16 +1217,45 @@
 %09String 
+strPath;%0A%09%09while (true) %7B%0A%09%09%09
 strPath 
@@ -1275,16 +1275,17 @@
 open();%0A
+%09
 %09%09if (st
@@ -1327,16 +1327,17 @@
 %0A%09%09%09
+%09
 return;%0A
 %09%09%7D%0A
@@ -1332,16 +1332,198 @@
 return;%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%09ma

improve wav export dialog