package main

import (
	"fmt"
	"runtime"
	"time"
	"unsafe"

	"github.com/Edgaru089/gl01/internal/asset"
	"github.com/Edgaru089/gl01/internal/render"
	"github.com/Edgaru089/gl01/internal/world"
	"github.com/go-gl/gl/all-core/gl"
	"github.com/go-gl/glfw/v3.3/glfw"
	"github.com/go-gl/mathgl/mgl32"
)

const (
	windowWidth  = 800
	windowHeight = 600
)

func init() {
	runtime.LockOSThread()
}
func main() {

	asset.InitWorldTextureAtlas()

	if err := glfw.Init(); err != nil {
		panic(err)
	}
	defer glfw.Terminate()

	glfw.WindowHint(glfw.Resizable, 0)
	glfw.WindowHint(glfw.ContextVersionMajor, 4)
	glfw.WindowHint(glfw.ContextVersionMinor, 1)
	glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
	glfw.WindowHint(glfw.OpenGLForwardCompatible, 1)
	win, err := glfw.CreateWindow(windowWidth, windowHeight, "Cube", nil, nil)
	if err != nil {
		panic(err)
	}
	win.MakeContextCurrent()
	glfw.SwapInterval(1)

	err = gl.Init()
	if err != nil {
		panic(err)
	}

	fmt.Println("OpenGL Version: ", gl.GoStr(gl.GetString(gl.VERSION)))

	shader, err := render.NewShader(asset.WorldShaderVert, asset.WorldShaderFrag)
	if err != nil {
		panic(err)
	}

	model := mgl32.Ident4()
	shader.SetUniformMat4("model", model)

	view := mgl32.LookAtV(mgl32.Vec3{3, 3, 3}, mgl32.Vec3{0, 0, 0}, mgl32.Vec3{0, 1, 0})
	shader.SetUniformMat4("view", view)

	projection := mgl32.Perspective(mgl32.DegToRad(45), float32(windowWidth)/float32(windowHeight), 0.1, 10)
	shader.SetUniformMat4("projection", projection)

	// Texture
	asset.InitWorldTextureAtlas()
	worldTexture := render.NewTextureRGBA(asset.WorldTextureAtlas.Image)
	shader.SetUniformTexture("tex", worldTexture)

	gl.BindFragDataLocation(shader.Handle(), 0, gl.Str("outputColor\x00"))

	var c, cm world.Chunk
	c.SetBlock(0, 0, 0, 2)
	c.SetBlock(0, 0, 2, 1)
	cm.SetChunkID(-1, -1)
	cm.SetBlock(15, 0, 15, 1)
	cm.SetBlock(15, 1, 15, 2)

	var arr []world.Vertex
	arr = c.AppendVertex(arr)
	arr = cm.AppendVertex(arr)

	// Vertex Array
	var vao uint32
	gl.GenVertexArrays(1, &vao)
	gl.BindVertexArray(vao)

	// Vertex Buffer
	var vbo uint32
	gl.GenBuffers(1, &vbo)
	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
	gl.BufferData(gl.ARRAY_BUFFER, int(unsafe.Sizeof(world.Vertex{}))*len(arr), gl.Ptr(arr), gl.STATIC_DRAW)

	vertAttrib := uint32(gl.GetAttribLocation(shader.Handle(), gl.Str("vert\x00")))
	gl.EnableVertexAttribArray(vertAttrib)
	gl.VertexAttribPointer(vertAttrib, 3, gl.FLOAT, false, int32(unsafe.Sizeof(world.Vertex{})), gl.PtrOffset(int(unsafe.Offsetof(world.Vertex{}.World))))

	texCoordAttrib := uint32(gl.GetAttribLocation(shader.Handle(), gl.Str("vertTexCoord\x00")))
	gl.EnableVertexAttribArray(texCoordAttrib)
	gl.VertexAttribPointer(texCoordAttrib, 2, gl.FLOAT, false, int32(unsafe.Sizeof(world.Vertex{})), gl.PtrOffset(int(unsafe.Offsetof(world.Vertex{}.Texture))))

	gl.Enable(gl.CULL_FACE)
	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LESS)
	gl.ClearColor(1, 1, 1, 1)

	angle := 0.0
	prevTime := time.Now()

	for !win.ShouldClose() {

		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

		// Angle update
		angle += time.Since(prevTime).Seconds()
		prevTime = time.Now()

		model = mgl32.HomogRotate3D(float32(angle), mgl32.Vec3{0, 1, 0})

		// Render
		// Shader variable
		shader.UseProgram()
		shader.BindTextures()
		shader.SetUniformMat4("model", model)

		gl.BindVertexArray(vao)

		//gl.ActiveTexture(gl.TEXTURE1)
		//gl.BindTexture(gl.TEXTURE_2D, texture)

		gl.DrawArrays(gl.TRIANGLES, 0, int32(len(arr)))

		win.SwapBuffers()
		glfw.PollEvents()
	}
}
