import { Mesh } from '..'
import Screen from '../../screen'
import { normalv3 } from '../../utility'

class Sphere extends Mesh {
  static new (radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength) {
    radius = radius || 1

    widthSegments = Math.max(3, Math.floor(widthSegments) || 8)
    heightSegments = Math.max(2, Math.floor(heightSegments) || 6)

    phiStart = phiStart !== undefined ? phiStart : 0
    phiLength = phiLength !== undefined ? phiLength : Math.PI * 2

    thetaStart = thetaStart !== undefined ? thetaStart : 0
    thetaLength = thetaLength !== undefined ? thetaLength : Math.PI

    const thetaEnd = Math.min(thetaStart + thetaLength, Math.PI)

    let ix
    let iy

    let index = 0
    const grid = []

    // buffers
    const indices = []
    const vertices = []
    const normals = []
    const uvs = []

    // generate vertices, normals and uvs
    for (iy = 0; iy <= heightSegments; iy++) {
      const verticesRow = []
      const v = iy / heightSegments

      // special case for the poles
      let uOffset = 0

      if (iy === 0 && thetaStart === 0) {
        uOffset = 0.5 / widthSegments
      } else if (iy === heightSegments && thetaEnd === Math.PI) {
        uOffset = -0.5 / widthSegments
      }

      for (ix = 0; ix <= widthSegments; ix++) {
        const u = ix / widthSegments

        // vertex
        const x = -radius * Math.cos(phiStart + u * phiLength) * Math.sin(thetaStart + v * thetaLength)
        const y = radius * Math.cos(thetaStart + v * thetaLength)
        const z = radius * Math.sin(phiStart + u * phiLength) * Math.sin(thetaStart + v * thetaLength)

        vertices.push(x, y, z)

        // normal
        const normal = normalv3([x, y, z])
        normals.push(normal[0], normal[1], normal[2])

        // uv
        uvs.push(u + uOffset, 1 - v)
        verticesRow.push(index++)
      }
      grid.push(verticesRow)
    }

    // indices

    for (iy = 0; iy < heightSegments; iy++) {
      for (ix = 0; ix < widthSegments; ix++) {
        const a = grid[iy][ix + 1]
        const b = grid[iy][ix]
        const c = grid[iy + 1][ix]
        const d = grid[iy + 1][ix + 1]

        if (iy !== 0 || thetaStart > 0) indices.push(a, b, d)
        if (iy !== heightSegments - 1 || thetaEnd < Math.PI) indices.push(b, c, d)
      }
    }

    return Mesh.construct(Screen.gl, vertices, indices, uvs, normals)
  }
}

export default Sphere
