package nak.nakloidGUI.actions.files;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
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.swt.SWT;

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

public class SaveAction extends AbstractAction {
	final String[] ext = {"*.nar"};
	final String [] filterNames = {"Nakloid Archive (*.nar)"};

	public SaveAction(MainWindow mainWindow, CoreData coreData) {
		super(mainWindow, coreData);
		setText("保存@Ctrl+S");
		setAccelerator(SWT.CTRL+'S');
	}

	@Override
	public void run() {
		String strPath = NakloidGUI.preferenceStore.getString("workspace.path_nar");
		if (strPath==null || strPath.isEmpty()) {
			mainWindow.saveAsAction.run();
			return;
		}
		Path pathNar = Paths.get(strPath);
		Path pathScore = coreData.nakloidIni.input.path_input_score;
		Path pathPitches = coreData.nakloidIni.input.path_input_pitches;
		byte[] buf = new byte[1024];
		ZipOutputStream zos = null;
		try {
			zos = new ZipOutputStream(new FileOutputStream(pathNar.toFile()));
			zos.putNextEntry(new ZipEntry(pathScore.getFileName().toString()));
			try (InputStream is = new BufferedInputStream(new FileInputStream(pathScore.toFile()))) {
				while (true) {
					int len = is.read(buf);
					if (len < 0) break;
					zos.write(buf, 0, len);
				}
			}
			zos.putNextEntry(new ZipEntry(pathPitches.getFileName().toString()));
			try (InputStream is = new BufferedInputStream(new FileInputStream(pathPitches.toFile()))) {
				while (true) {
					int len = is.read(buf);
					if (len < 0) break;
					zos.write(buf, 0, len);
				}
			}
			NakloidGUI.preferenceStore.save();
		} 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));
		} finally {
			try {
				zos.close();
			} catch (IOException e) {}
		}
	}
}

@@ -1244,16 +1244,561 @@
 rn;%0A%09%09%7D%0A
+%09%09try %7B%0A%09%09%09coreData.saveScore();%0A%09%09%09coreData.savePitches();%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09ErrorDialog.openError(mainWindow.getShell(), %22NakloidGUI%22,%0A%09%09%09%09%09%22%E4%BF%9D%E5%AD%98%E6%99%82%E3%81%AE%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E5%85%A5%E5%87%BA%E5%8A%9B%E3%81%AB%E5%A4%B1%E6%95%97%E3%81%97%E3%81%BE%E3%81%97%E3%81%9F%E3%80%82%22,%0A%09%09%09%09%09new MultiStatus(%22.%22, IStatus.ERROR,%0A%09%09%09%09%09%09%09Stream.of(e.getStackTrace())%0A%09%09%09%09%09%09%09%09%09.map(s-%3Enew Status(IStatus.ERROR, %22.%22, %22at %22+s.getClassName()+%22: %22+s.getMethodName()))%0A%09%09%09%09%09%09%09%09%09.collect(Collectors.toList()).toArray(new Status%5B%5D%7B%7D),%0A%09%09%09%09%09%09%09e.getLocalizedMessage(), e));%0A%09%09%09try %7B%0A%09%09%09%09coreData.reloadScoreAndPitches();%0A%09%09%09%7D catch (IOException e1) %7B%7D%0A%09%09%09return;%0A%09%09%7D%0A
 %09%09Path p
@@ -3083,16 +3083,94 @@
 ), e));%0A
+%09%09%09try %7B%0A%09%09%09%09coreData.reloadScoreAndPitches();%0A%09%09%09%7D catch (IOException e1) %7B%7D%0A
 %09%09%7D fina

save action bugfix