package main

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

@(private="file") state_current: ^UiContext

UiElementType :: enum {
	Input,
	Button,
	Container,
	Text,
	DropDown,
}

UiElementData :: union {
	UiElementData_Button,
	UiElementData_Input,
	UiElementData_Container,
	UiElementData_Text,
	UiElementData_Dropdown,
}

UiElement :: struct {
	area: rect2,
	type: UiElementType,
	id: UiElementId,
	data: UiElementData,
}

UiElementId :: bit_field u32 {
	group_index: u16    | 16,
	element_index: u16  | 16,
}

UiContext :: struct {
	origin, position_left, position: v2,
	same_line: bool,
	style: UiStyle,
	element_focus_id: UiElementId,
	id_current: UiElementId,
	elements: [dynamic]UiElement,
	container: ^UiElement,
	group_element_count: int,
}

UiStyle :: struct {
	margin, padding:     f32,
	font_style:          FontStyle,
}

ui_init :: proc(state: ^UiContext, style: UiStyle, origin: v2, allocator: mem.Allocator) {
	state^ = {
		element_focus_id = ui_id_invalid(),
		origin = origin,
		position = origin,
		style = style,
		elements = make(type_of(state.elements), 0, 512, allocator)
	}
}

ui_start :: proc(state: ^UiContext) {
	state_current = state
	clear(&state.elements)
	state.id_current = {group_index = 0, element_index = 0}
	state.position_left = state.origin
	state.position = state.position_left
}

ui_end :: proc() -> []UiElement {
	elements := state_current.elements[:]
	state_current = nil
	return elements
}

UiElementCallback_Button :: proc(data: rawptr)

UiElementData_Button :: struct {
	label: cstring,
	callback: UiElementCallback_Button,
	callback_data: rawptr,
}

ui_button :: proc(size: v2, data: UiElementData_Button) {
	state := state_current
	area := _ui_advance_element(state, size)
	state.id_current.element_index += 1
	append(&state.elements, UiElement{id = state.id_current, area = area, type = .Button, data = data})
}

UiElementData_Container :: struct {
	label: cstring,
}

ui_container_begin :: proc(data: UiElementData_Container) {
	state := state_current
	assert(state.container == nil)
	state.id_current = {group_index = state.id_current.group_index + 1, element_index = 0}
	element := UiElement{id = state.id_current, area = _ui_advance_container(state), type = .Container, data = data}
	append(&state.elements, element)
	state.position_left += state.style.margin + state.style.padding
	state.container = &state.elements[len(state.elements) - 1]
	state.group_element_count = 0
}

ui_container_end :: proc() {
	state := state_current
	assert(state.container != nil)
	state.position_left.x = state.origin.x
	state.position_left.y = state.container.area.y + state.container.area.height
	state.position = state.position_left
	state.container = nil
	state.group_element_count = 0
}

UiElementData_Input :: struct {
	label: cstring,
	input: []byte,
	min, max: f32,
}

ui_input :: proc(size: v2, data: UiElementData_Input) {
	state := state_current
	area := _ui_advance_element(state, size)
	state.id_current.element_index += 1
	append(&state.elements, UiElement{id = state.id_current, area = area, type = .Input, data = data})
}

UiElementData_Text :: struct {
	text: cstring,
	color: rl.Color,
}

ui_text :: proc(data: UiElementData_Text) {
	state := state_current
	size := measure_text(data.text, state.style.font_style)
	area := _ui_advance_element(state, size)
	state.id_current.element_index += 1
	append(&state.elements, UiElement{id = state.id_current, area = area, type = .Text, data = data})
}

ui_mat2 :: proc(input_field_size: v2, inputs: [][]byte, min, max: f32) {
	ui_input(input_field_size, {label = "", input = inputs[0], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[1], min = min, max = max})

	ui_input(input_field_size, {label = "", input = inputs[2], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[3], min = min, max = max})
}

ui_mat3 :: proc(input_field_size: v2, inputs: [][]byte, min, max: f32) {
	ui_input(input_field_size, {label = "", input = inputs[0], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[1], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[2], min = min, max = max})

	ui_input(input_field_size, {label = "", input = inputs[3], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[4], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[5], min = min, max = max})

	ui_input(input_field_size, {label = "", input = inputs[6], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[7], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[8], min = min, max = max})
}

ui_mat4 :: proc(input_field_size: v2, inputs: [][]byte, min, max: f32) {
	ui_input(input_field_size, {label = "", input = inputs[0], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[1], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[2], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[3], min = min, max = max})

	ui_input(input_field_size, {label = "", input = inputs[4], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[5], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[6], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[7], min = min, max = max})

	ui_input(input_field_size, {label = "", input = inputs[8], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[9], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[10], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[11], min = min, max = max})

	ui_input(input_field_size, {label = "", input = inputs[12], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[13], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[14], min = min, max = max})
	ui_same_line()
	ui_input(input_field_size, {label = "", input = inputs[15], min = min, max = max})
}

UiElementCallback_Dropdown :: proc(previous, current: i32, data: rawptr)

UiElementData_Dropdown :: struct {
	label: cstring,
	active: ^i32,
	callback: UiElementCallback_Dropdown,
	callback_data: rawptr,
}

ui_dropdown :: proc(size: v2, data: UiElementData_Dropdown) {
	state := state_current
	area := _ui_advance_element(state, size)
	state.id_current.element_index += 1
	append(&state.elements, UiElement{id = state.id_current, area = area, type = .DropDown, data = data})
}

ui_same_line :: proc() {
	state_current.same_line = true
}

ui_element_is_editing :: proc(state: UiContext, id: UiElementId) -> bool {
	return state.element_focus_id == id
}

ui_element_focus :: proc(state: ^UiContext, id: UiElementId) {
	state.element_focus_id = id
}

ui_element_focus_reset :: proc(state: ^UiContext) {
	state.element_focus_id = ui_id_invalid()
}

ui_id_invalid :: proc() -> UiElementId {
	return {group_index = max(u16), element_index = max(u16)}
}

_ui_advance_element :: proc(state: ^UiContext, size: v2) -> rect2 {
	if state.group_element_count > 0 {
		if state.same_line {
			previous_element := state.elements[len(state.elements) - 1]
			state.position.x += previous_element.area.width + state.style.padding
			state.same_line = false
		} else {
			state.position_left.y += size.y + state.style.padding
			state.position = state.position_left
		}
	} else {
		state.position += state.style.padding
	}
	area := rect2{**state.position, **size}
	if state.container != nil {
		element_bottom_left := rect_get_br(area)
		container_bottom_left := rect_get_br(state.container.area)
		new_br := linalg.max(element_bottom_left + state.style.padding, container_bottom_left)
		state.container.area.width = new_br.x - state.container.area.x
		state.container.area.height = new_br.y - state.container.area.y
	}
	state.group_element_count += 1
	return area
}

_ui_advance_container :: proc(state: ^UiContext) -> rect2 {
	state.position += state.style.margin
	return {**state.position, 0.0, 0.0}
}
