/*****************************************************************************
 *  Limpet - the Lightweight InforMation ProcEssing Toolkit
 *  http://limpet.info
 *
 *  (C) 2015-2016, Deep Blue C Technologies Ltd
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the Eclipse Public License v1.0
 *  (http://www.eclipse.org/legal/epl-v10.html)
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *****************************************************************************/
package info.limpet.ui.xy_plot;

import info.limpet.IStoreItem;
import info.limpet.impl.NumberDocument;
import info.limpet.ui.xy_plot.Helper2D.HContainer;

import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.january.dataset.DoubleDataset;
import org.eclipse.nebula.visualization.widgets.datadefinition.ColorMap;
import org.eclipse.nebula.visualization.widgets.datadefinition.ColorMap.PredefinedColorMap;
import org.eclipse.nebula.visualization.widgets.figures.IntensityGraphFigure;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

/**
 * display analysis overview of selection
 * 
 * @author ian
 * 
 */
public class HeatmapView extends CommonGridView
{

  /**
   * The ID of the view as specified by the extension.
   */
  public static final String ID = "info.limpet.ui.HeatMapView";

  private IntensityGraphFigure intensityGraph;
  private Canvas parentCanvas;

  public HeatmapView()
  {
    super(ID, "Heatmap view");
  }

  protected void clearChart()
  {
    intensityGraph.setDataArray(new double[]
    {});
  }

  /**
   * This is a callback that will allow us to create the viewer and initialize it.
   */
  @Override
  public void createPartControl(final Composite parent)
  {
    // Create the composite to hold the controls
    final Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(1, false));

    // Create the combo to hold the team names
    titleLbl = new Text(composite, SWT.NONE);
    titleLbl.setText(" ");

    // insert the holder for the heatmap
    parentCanvas = new Canvas(composite, SWT.NULL);
    parentCanvas.setLayoutData(new GridData(GridData.FILL_BOTH));

    // Create Intensity Graph
    intensityGraph = new IntensityGraphFigure();

    // use LightweightSystem to create the bridge between SWT and draw2D
    final LightweightSystem lws = new LightweightSystem(parentCanvas);
    lws.setContents(intensityGraph);

    // ok, layout the panel
    parentCanvas.pack();

    makeActions();
    contributeToActionBars();

    // register as selection listener
    setupListener();
  }

  @Override
  protected String getTextForClipboard()
  {
    return "Pending";
  }

  @Override
  public void setFocus()
  {
    parentCanvas.setFocus();
  }

  protected void show(final IStoreItem item)
  {
    final NumberDocument thisQ = (NumberDocument) item;

    clearChart();

    final String seriesName = thisQ.getName();
    titleLbl.setText(seriesName + " (" + thisQ.getUnits().toString() + ")");

    // get the data
    final HContainer hData =
        Helper2D.convert((DoubleDataset) thisQ.getDataset());

    final int rows = hData.rowTitles.length;
    final int cols = hData.colTitles.length;

    final int DataHeight = 1024;
    final int DataWidth = 1024;

    final double rowStep = DataHeight / rows;
    final double colStep = DataWidth / cols;

    final double[] data = new double[DataWidth * DataHeight];

    for (int i = 0; i < DataHeight; i++)
    {
      for (int j = 0; j < DataWidth; j++)
      {
        final int thisI = (int) (i / rowStep);
        final int thisJ = (int) (j / colStep);
        final double thisVal = hData.values[thisI][thisJ];
        data[i + j * DataHeight] = thisVal;
      }
    }

    final DescriptiveStatistics stats = new DescriptiveStatistics(data);

    if (Double.isNaN(stats.getMax()))
    {
      // ok, drop out, we haven't got any data
      return;
    }

    // Configure
    intensityGraph.setMax(stats.getMax());
    intensityGraph.setMin(stats.getMin());
    intensityGraph.setDataHeight(DataHeight);
    intensityGraph.setDataWidth(DataWidth);
    intensityGraph
        .setColorMap(new ColorMap(PredefinedColorMap.JET, true, true));
    intensityGraph.setDataArray(data);

    // parentCanvas.pack();
    titleLbl.pack();
  }

}

@@ -1229,16 +1229,83 @@
 Figure;%0A
+import org.eclipse.nebula.visualization.xygraph.linearscale.Range;%0A
 import o
@@ -3703,119 +3703,27 @@
 t = 
-1024;%0A    final int DataWidth = 1024;%0A%0A    final double rowStep = DataHeight / rows;%0A    final double colStep =
+rows;%0A    final int
  Dat
@@ -3729,17 +3729,17 @@
 taWidth 
-/
+=
  cols;%0A%0A
@@ -3902,102 +3902,8 @@
   %7B%0A
-        final int thisI = (int) (i / rowStep);%0A        final int thisJ = (int) (j / colStep);%0A
     
@@ -3946,20 +3946,12 @@
 ues%5B
-thisI%5D%5BthisJ
+i%5D%5Bj
 %5D;%0A 
@@ -4514,24 +4514,331 @@
 ray(data);%0A%0A
+    // sort out the axis ranges%0A    intensityGraph.getXAxis().setRange(%0A        new Range(hData.rowTitles%5B0%5D,%0A            hData.rowTitles%5BhData.rowTitles.length - 1%5D));%0A    intensityGraph.getYAxis().setRange(%0A        new Range(hData.colTitles%5B0%5D,%0A            hData.colTitles%5BhData.colTitles.length - 1%5D));%0A%0A
     // paren

improve performance, set the axis range to match our index values