import external from '../externalModules.js';
import toolStyle from '../stateManagement/toolStyle.js';
import path from './path.js';

const defaultHandleRadius = 6;

/**
 * Draws proivded handles to the provided context
 * @public
 * @method drawHandles
 * @memberof Drawing
 *
 * @param {CanvasRenderingContext2D} context - Target context
 * @param {*} renderData - Cornerstone Tool's event detail
 * @param {Object[]} handles - An array of handle objects
 * @param {string} color - Handle color
 * @param {Object} [options] - Options object
 * @param {Boolean} [options.drawHandlesIfActive] - Whether the handles should only be drawn if Active (hovered/selected)
 * @returns {undefined}
 */
export default function(context, renderData, handles, color, options) {
  context.strokeStyle = color;

  Object.keys(handles).forEach(function(name) {
    const handle = handles[name];

    if (handle.drawnIndependently === true) {
      return;
    }

    if (options && options.drawHandlesIfActive === true && !handle.active) {
      return;
    }

    const lineWidth = handle.active
      ? toolStyle.getActiveWidth()
      : toolStyle.getToolWidth();
    const fillStyle = options && options.fill;

    path(
      context,
      {
        lineWidth,
        fillStyle,
      },
      context => {
        const handleCanvasCoords = external.cornerstone.pixelToCanvas(
          renderData.element,
          handle
        );

        const handleRadius = getHandleRadius(options);

        context.arc(
          handleCanvasCoords.x,
          handleCanvasCoords.y,
          handleRadius,
          0,
          2 * Math.PI
        );
      }
    );
  });
}

/**
 * Utility function to get the handleRadius
 * @private
 * @function getHandleRadius
 *
 * @param {Object} [options] - Options object
 * @param {Number} [options.handleRadius=6] - Handle radius to use
 * @returns {Number} handleRadius
 */
function getHandleRadius(options) {
  let handleRadius;

  if (options && options.handleRadius) {
    handleRadius = options.handleRadius;
  } else {
    handleRadius = defaultHandleRadius;
  }

  return handleRadius;
}

@@ -131,40 +131,8 @@
 ';%0A%0A
-const defaultHandleRadius = 6;%0A%0A
 /**%0A
@@ -477,32 +477,35 @@
 Object%7D %5Boptions
+=%7B%7D
 %5D - Options obje
@@ -555,16 +555,22 @@
 IfActive
+=false
 %5D - Whet
@@ -635,16 +635,60 @@
 lected)%0A
+ * @param %7BNumber%7D %5Boptions.handleRadius=6%5D%0A
  * @retu
@@ -771,24 +771,29 @@
 lor, options
+ = %7B%7D
 ) %7B%0A  contex
@@ -981,27 +981,16 @@
 (options
- && options
 .drawHan
@@ -1183,19 +1183,8 @@
 le =
- options &&
  opt
@@ -1434,25 +1434,32 @@
       const 
-h
+defaultH
 andleRadius 
@@ -1460,20 +1460,34 @@
 adius = 
-getH
+6;%0A        const h
 andleRad
@@ -1489,25 +1489,62 @@
 leRadius
-(
+ = 
 options
-)
+.handleRadius %7C%7C defaultHandleRadius
 ;%0A%0A     
@@ -1720,468 +1720,4 @@
 ;%0A%7D%0A
-%0A/**%0A * Utility function to get the handleRadius%0A * @private%0A * @function getHandleRadius%0A *%0A * @param %7BObject%7D %5Boptions%5D - Options object%0A * @param %7BNumber%7D %5Boptions.handleRadius=6%5D - Handle radius to use%0A * @returns %7BNumber%7D handleRadius%0A */%0Afunction getHandleRadius(options) %7B%0A  let handleRadius;%0A%0A  if (options && options.handleRadius) %7B%0A    handleRadius = options.handleRadius;%0A  %7D else %7B%0A    handleRadius = defaultHandleRadius;%0A  %7D%0A%0A  return handleRadius;%0A%7D%0A

Simplify drawHandles