package nak.nakloidGUI.actions.executors;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
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.jface.dialogs.MessageDialog;

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

public class InitializePitchesAction extends AbstractAction {
	public InitializePitchesAction(MainWindow mainWindow, CoreData coreData) {
		super(mainWindow, coreData);
		setText("ピッチ情報の再計算");
	}

	@Override
	public void run() {
		if (coreData.getScoreLength() == 0) {
			MessageDialog.openError(mainWindow.getShell(), "NakloidGUI", "音符が見つからないためピッチ情報が作成できません。");
			return;
		}
		final Path tmpInputPath = coreData.nakloidIni.input.path_input_pitches;
		try {
			Files.deleteIfExists(tmpInputPath);
		} 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;
		}
		if (coreData.getSongWaveform() != null) {
			coreData.getSongWaveform().pause();
			coreData.getSongWaveform().close();
		}
		mainWindow.showWaveformStatus("歌声合成中...");
		mainWindow.flushLoggerWindow();
		try {
			CoreData tmpCoreData = new CoreData.Builder()
					.loadScore(coreData.getScorePath())
					.loadOtoIni(coreData.getVocalPath())
					.build();
			tmpCoreData.nakloidIni.input.path_input_pitches = null;
			tmpCoreData.nakloidIni.output.path_output_pitches = tmpInputPath;
			tmpCoreData.nakloidIni.input.pitches_mode = NakloidIni.PitchesMode.pitches_mode_none;
			tmpCoreData.synthesize(new CoreDataSynthesisListener() {
				@Override
				public void synthesisFinished() {
					try {
						coreData.reloadPitches();
					} 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));
					}
				}
			});
		} catch (IOException e) {
			ErrorDialog.openError(mainWindow.getShell(), "NakloidGUI",
					"楽譜情報の読み込みに失敗又は歌声合成のファイルの入出力時にエラーが発生しました。\ntemporaryフォルダ及びNakloid.iniに書き込み権限があるか確認してください。",
					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));
		} catch (InterruptedException 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));
		}
	}
}

@@ -1023,16 +1023,134 @@
 rn;%0A%09%09%7D%0A
+%09%09if (!MessageDialog.openConfirm(mainWindow.getShell(), %22NakloidGUI%22, %22%E7%8F%BE%E5%9C%A8%E3%81%AE%E3%83%94%E3%83%83%E3%83%81%E6%83%85%E5%A0%B1%E3%81%AF%E5%85%A8%E3%81%A6%E7%A0%B4%E6%A3%84%E3%81%95%E3%82%8C%E3%81%BE%E3%81%99%E3%80%82%E3%82%88%E3%82%8D%E3%81%97%E3%81%84%E3%81%A7%E3%81%99%E3%81%8B%EF%BC%9F%22)) %7B%0A%09%09%09return;%0A%09%09%7D%0A
 %09%09final 

add destroy pitches confirm dialog