package render

import (
	"unsafe"

	"edgaru089.ml/go/gl01/internal/asset"
	"edgaru089.ml/go/gl01/internal/io"
	"edgaru089.ml/go/gl01/internal/util/itype"
	"edgaru089.ml/go/gl01/internal/world"
	"github.com/go-gl/gl/all-core/gl"
	"github.com/go-gl/mathgl/mgl32"
	"github.com/inkyblackness/imgui-go/v4"
)

var (
	ShadowmapSize = itype.Vec2i{3072, 3072} // Size of the shadow mapping
)

// WorldRenderer holds texture/shader resource and viewport
// information for world rendering.
type WorldRenderer struct {
	shader  *Shader
	texture *Texture

	depthmapFBO, depthmap uint32
	depthmapShader        *Shader
}

// The default WorldRenderer.
var DefaultWorldRenderer WorldRenderer

// Init initializes the WorldRenderer.
func (r *WorldRenderer) Init(w *world.World) (err error) {

	r.shader, err = NewShader(asset.WorldShaderVert, asset.WorldShaderFrag)
	if err != nil {
		return err
	}

	r.depthmapShader, err = NewShader(asset.WorldShaderShadowmapVert, asset.WorldShaderShadowmapFrag)
	if err != nil {
		return err
	}

	asset.InitWorldTextureAtlas()
	r.texture = NewTextureRGBA(asset.WorldTextureAtlas.Image)
	r.shader.SetUniformTexture("tex", r.texture)

	r.shader.SetUniformMat4("model", mgl32.Ident4())
	r.depthmapShader.SetUniformMat4("model", mgl32.Ident4())
	// and view and projection uniforms not yet set
	gl.BindFragDataLocation(r.shader.Handle(), 0, gl.Str("outputColor\x00"))

	// Set the pointers for attributions
	vertAttrib := r.shader.GetAttribLocation("vert")
	gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, int32(unsafe.Sizeof(world.Vertex{})), gl.PtrOffset(int(unsafe.Offsetof(world.Vertex{}.World))))
	normalAttrib := r.shader.GetAttribLocation("normal")
	gl.VertexAttribPointer(normalAttrib, 3, gl.FLOAT, false, int32(unsafe.Sizeof(world.Vertex{})), gl.PtrOffset(int(unsafe.Offsetof(world.Vertex{}.Normal))))
	texCoordAttrib := r.shader.GetAttribLocation("vertTexCoord")
	gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, int32(unsafe.Sizeof(world.Vertex{})), gl.PtrOffset(int(unsafe.Offsetof(world.Vertex{}.Texture))))
	lightAttrib := r.shader.GetAttribLocation("light")
	gl.VertexAttribPointer(lightAttrib, 1, gl.FLOAT, false, int32(unsafe.Sizeof(world.Vertex{})), gl.PtrOffset(int(unsafe.Offsetof(world.Vertex{}.Light))))

	// generate the depthmap and depthmap FBO
	gl.GenFramebuffers(1, &r.depthmapFBO)
	gl.GenTextures(1, &r.depthmap)
	gl.BindTexture(gl.TEXTURE_2D, r.depthmap)
	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.DEPTH_COMPONENT, int32(ShadowmapSize[0]), int32(ShadowmapSize[1]), 0, gl.DEPTH_COMPONENT, gl.FLOAT, nil)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_BORDER)
	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_BORDER)
	borderColor := []float32{1, 1, 1, 1}
	gl.TexParameterfv(gl.TEXTURE_2D, gl.TEXTURE_BORDER_COLOR, &borderColor[0])
	// attach depth texture as FBO's depth buffer
	gl.BindFramebuffer(gl.FRAMEBUFFER, r.depthmapFBO)
	gl.FramebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, r.depthmap, 0)
	gl.DrawBuffer(gl.NONE)
	gl.ReadBuffer(gl.NONE)
	gl.BindFramebuffer(gl.FRAMEBUFFER, 0)

	r.shader.SetUniformTextureHandle("shadowmap", r.depthmap)

	gl.Enable(gl.CULL_FACE)
	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)

	return nil
}

var sun = [3]float32{0.2, 0.4, 0.3}

func (r *WorldRenderer) Render(world *world.World, view *View) {
	imgui.SliderFloat3("Sun", &sun, -1, 1)
	normalSun := itype.Vec3f(sun).Normalize()

	// 1. Render to depth map
	gl.Viewport(0, 0, int32(ShadowmapSize[0]), int32(ShadowmapSize[1]))
	gl.BindFramebuffer(gl.FRAMEBUFFER, r.depthmapFBO)
	gl.Clear(gl.DEPTH_BUFFER_BIT)

	lightPos := view.EyePos.Add(normalSun.Multiply(20))
	lightView := mgl32.LookAt(lightPos[0], lightPos[1], lightPos[2], view.EyePos[0], view.EyePos[1], view.EyePos[2], 0, 1, 0)
	lightProjection := mgl32.Ortho(-50, 50, -50, 50, 1, 50)
	lightspace := lightProjection.Mul4(lightView)

	r.depthmapShader.UseProgram()
	r.depthmapShader.SetUniformMat4("lightspace", lightspace)

	world.Render()

	gl.BindFramebuffer(gl.FRAMEBUFFER, 0)

	// 2. Render the scene
	gl.Viewport(0, 0, int32(io.DisplaySize[0]), int32(io.DisplaySize[1]))
	gl.Clear(gl.DEPTH_BUFFER_BIT)
	r.shader.UseProgram()
	r.shader.BindTextures()

	r.shader.SetUniformMat4("lightspace", lightspace)
	r.shader.SetUniformMat4("view", view.View())
	r.shader.SetUniformMat4("projection", view.Perspective())
	r.shader.SetUniformVec3f("viewPos", view.EyePos)
	r.shader.SetUniformVec4f("fogColor", itype.Vec4f{0.6, 0.8, 1.0, 1.0})
	r.shader.SetUniformVec3f("sun", normalSun)

	gl.Enable(gl.FRAMEBUFFER_SRGB)
	world.Render()
	gl.Disable(gl.FRAMEBUFFER_SRGB)
}
