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

tape('attribute constants', function (t) {
  var gl = createContext(2, 2)
  var regl = createREGL(gl)

  var vert = [
    'precision highp float;',
    'attribute vec4 color;',
    'attribute vec2 position;',
    'varying vec4 fragColor;',
    'void main () {',
    '  fragColor = color;',
    '  gl_Position = vec4(position, 0, 1);',
    '}'
  ].join('\n')

  var frag = [
    'precision highp float;',
    'varying vec4 fragColor;',
    'void main () {',
    '  gl_FragColor = fragColor;',
    '}'
  ].join('\n')

  var baseDesc = {
    vert: vert,
    frag: frag,
    attributes: {
      position: [
        -4, 0,
        4, 4,
        -4, 4
      ]
    },
    count: 3,
    depth: {enable: false},
    primitive: 'triangles'
  }

  var colors = {
    '1': [1],
    '2': [1, 1],
    '3': [1, 0, 1],
    '4': [1, 0, 0, 1]
  }

  var commands = {
    'static': function (color) {
      var desc = extend({}, baseDesc)
      desc.attributes = extend(desc.attributes, {
        color: {
          constant: color
        }
      })
      return regl(desc)
    },

    'static - dyn shader': function (color) {
      var desc = extend({
        frag: function (context, props) {
          return frag
        }
      }, baseDesc)
      desc.attributes = extend(desc.attributes, {
        color: {
          constant: color
        }
      })
      return regl(desc)
    },

    'prop': function (color) {
      var desc = extend({}, baseDesc)
      desc.attributes = extend(desc.attributes, {
        color: {
          constant: regl.prop('color')
        }
      })
      return regl(desc)
    },

    'prop - dyn shader': function (color) {
      var desc = extend({
        frag: function (context, props) {
          return frag
        }
      }, baseDesc)
      desc.attributes = extend(desc.attributes, {
        color: {
          constant: regl.prop('color')
        }
      })
      return regl(desc)
    },

    'context': function (color) {
      var desc = extend({
        context: {
          color: color
        }
      }, baseDesc)
      desc.attributes = extend(desc.attributes, {
        color: {
          constant: regl.context('color')
        }
      })
      return regl(desc)
    },

    'context - dyn shader': function (color) {
      var desc = extend({
        context: {
          color: color
        },
        frag: function () {
          return frag
        }
      }, baseDesc)
      desc.attributes = extend(desc.attributes, {
        color: {
          constant: regl.context('color')
        }
      })
      return regl(desc)
    }
  }

  var cases = {
    'draw': function (command, color) {
      command({ color: color })
    },
    'batch': function (command, color) {
      command([{color: color}])
    },
    'scope': function (command, color) {
      command({color: color}, function () {
        regl.draw()
      })
    },
    'scope - batch': function (command, color) {
      command({color: color}, function () {
        regl.draw(1)
      })
    }
  }

  function checkPixels (color) {
    var pixels = regl.read()
    for (var i = 0; i < 2 * 2; ++i) {
      for (var j = 0; j < color.length; ++j) {
        if (pixels[4 * i + j] !== color[j] * 255) {
          return false
        }
      }
    }
    return true
  }

  Object.keys(colors).forEach(function (count) {
    var color = colors[count]
    Object.keys(commands).forEach(function (commandName) {
      var genCommand = commands[commandName]
      var command = genCommand(color)
      Object.keys(cases).forEach(function (caseName) {
        var caseCode = cases[caseName]
        regl.clear({
          color: [0, 0, 0, 0]
        })
        caseCode(command, color)
        t.ok(checkPixels(color), caseName + ',' + commandName + ' ' + count + 'f')
      })
    })
  })

  regl.destroy()
  createContext.destroy(gl)
  t.end()
})

@@ -323,15 +323,18 @@
  vec
-4 color
+2 position
 ;',%0A
@@ -347,34 +347,31 @@
 ttribute vec
-2 position
+4 color
 ;',%0A    'var
@@ -768,13 +768,13 @@
     
--4, 0
+0, -4
 ,%0A  
@@ -779,16 +779,17 @@
         
+-
 4, 4,%0A  
@@ -790,25 +790,24 @@
  4,%0A        
--
 4, 4%0A      %5D
@@ -901,24 +901,41 @@
  colors = %7B%0A
+    'scalar': 1,%0A
     '1': %5B1%5D
@@ -969,24 +969,24 @@
  %5B1, 0, 1%5D,%0A
-
     '4': %5B1,
@@ -994,16 +994,186 @@
 0, 0, 1%5D
+,%0A    '1-typed': new Float32Array(%5B1%5D),%0A    '2-typed': new Float32Array(%5B1, 1%5D),%0A    '3-typed': new Float32Array(%5B1, 0, 1%5D),%0A    '4-typed': new Float32Array(%5B1, 0, 0, 1%5D)
 %0A  %7D%0A%0A  
@@ -3444,24 +3444,158 @@
 * 2; ++i) %7B%0A
+      if (typeof color === 'number') %7B%0A        if (pixels%5B4 * i%5D !== color * 255) %7B%0A          return false%0A        %7D%0A      %7D else %7B%0A  
       for (v
@@ -3625,24 +3625,26 @@
 gth; ++j) %7B%0A
+  
         if (
@@ -3685,32 +3685,34 @@
 55) %7B%0A          
+  
 return false%0A   
@@ -3700,32 +3700,44 @@
    return false%0A
+          %7D%0A
         %7D%0A      
@@ -4254,14 +4254,8 @@
 ount
- + 'f'
 )%0A  

fix up tests