package org.bonej.wrapperPlugins;

import net.imagej.ImgPlus;
import net.imagej.ops.OpService;
import net.imglib2.type.numeric.RealType;
import org.bonej.ops.ThresholdElementFraction;
import org.bonej.ops.ThresholdElementFraction.Results;
import org.bonej.ops.ThresholdElementFraction.Settings;
import org.bonej.utilities.ElementUtil;
import org.bonej.utilities.ResultsInserter;
import org.scijava.command.Command;
import org.scijava.command.ContextCommand;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.UIService;

import static org.bonej.wrapperPlugins.CommonMessages.*;
import static org.scijava.ui.DialogPrompt.MessageType.WARNING_MESSAGE;

/**
 * A wrapper UI class for the ThresholdElementFraction Op
 *
 * @author Richard Domander
 */
@Plugin(type = Command.class, menuPath = "Plugins>BoneJ>Fraction>Area/Volume fraction", headless = true)
public class ThresholdElementFractionWrapper<T extends RealType<T>> extends ContextCommand {
    @Parameter(initializer = "initializeImage")
    private ImgPlus<T> inputImage;

    @Parameter
    private OpService opService;

    @Parameter
    private UIService uiService;

    @Override
    public void run() {
        final T element = inputImage.cursor().get();
        final T minThreshold = element.createVariable();
        final T maxThreshold = element.createVariable();
        minThreshold.setReal(127);
        maxThreshold.setReal(255);
        final Settings<T> settings = new Settings<>(minThreshold, maxThreshold);

        final Results results = (Results) opService.run(ThresholdElementFraction.class, inputImage, settings);

        showResults(results);
    }

    private void showResults(final Results results) {
        final String label = inputImage.getName();
        //TODO: Determine exponent
        final String unitHeader = WrapperUtils.getUnitHeader(inputImage);

        if (unitHeader.isEmpty()) {
            uiService.showDialog(BAD_CALIBRATION, WARNING_MESSAGE);
        }

        //TODO: adjust results to calibration

        //TODO: In headers, show area or volume based on image dimensionality
        final ResultsInserter resultsInserter = ResultsInserter.getInstance();
        resultsInserter.setMeasurementInFirstFreeRow(label, "Bone volume " + unitHeader, results.thresholdElements);
        resultsInserter.setMeasurementInFirstFreeRow(label, "Total volume " + unitHeader, results.elements);
        resultsInserter.setMeasurementInFirstFreeRow(label, "Ratio", results.ratio);
        resultsInserter.updateResults();
    }

    @SuppressWarnings("unused")
    private void initializeImage() {
        if (inputImage == null) {
            cancel(NO_IMAGE_OPEN);
            return;
        }

        if (!ElementUtil.isColorsBinary(inputImage)) {
            cancel(NOT_BINARY);
        }
    }
}

@@ -288,16 +288,54 @@
 ttings;%0A
+import org.bonej.utilities.AxisUtils;%0A
 import o
@@ -1811,24 +1811,25 @@
 Name();%0A
+%0A
         
 //TODO: 
@@ -1824,34 +1824,542 @@
     
-//TODO: Determine exponent
+final long dimensions = AxisUtils.countSpatialDimensions(inputImage);%0A        final String sizeDescription;%0A        if (dimensions == 2) %7B%0A            sizeDescription = %22Area %22;%0A        %7D else if (dimensions == 3) %7B%0A            sizeDescription = %22Volume %22;%0A        %7D else %7B%0A            sizeDescription = %22Size %22;%0A        %7D%0A%0A        final String exponent;%0A        if (dimensions == 2) %7B%0A            exponent = %22%C2%B2%22;%0A        %7D else if (dimensions == 3) %7B%0A            exponent = %22%C2%B3%22;%0A        %7D else %7B%0A            exponent = %22%22;%0A        %7D%0A
 %0A   
@@ -2426,16 +2426,26 @@
 putImage
+, exponent
 );%0A%0A    
@@ -2567,124 +2567,230 @@
     
-//TODO: adjust results to calibration%0A%0A        //TODO: In headers, show area or volume based on image dimensionality
+final double elementSize = AxisUtils.calibratedSpatialElementSize(inputImage);%0A        final double thresholdSize = results.thresholdElements * elementSize;%0A        final double totalSize = results.elements * elementSize;%0A
 %0A   
@@ -2931,24 +2931,35 @@
 , %22Bone 
-volume %22
+%22 + sizeDescription
  + unitH
@@ -2965,24 +2965,16 @@
 Header, 
-results.
 threshol
@@ -2974,24 +2974,20 @@
 hreshold
-Elements
+Size
 );%0A     
@@ -3052,47 +3052,51 @@
 tal 
-volume %22 + unitHeader, results.elements
+%22 + sizeDescription + unitHeader, totalSize
 );%0A 
@@ -3154,17 +3154,36 @@
 (label, 
-%22
+sizeDescription + %22 
 Ratio%22, 

Add table headers