package main

import rl "libraries/raylib"
import "base:intrinsics"
import "core:strings"
import "core:fmt"
import "core:math/linalg"
import "core:mem"

// Wraps os.read_entire_file and os.write_entire_file, but they also work with emscripten.
@(require_results)
read_entire_file :: proc(name: string, allocator := context.allocator, loc := #caller_location) -> (data: []byte, success: bool) {
	return _read_entire_file(name, allocator, loc)
}

write_entire_file :: proc(name: string, data: []byte, truncate := true) -> (success: bool) {
	return _write_entire_file(name, data, truncate)
}

v1 :: [1]f32
v2 :: [2]f32
v3 :: [3]f32
v4 :: [4]f32
iv2 :: [2]i32
iv3 :: [3]i32
iv4 :: [4]i32
mat2 :: matrix[2, 2]f32
mat3 :: matrix[3, 3]f32
mat4 :: matrix[4, 4]f32
rect2 :: rl.Rectangle
irect2 :: struct {x, y, width, height: i32}
tri2 :: [3]v2
Bezier :: [3]v2

/// ### MISCELANIOUS ### ///
enum_to_raygui_options_string :: proc($T: typeid, allocator: mem.Allocator) -> cstring where intrinsics.type_is_enum(T) {
	b := strings.builder_make(allocator)
	for element, i in T {
		option := fmt.aprint(element, allocator = allocator)
		strings.write_string(&b, strings.to_ada_case(option, allocator))
		strings.write_rune(&b, ';')
	}
	strings.builder_replace_all(&b, "_", " ")
	b.buf[len(b.buf) - 1] = 0
	return cstring(raw_data(b.buf[:len(b.buf)]))
}


/// ### RECTANGLES ### ///
rect_from :: proc {
	rect2_from_v2,
	irect2_from_iv2,
}

@(private="file") rect2_from_v2 :: proc "contextless" (position, size: v2) -> rect2 {
	return {position.x, position.y, size.x, size.y}
}

@(private="file") irect2_from_iv2 :: proc "contextless" (position, size: iv2) -> irect2 {
	return {position.x, position.y, size.x, size.y}
}

rect_scale :: proc{
	rect2_scale,
	irect2_scale,
}

@(private="file") rect2_scale :: proc "contextless" (rect: rect2, scalar: f32) -> rect2 {
	return {rect.x * scalar, rect.y * scalar, rect.width * scalar, rect.height * scalar}
}

@(private="file") irect2_scale :: proc "contextless" (rect: irect2, scalar: i32) -> irect2 {
	return {rect.x * scalar, rect.y * scalar, rect.width * scalar, rect.height * scalar}
}

rect_grow :: proc {
	rect2_grow,
	rect2_grow_v2,
	irect2_grow,
	rect2_grow_iv2,
}

@(private="file") rect2_grow :: proc "contextless" (rect: rect2, grow: f32) -> rect2 {
	return {rect.x - grow, rect.y - grow, rect.width + grow * 2.0, rect.height + grow * 2.0}
}

@(private="file") rect2_grow_v2 :: proc "contextless" (rect: rect2, grow: v2) -> rect2 {
	return {rect.x - grow.x, rect.y - grow.y, rect.width + grow.x * 2.0, rect.height + grow.y * 2.0}
}

@(private="file") irect2_grow :: proc "contextless" (rect: irect2, grow: i32) -> irect2 {
	return {rect.x - grow, rect.y - grow, rect.width + grow * 2, rect.height + grow * 2}
}

@(private="file") rect2_grow_iv2 :: proc "contextless" (rect: irect2, grow: iv2) -> irect2 {
	return {rect.x - grow.x, rect.y - grow.y, rect.width + grow.x * 2.0, rect.height + grow.y * 2.0}
}


rect_from_area :: proc {
	rect_from_area_v2,
	rect_from_area_iv2,
}

@(private="file") rect_from_area_v2 :: proc "contextless" (start, end: v2) -> rect2 {
	tl := linalg.min(start, end)
	return {**tl, **(linalg.max(start, end) - tl)}
}

@(private="file") rect_from_area_iv2 :: proc "contextless" (start, end: iv2) -> irect2 {
	tl := linalg.min(start, end)
	return {**tl, **(linalg.max(start, end) - tl)}
}

rect_from_corners :: proc {
	rect2_from_corners,
	irect2_from_corners,
}

@(private="file") rect2_from_corners :: proc "contextless" (tl, tr, br, bl: v2) -> rect2 {
	top_left := linalg.min(linalg.min(tl, tr, br), bl)
	bottom_right := linalg.max(linalg.max(tl, tr, br), bl)
	return {**top_left, **(bottom_right - top_left)}
}

@(private="file") irect2_from_corners :: proc "contextless" (tl, tr, br, bl: iv2) -> irect2 {
	top_left := linalg.min(linalg.min(tl, tr, br), bl)
	bottom_right := linalg.max(linalg.max(tl, tr, br), bl)
	return {**top_left, **(bottom_right - top_left)}
}

rect_get_tl :: proc {
	rect2_get_tl,
	irect2_get_tl,
}

@(private="file") rect2_get_tl :: proc(rect: rect2) -> v2 {
	return {rect.x, rect.y}
}

@(private="file") irect2_get_tl :: proc(rect: irect2) -> iv2 {
	return {rect.x, rect.y}
}

rect_get_br :: proc {
	rect2_get_br,
	irect2_get_br,
}

@(private="file") rect2_get_br :: proc(rect: rect2) -> v2 {
	return {rect.x + rect.width, rect.y + rect.height}
}

@(private="file") irect2_get_br :: proc(rect: irect2) -> iv2 {
	return {rect.x + rect.width, rect.y + rect.height}
}

rect_get_size :: proc {
	rect2_get_size,
	irect2_get_size,
}

@(private="file") rect2_get_size :: proc(rect: rect2) -> v2 {
	return {rect.width, rect.height}
}

@(private="file") irect2_get_size :: proc(rect: irect2) -> iv2 {
	return {rect.width, rect.height}
}

/// ## LINEAR ALGEBRA ## ///
matrix3_translate2 :: proc "contextless" (translation: v2) -> mat3 {
	return {
		1.0, 0.0, translation.x,
		0.0, 1.0, translation.y,
		0.0, 0.0, 1.0,
	}
}

matrix3_transform_v2 :: proc(m: mat3, v: v2) -> v2 {
	return (m * v3{**v, 1.0}).xy
}

matrix3_transform_rect2 :: proc(m: mat3, r: rect2) -> rect2 {
	tl := m * v3{r.x, r.y, 1.0}
	br := m * v3{r.x + r.width, r.y + r.height, 1.0}
	return {**tl.xy, **(br - tl).xy}
}
