var createContext = require('./util/create-context')
var createREGL = require('../../regl')
var tape = require('tape')

function find (array, pred) {
  for (var i = 0; i < array.length; ++i) {
    if (pred(array[i])) {
      return array[i]
    }
  }
}

tape('batch', function (t) {
  setTimeout(function () {
    var regl = createREGL(createContext(16, 16))

    regl.clear({
      color: [1, 0, 0, 1]
    })

    var drawBatch = regl({
      frag: [
        'void main() {',
        '  gl_FragColor = vec4(0, 0, 1, 1);',
        '}'
      ].join('\n'),

      vert: [
        'attribute vec2 position;',
        'uniform vec2 offset;',
        'void main() {',
        '  gl_Position = vec4(position + offset, 0, 1);',
        '}'
      ].join('\n'),

      attributes: {
        position: regl.buffer([
          [0, 0]
        ])
      },

      uniforms: {
        offset: regl.prop('offset')
      },

      count: 1,

      primitive: 'points'
    })

    var drawBatchCount = regl({
      frag: [
        'void main() {',
        '  gl_FragColor = vec4(0, 0, 1, 1);',
        '}'
      ].join('\n'),

      vert: [
        'attribute vec2 position;',
        'uniform vec2 offset;',
        'void main() {',
        '  gl_Position = vec4(position + offset, 0, 1);',
        '}'
      ].join('\n'),

      attributes: {
        position: regl.buffer([
          [0, 0]
        ])
      },

      uniforms: {
        offset: function (args, index) {
          var p = points[index]
          return [(p[0] + 1) / 8 - 1.0, (p[1] + 1) / 8 - 1.0]
        }
      },

      count: 1,

      primitive: 'points'
    })

    var points = [
      [8, 8],
      [0, 8],
      [8, 0],
      [15, 8],
      [8, 15]
    ]

    function runTest () {
      var pixels = regl.read()

      for (var i = 0; i < 16; ++i) {
        for (var j = 0; j < 16; ++j) {
          var ptr = 4 * (16 * i + j)
          var hit = !!find(points, function (p) {
            return p[0] === i && p[1] === j
          })
          t.equals(pixels[ptr], hit ? 0 : 255, hit && 'hit')
          t.equals(pixels[ptr + 1], 0)
          t.equals(pixels[ptr + 2], hit ? 255 : 0, hit && '')
          t.equals(pixels[ptr + 3], 255)
        }
      }
    }

    drawBatch(points.map(function (p) {
      return {
        offset: [(p[0] + 1) / 8 - 1.0, (p[1] + 1) / 8 - 1.0]
      }
    }))
    runTest()

    drawBatchCount(points.length)
    runTest()

    regl.destroy()

    t.end()
  }, 120)
})

@@ -347,16 +347,109 @@
 ext(
-16, 16))
+5, 5))%0A%0A    var points = %5B%0A      %5B2, 2%5D,%0A      %5B0, 2%5D,%0A      %5B2, 0%5D,%0A      %5B4, 2%5D,%0A      %5B2, 4%5D%0A    %5D
 %0A%0A  
@@ -1003,32 +1003,63 @@
      count: 1,%0A%0A
+      depth: %7Benable: false%7D,%0A%0A
       primitive:
@@ -1623,38 +1623,43 @@
 return %5B(p%5B0%5D + 
-1
+0.5
 ) / 
-8
+2.25
  - 1.0, (p%5B1%5D + 
@@ -1650,38 +1650,43 @@
  - 1.0, (p%5B1%5D + 
-1
+0.5
 ) / 
-8
+2.25
  - 1.0%5D%0A        
@@ -1710,24 +1710,55 @@
  count: 1,%0A%0A
+      depth: %7Benable: false%7D,%0A%0A
       primit
@@ -1783,105 +1783,8 @@
 %7D)%0A%0A
-    var points = %5B%0A      %5B8, 8%5D,%0A      %5B0, 8%5D,%0A      %5B8, 0%5D,%0A      %5B15, 8%5D,%0A      %5B8, 15%5D%0A    %5D%0A%0A
     
@@ -1863,18 +1863,17 @@
  0; i %3C 
-16
+5
 ; ++i) %7B
@@ -1901,18 +1901,17 @@
  0; j %3C 
-16
+5
 ; ++j) %7B
@@ -1940,10 +1940,9 @@
  * (
-16
+5
  * i
@@ -2358,30 +2358,35 @@
 t: %5B(p%5B0%5D + 
-1
+0.5
 ) / 
-8
+2.25
  - 1.0, (p%5B1
@@ -2393,14 +2393,19 @@
 %5D + 
-1
+0.5
 ) / 
-8
+2.25
  - 1

modify pixel snapping and lower resolution in batch test