import vertSingle from '../shaders/vertex/single.js'

import fragSingle from '../shaders/fragment/single.js'

import {
  DEG2RAD,
  SLOPEFACTOR
} from '../constants'

import * as util from '../util'

/**
 * The resulting Regl DrawCommand is used to draw a single tile. The fragment shader decodes the
 * Float32 value of a pixel and colorizes it with the given color scale (and/or sentinel values).
 */
export function createDrawTileCommand (regl, commonConfig, fragMacros) {
  return regl({
    ...commonConfig,
    vert: vertSingle,
    frag: util.defineMacros(fragSingle, fragMacros),
    uniforms: {
      ...commonConfig.uniforms,
      scaleLength: regl.prop('scaleLength'),
      sentinelLength: regl.prop('sentinelLength'),
      scaleColormap: regl.prop('scaleColormap'),
      sentinelColormap: regl.prop('sentinelColormap'),
      aboveColor: regl.prop('aboveColor'),
      belowColor: regl.prop('belowColor'),
      texture: (_, { texture }) => texture,
      enableSimpleHillshade: (_, { enableSimpleHillshade }) => enableSimpleHillshade,
      offset: 0,
      azimuth: 0,
      altitude: 0,
      slopescale: 0,
      deg2rad: DEG2RAD,
      slopeFactor: SLOPEFACTOR,
      tileSize: 0,
      textureSize: 0,
      textureBounds: [0, 0, 0, 0]
    },
    attributes: {
      ...commonConfig.attributes,
      texCoord: (_, { textureBounds }) => util.getTexCoordVerticesTriangleStripQuad(textureBounds)
    }
  })
}