package game

import (
	"time"

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

// Init initializes the game.
func (g *Game) Init(win *glfw.Window) {

	g.world = world.NewWorld()
	g.view = &render.View{}
	g.worldrender = &render.WorldRenderer{}

	var seed int64 = time.Now().Unix()
	for i := -2; i <= 3; i++ {
		for j := -2; j <= 3; j++ {
			c := &world.Chunk{}
			g.world.SetChunk(i, j, c)
			worldgen.Chunk(c, g.world, seed)
		}
	}

	width, height := win.GetSize()
	g.view.Aspect(float32(width)/float32(height)).FovY(60).LookAt(g.cameraPos.ToFloat32(), g.rotY, g.rotZ)

	err := g.worldrender.Init(g.world)
	if err != nil {
		panic(err)
	}

	g.fbSize[0], g.fbSize[1] = win.GetFramebufferSize()
	win.SetSizeCallback(func(w *glfw.Window, width, height int) {
		win.SetCursorPos(float64(width)/2, float64(height)/2)
		g.view.Aspect(float32(width) / float32(height))
		g.fbSize[0], g.fbSize[1] = w.GetFramebufferSize()
	})

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

	win.SetCursorPos(float64(width)/2, float64(height)/2)

	win.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
	win.SetCursorPosCallback(func(w *glfw.Window, xpos, ypos float64) {
		width, height := w.GetSize()
		centerX, centerY := float64(width)/2, float64(height)/2
		deltaX, deltaY := xpos-centerX, ypos-centerY
		g.rotY -= float32(deltaX / 10)
		g.rotZ -= float32(deltaY / 10)
		if g.rotZ > 89.9 {
			g.rotZ = 89.9
		}
		if g.rotZ < -89.9 {
			g.rotZ = -89.9
		}
		win.SetCursorPos(centerX, centerY)
	})
}

// Update updates the game state, not necessarily in the main thread.
func (g *Game) Update(win *glfw.Window, delta time.Duration) {
	if win.GetKey(glfw.KeyLeft) == glfw.Press {
		g.rotY += float32(delta.Seconds()) * 100
	}
	if win.GetKey(glfw.KeyRight) == glfw.Press {
		g.rotY -= float32(delta.Seconds()) * 100
	}
	if win.GetKey(glfw.KeyUp) == glfw.Press {
		g.rotZ += float32(delta.Seconds()) * 100
		if g.rotZ > 89.9 {
			g.rotZ = 89.9
		}
	}
	if win.GetKey(glfw.KeyDown) == glfw.Press {
		g.rotZ -= float32(delta.Seconds()) * 100
		if g.rotZ < -89.9 {
			g.rotZ = -89.9
		}
	}

	forward := itype.Vec3f(mgl32.Rotate3DY(mgl32.DegToRad(g.rotY)).Mul3(mgl32.Rotate3DZ(mgl32.DegToRad(g.rotZ))).Mul3x1(mgl32.Vec3{1, 0, 0})).ToFloat64().Multiply(delta.Seconds() * 8)
	right := forward.Cross(itype.Vec3d{0, 1, 0})
	right = right.Multiply(1 / right.Length()).Multiply(delta.Seconds() * 8)
	if win.GetKey(glfw.KeyW) == glfw.Press {
		g.cameraPos = g.cameraPos.Add(forward)
	}
	if win.GetKey(glfw.KeyS) == glfw.Press {
		g.cameraPos = g.cameraPos.Add(forward.Negative())
	}
	if win.GetKey(glfw.KeyD) == glfw.Press {
		g.cameraPos = g.cameraPos.Add(right)
	}
	if win.GetKey(glfw.KeyA) == glfw.Press {
		g.cameraPos = g.cameraPos.Add(right.Negative())
	}

	g.view.LookAt(g.cameraPos.ToFloat32(), g.rotY, g.rotZ)
}

// Render, called with a OpenGL context, renders the game.
func (g *Game) Render() {
	gl.Viewport(0, 0, int32(g.fbSize[0]), int32(g.fbSize[1]))
	g.worldrender.Render(g.world, g.view)
	render.Framewire.Render(g.view)
}
