package main

import "core:math"
import rl "vendor:raylib"

GfxState :: struct {
	shader:     rl.Shader,
	shader_loc: i32,
	car:        rl.Model,
}

gfx_init :: proc() -> GfxState {

	state: GfxState

	state.car = rl.LoadModel("./assets/3rdparty/car.glb")

	state.shader = rl.LoadShader(
		"src/shader/nfs_car.vs",
		"src/shader/nfs_car.fs",
	)

	light_dir_loc := rl.GetShaderLocation(state.shader, "lightDir")
	light_col_loc := rl.GetShaderLocation(state.shader, "lightColor")
	ambient_col_loc := rl.GetShaderLocation(state.shader, "ambientColor")
	state.shader_loc = rl.GetShaderLocation(state.shader, "viewPos")

	// Set Static Lighting Values (Night/Arcade Theme)
	light_dir := rl.Vector3{-0.5, -1.0, -0.5} // Angled overhead light
	light_color := rl.Vector3{1.2, 1.1, 0.9} // Slightly warm sunlight/streetlight
	ambient_col := rl.Vector3{0.15, 0.15, 0.25} // Cool dark blue ambient for shadows

	rl.SetShaderValue(state.shader, light_dir_loc, &light_dir, .VEC3)
	rl.SetShaderValue(state.shader, light_col_loc, &light_color, .VEC3)
	rl.SetShaderValue(state.shader, ambient_col_loc, &ambient_col, .VEC3)

	// Assign shader to model materials
	for i in 0 ..< state.car.materialCount {
		state.car.materials[i].shader = state.shader
	}

	return state
}

gfx_unload :: proc(state: ^GfxState) {
	rl.UnloadModel(state.car)
	rl.UnloadShader(state.shader)
}

gfx_draw_car :: proc(state: ^GfxState, pos: rl.Vector3, yaw: f32) {
	angle_degrees := yaw * (180 / math.PI)
	rotation_axis := rl.Vector3{0, 1, 0}
	rl.DrawModelEx(state.car, pos, rotation_axis, angle_degrees, 1.0, rl.WHITE)
}

gfx_update :: proc(state: ^GfxState, cam_pos: ^rl.Vector3) {
	rl.SetShaderValue(state.shader, state.shader_loc, cam_pos, .VEC3)
}
