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];

    if (logCurve == null) {
      this.tooltip.hide();
    } else {
      const data = logCurve.getDataSource();
      // console.log('data',data)
      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());
      // console.log('data.getSize()',data.getSize())
      // console.log('logCurve.getInterpolationType()',logCurve.getInterpolationType())
      // console.log('cursorValue',cursorValue)

      let finalDepth, finalValue;
      if (maxIndex - minIndex <= 1) {
        finalDepth = cursor.getY();
        finalValue = cursorValue;
        // console.log('cursorValue',finalValue)
      } 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());
          // console.log('finalValue if',finalValue)
        } else if (minValueDis < cursorValue) {
          finalDepth = data.getDepth(minValueDisIndex);
          finalValue = data.getValue(minValueDisIndex);
          // console.log('finalValue  else if',finalValue)

        } else {
          finalDepth = cursor.getY();
          finalValue = cursorValue;
          // console.log('finalValue  else ',finalValue)

        }
      }
      let string = finalValue != null ? finalValue.toString() : '0';
      finalValue = string.substring(0,string.indexOf('.')+3)
      // console.log('finalValue',finalValue)
      const text = '名称: ' + logCurve.getName() + '<br>' +
        '垂深: ' + finalDepth.toFixed(2) + '<br>' +
        '值: ' + (finalValue || '0');
      const native = eventArgs.getNativeEventArgs();
      this.tooltip.show(native.clientX, native.clientY, text);
    }
  }
}
obfuscate(TooltipTool);
