/*
 * #%L
 * ImageJ software for multidimensional image processing and analysis.
 * %%
 * Copyright (C) 2009 - 2015 Board of Regents of the University of
 * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
 * Institute of Molecular Cell Biology and Genetics.
 * %%
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * #L%
 */

package net.imagej.matlab;

import javax.script.ScriptEngine;

import net.imagej.Dataset;
import net.imagej.display.ImageDisplayService;

import org.scijava.log.LogService;
import org.scijava.plugin.AbstractRichPlugin;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.plugins.scripting.matlab.MATLABCommands;
import org.scijava.plugins.scripting.matlab.MATLABService;
import org.scijava.script.ScriptService;
import org.scijava.ui.UIService;

import matlabcontrol.extensions.MatlabNumericArray;

/**
 * {@link MATLABCommands} suite for converting ImageJ data structures to MATLAB
 * structures.
 *
 * @author Mark Hiner
 */
@Plugin(type = MATLABCommands.class, name = ImageJMATLABCommands.NAME)
public class ImageJMATLABCommands extends AbstractRichPlugin implements
	MATLABCommands
{

	@Parameter(required = false)
	private ImageDisplayService imageDisplayService;

	@Parameter(required = false)
	private UIService uiService;

	@Parameter
	private LogService logService;

	@Parameter
	private ScriptService scriptService;

	@Parameter
	private ImageJMATLABService ijmService;

	@Parameter
	private MATLABService matlabService;

	public static final String NAME = "IJM";

	// -- ImageJ-MATLAB commands --

	/**
	 * Converts the active {@link Dataset} to a MATLAB matrix, stored in a
	 * variable using {@link Dataset#getName()}.
	 */
	public void getDataset() {
		final Dataset activeDataset = imageDisplayService.getActiveDataset();

		importDataset(activeDataset.getName(), activeDataset);
	}

	/**
	 * As {@link #getDataset()}, using the specified variable name.
	 */
	public void getDatasetAs(final String name) {
		final Dataset activeDataset = imageDisplayService.getActiveDataset();

		importDataset(name, activeDataset);
	}

	/**
	 * Take an array variable in MATLAB and attempt to display it as a Dataset
	 * in ImageJ
	 */
	public void show(final String matrix) {
		if (uiService == null) {
			logService.info("No UI available to display array");
			return;
		}

		final ScriptEngine engine =
				scriptService.getLanguageByName("MATLAB").getScriptEngine();
		final Object o = engine.get(matrix);
		MatlabNumericArray array = null;

		if (o instanceof MatlabNumericArray) array = (MatlabNumericArray) o;

		if (array == null) {
			logService.info("Variable of name: " + matrix + " is not an array.");
			return;
		}

		uiService.show(ijmService.getDataset(array));
	}

	// -- MATLABCommands methods --

	@Override
	public String help() {
		final String usage =
			"-- ImageJ MATLAB commands --\n\n" + "Usage: IJM.[command]\n"
				+ "\thelp - give a brief description of available commands\n"
				+ "\tgetDataset - creates a MATLAB matrix from the active dataset\n"
				+ "\tgetDatasetAs(name) - creates a MATLAB matrix from the active "
				+ "dataset and assigns it to the specified variable name\n"
				+ "\tshow(name) - takes the MATLAB matrix with the specified name and displays it as an image";
		return usage;
	}

	// -- Helper methods --

	/**
	 * Helper method to perform {@link Dataset} conversion, and set the variable
	 * within MATLAB.
	 */
	private void importDataset(final String name, final Dataset dataset) {
		if (dataset == null) {
			logService.error("No active dataset to import to MATLAB.");
		}

		// Convert the active dataset to a MATLAB-compatible array.
		final MatlabNumericArray matrix = ijmService.getArray(dataset);

		matlabService.makeMATLABVariable(name, matrix);
	}

}

@@ -3005,133 +3005,26 @@
 %7B%0A%09%09
-final Dataset activeDataset = imageDisplayService.getActiveDataset();%0A%0A%09%09importDataset(activeDataset.getName(), activeDataset
+importDataset(null
 );%0A%09
@@ -3152,81 +3152,8 @@
 ) %7B%0A
-%09%09final Dataset activeDataset = imageDisplayService.getActiveDataset();%0A%0A
 %09%09im
@@ -3172,23 +3172,8 @@
 name
-, activeDataset
 );%0A%09
@@ -3998,12 +3998,14 @@
 p - 
-give
+prints
  a b
@@ -4108,23 +4108,28 @@
  active 
-dataset
+ImageJ image
 %5Cn%22%0A%09%09%09%09
@@ -4203,23 +4203,29 @@
 %0A%09%09%09%09+ %22
-dataset
+ImageJ image,
  and ass
@@ -4547,22 +4547,16 @@
 Dataset(
-final 
 String n
@@ -4558,18 +4558,23 @@
 ing name
-, 
+) %7B%0A%0A%09%09
 final Da
@@ -4583,26 +4583,78 @@
 set 
-dataset) %7B%0A%09%09if (d
+activeDataset = imageDisplayService.getActiveDataset();%0A%0A%09%09if (activeD
 atas
@@ -4685,13 +4685,12 @@
 ice.
-error
+info
 (%22No
@@ -4701,43 +4701,123 @@
 ive 
-dataset to import to MATLAB.%22);%0A%09%09%7D
+image. Please open an image in ImageJ first.%22);%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09if (name == null) name = activeDataset.getName();
 %0A%0A%09%09
@@ -4932,17 +4932,23 @@
 etArray(
-d
+activeD
 ataset);

unify "no dataset" logic