import React from 'react';
import {Group} from 'react-art';
import Circle from 'react-art/shapes/circle';
import MetricsPath from 'art/metrics/path';
import Utils from '../utils/DrawingUtils.js';
import { CURRENT } from './Constants.js';

const STANDING_OFFSET = CURRENT.DOT_DISTANCE / 2;

/**
 * Displays current flow along a path.
 */
const CurrentPath = (
    {
      current = 0,
      currentOffset,
      endPoints
    },
    {
      disableCurrent = false,
      theme
    }
  ) => {
  const circles = [],
        showCurrent = !disableCurrent;
  if (showCurrent) {
    const [end1, end2] = endPoints,
          d = Utils.diff(end1, end2),

          path = new MetricsPath()
            .moveTo(end1.x, end1.y)
            .lineTo(end2.x, end2.y),

          fiddleCurrent = current / 10, // FIXME no magic fiddles

          offset = (fiddleCurrent * currentOffset + STANDING_OFFSET) % CURRENT.DOT_DISTANCE,
          startPos = current >= 0
            ? offset
            : offset + CURRENT.DOT_DISTANCE;
    let key = 0;
    for (let position = startPos; position <= d.length(); position += CURRENT.DOT_DISTANCE) {
      // TODO pool Circle instances?
      circles.push(
        <Circle
          key={key++}
          radius={CURRENT.RADIUS}
          fill={theme.COLORS.current}
          transform={path.point(position)}
        />
      );
    }
  }
  return (
    <Group>
      {circles ? circles : null}
    </Group>
  );
};

CurrentPath.propTypes = {
  endPoints: React.PropTypes.arrayOf(Utils.PropTypes.Vector).isRequired,
  current: React.PropTypes.number,
  currentOffset: React.PropTypes.number
};

CurrentPath.contextTypes = {
  disableCurrent: React.PropTypes.bool, // can't set defaultContext so defaults to falsy
  theme: React.PropTypes.object.isRequired
};

export default CurrentPath;

@@ -1014,24 +1014,8 @@
 CE;%0A
-    let key = 0;
 %0A   
@@ -1043,16 +1043,25 @@
 startPos
+, key = 0
 ; positi
@@ -1110,16 +1110,23 @@
 DISTANCE
+, key++
 ) %7B%0A    
@@ -1194,16 +1194,16 @@
 %3CCircle%0A
+
         
@@ -1212,18 +1212,16 @@
 key=%7Bkey
-++
 %7D%0A      

Increment current dot key in loop