local love = love
local gpu = require( "gpu" )
local t = {}

local canvas

local meshes = {}
local isDebugging = false
local tf = love.math.newTransform()
local debugTF = love.math.newTransform()
local sky
local bell
local contempra
local terrain
local light = require( "level.light" )

function t.start()
  canvas = {
    love.graphics.newCanvas(),
    stencil = false,
    depth = true,
  }
  
  sky = require( "models.sky" )
  bell = require( "models.bell" )
  contempra = require( "models.contempra" )
  terrain = require( "models.terrain" )
end

function t.debug()
  isDebugging = true
end

local function matte( )
  local list = drawLists.matte
  local shader = shaders.matte
  love.graphics.setShader( shader )
  for mesh in pairs( list ) do
    shader:send( "mdl", "column", meshes[mesh])
    love.graphics.draw( mesh )
  end
end

function t.draw( view, proj )

  if isDebugging then
  end


  love.graphics.push( "all" )
  love.graphics.setScissor( 0, 0, love.graphics.getDimensions() )
  love.graphics.setCanvas( canvas )
  love.graphics.clear()
  love.graphics.setDepthMode( "less", true )
  love.graphics.replaceTransform( tf )
  love.graphics.setMeshCullMode( "back" )
  
  sky.draw( view, proj, light )
  terrain.draw( view, proj )
  bell.draw( view, proj, light )
  contempra.draw( view, proj, light )


  love.graphics.pop()
  love.graphics.setColor( 1, 1, 1, 1 )
  love.graphics.draw( canvas[1] )

  if isDebugging then
    isDebugging = false
    io.flush()
  end
end

function t.update( player )
  
end

function t.transposeBells( i, j )
  return bell.transpose( i, j )
end

function t.targetLight( i )
  return light.setTarget( i )
end

function t.pickupPhone()
  return contempra.pickup()
end

return t