import Promise from 'bluebird'

export default class AbstractFractal {

  constructor({
    scale,
    epsilon, // length of each sub-segment
    dist,    // length of the main segment
    points = [ ],
    offset = 0,
    width  = window.innerWidth,
    height = window.innerHeight,
    depth  = 0 // also used as maxDepth... FIXME
  }) {
    if (new.target === AbstractFractal) {
      throw 'cannot instantiate an abstract fractal (unless you have the right drugs)'
    }

    this.scale    = scale
    this.epsilon  = epsilon // aka. scaleFactor or unit
    this.points   = points
    this.offset   = offset
    this.width    = width
    this.height   = height
    this.maxDepth = maxDepth

    this.canvas  = document.getElementById('canvas')
    this.context = this.canvas.getContext('2d')

    this.size = { height, width }
  }

  get data() {
    return this._datums
  }

  set data(data) {
    if (data instanceof Array) {
      this._datums = data
    }
  }

  get dimension() {
    // @see pg. 5 section 1.3.2, PAFS
  }

  set size({ height, width }) {
    this.height = this.canvas.height = height
    this.width  = this.canvas.width  = width
  }

  iteration(point, depth = this.depth) {
    this.context.save()
    this.context.translate(dist, 0)
    this.context.scale(scaleFactor, scaleFactor)

    drawShape(depth)

    if (depth > 0) {
      iteration(point, depth - 1) // TODO: actually map to points
    }

    this.context.restore()
  }

  iterate() { // TODO: integrate setInterval and clearInterval
    return this.points.map(this.iteration)
  }

  draw() {
    this.clear()

    return this.iterate()
  }

  drawShape() {
    return false
  }

  clear(color) {
    if (color) {
      this.context.fillStyle = color
      this.context.fillRect(0, 0, this.width, this.height)
    } else {
      this.context.clearRect(0, 0, this.width, this.height)
    }
  }

}

@@ -486,17 +486,16 @@
 scale   
- 
 = scale%0A
@@ -511,17 +511,16 @@
 epsilon 
- 
 = epsilo
@@ -566,17 +566,16 @@
 points  
- 
 = points
@@ -592,17 +592,16 @@
 offset  
- 
 = offset
@@ -618,17 +618,16 @@
 width   
- 
 = width%0A
@@ -643,17 +643,16 @@
 height  
- 
 = height
@@ -665,17 +665,16 @@
 his.
-maxD
+d
 epth 
+  
 = ma
@@ -1160,24 +1160,37 @@
 ation(point,
+ size, angle,
  depth = thi
@@ -1322,19 +1322,31 @@
 draw
-Shape
+Unit
 (depth
+, size, angle
 )%0A%0A 
@@ -1475,20 +1475,8 @@
 %7D%0A%0A 
- iterate() %7B
  // 
@@ -1520,16 +1520,49 @@
 Interval
+, or use generators%0A  iterate() %7B
 %0A    ret
@@ -1672,34 +1672,254 @@
 draw
-Shape() %7B%0A    return false
+Unit(depth = 0, size = 1, angle = 0) %7B%0A    chaos.context.save()%0A    chaos.context.rotate(angle)%0A    chaos.context.beginPath()%0A    chaos.context.moveTo(0, 0)%0A    chaos.context.lineTo(0, -size)%0A    chaos.context.stroke()%0A    chaos.context.restore()
 %0A  %7D

work towards general iteration