import {LogCurve} from '@int/geotoolkit/welllog/LogCurve';
import {Selector} from '@int/geotoolkit/selection/Selector';
import {AbstractTool} from '@int/geotoolkit/controls/tools/AbstractTool';
import {obfuscate} from '@int/geotoolkit/lib';
import {Tooltip} from './tooltip';


export class TooltipTool extends AbstractTool {
  constructor (widget) {
    super();
    this.setSlots({
      'mousemove': this.onMouseMove.bind(this),
      'MSPointerMove': this.onMouseMove.bind(this),
      'pointermove': this.onMouseMove.bind(this),
      'touchmove': this.onMouseMove.bind(this),
      'touchstart': this.onMouseMove
    });
    this.widget = widget;
    this.selector = new Selector();
    this.tooltip = new Tooltip('tooltip-container');
    console.log('this.tooltip',this.tooltip)
  }

  onMouseMove (eventArgs) {
    const pt = eventArgs.getPlotPoint();
    // select nodes
    const mouseTolerance = 2;
    const logCurve = this.selector.select(this.widget, pt.getX(), pt.getY(), mouseTolerance)
      .filter((node) => (node instanceof LogCurve))[0];
    console.log('logCurve',logCurve)
    if (logCurve == null || logCurve == undefined) {
      this.tooltip.hide();
    } else {
      console.log('logCurve11',logCurve)
      const data = logCurve.getDataSource();
      const tr = logCurve.getSceneTransform().createInverse(); // device to model
      const minDepth = tr.transformY(pt.getX(), pt.getY() - mouseTolerance);
      const maxDepth = tr.transformY(pt.getX(), pt.getY() + mouseTolerance);
      const minIndex = Math.max(0, data.getIndexAt(minDepth) - 1);
      const maxIndex = data.getIndexAt(maxDepth) + 1;
      const cursor = tr.transform(pt);
      const cursorValue = data.getValueAt(cursor.getY(), 0, data.getSize(), logCurve.getInterpolationType());
      let finalDepth, finalValue;
      if (maxIndex - minIndex <= 1) {
        finalDepth = cursor.getY();
        finalValue = cursorValue;
      } else {
        const valueLimits = logCurve.getMaximumNormalizationLimit() - logCurve.getMinimumNormalizationLimit();
        const value = logCurve.getMinimumNormalizationLimit() + cursor.getX() * valueLimits;
        let xIndex = null, yIndex = null;
        let minValueDis = Infinity, minValueDisIndex = null;
        for (let j = minIndex; j < maxIndex; j++) {
          if (data.getDepth(j) < cursor.getY() && data.getDepth(j + 1) > cursor.getY()) {
            yIndex = j;
          }
          if ((data.getValue(j) > value) !== (data.getValue(j + 1) > value)) {
            xIndex = j;
          }
          if (minValueDis > Math.abs(data.getValue(j) - value)) {
            minValueDis = Math.abs(data.getValue(j) - value);
            minValueDisIndex = j;
          }
          if (xIndex !== null && yIndex !== null) break;
        }
        if (xIndex !== null) {
          const depth1 = data.getDepth(xIndex);
          const depth2 = data.getDepth(xIndex + 1);
          finalDepth = depth1 + (depth2 - depth1) * (value - data.getValue(xIndex)) /
            (data.getValue(xIndex + 1) - data.getValue(xIndex));
          finalValue = data.getValueAt(finalDepth, 0, data.getSize(), logCurve.getInterpolationType());
        } else if (minValueDis < cursorValue) {
          finalDepth = data.getDepth(minValueDisIndex);
          finalValue = data.getValue(minValueDisIndex);
        } else {
          finalDepth = cursor.getY();
          finalValue = cursorValue;
        }
      }
      const text = 'Name: ' + logCurve.getName() + '<br>' +
        'Depth: ' + finalDepth.toFixed(2) + '<br>' +
        'Value: ' + finalValue.toFixed(2);
      console.log('text',text)
      const native = eventArgs.getNativeEventArgs();
      console.log('native',native)

      console.log('native.pageX',native.clientX)

      console.log('native.pageX',native.pageX)
      console.log('native.pageX',native.pageY)

      this.tooltip.show(native.pageX, native.pageY, text);
      console.log('text',text)
    }
  }
}
obfuscate(TooltipTool);
