-- ui.lua
-- @date 2021-09-06
-- @author Eldin Zenderink
-- @brief All the ui helper functions reside here
#include "util.lua"

_UI_Toggle_Buttons = {}
function Generic_TableContains(t1,contains)
    for i=1,#t1 do
        if t1[i] == contains then
			return true
		end
    end
    return false
end
-- @note original from smokegun mod from teardown!
function Ui_StringProperty(x, y, current, name, notes, list)
    local value = nil
    if current == "" then
        current = list[1]
    end
    UiPush()
		UiTranslate(x, y)
        UiFont("bold.ttf", 11)
        UiText(notes)
        UiTranslate(0, 22)
        UiFont("regular.ttf", 22)
        UiText(name)
        UiTranslate(500, -18)
        UiFont("bold.ttf", 22)
        if UiTextedButton(current, "center middle", 240, 33, {0.5,0.5,0.5,1}, {1,1,1,1}) then
            local new = nil
            for i=1, #list-1 do
                if list[i] == current then
                    new = list[i+1]
                end
            end
            if new then
				if new ~= current then
                	value = new
				end
            else
				if list[1] ~= current then
                	value = list[1]
				end
            end
        end
    UiPop()
    return value
end

function Ui_KeySelector(x, y, font_size, name, notes, id, current_key)
    local value = current_key
	local awaiting_key_press = GetBool("savegame.mod.planespawner.ui.keyselector." .. id .. ".awaiting_key_press")
	local last_pressed = InputLastPressedKey():lower()


	local ignore_inputs = {
		" ",
		",",
		"tab",
		"lmb",
		"rmb",
		"mmb",
		"uparrow",
		"downarrow",
		"leftarrow",
		"rightarrow",
		"f1",
		"backspace",
		"alt",
		"delete",
		"home",
		"end",
		"pgup",
		"pgdown",
		"insert",
		"shift",
		"ctrl"
	}

	if Generic_TableContains(ignore_inputs, last_pressed) then
		last_pressed = current_key
	end


	local  color = {0.5,0.5,0.5,1}
	local text_color = {1,1,1,1}

	if awaiting_key_press and last_pressed ~= current_key then
		SetBool("savegame.mod.planespawner.ui.keyselector." .. id .. ".awaiting_key_press", false)
		value = last_pressed
	end
    UiPush()
		UiTranslate(x, y)
        UiFont("bold.ttf", font_size / 2)
		UiColor(1,1,1)
        UiText(notes)
        UiTranslate(0, font_size)
        UiFont("regular.ttf", font_size)
        UiText(name)
        UiTranslate(100, -(font_size - (font_size / 10 * 2)))
        UiFont("bold.ttf", font_size)
		if  awaiting_key_press then
			text_color = {0,0,0,1}
			color = {0.2,1,0.2,1}
		else
			text_color = {1,1,1,1}
			color = {0.5,0.5,0.5,1}
		end
        if UiTextedButton(value, "center middle", 100, (font_size / 2 * 3), color, text_color) then
			if not awaiting_key_press then
				SetBool("savegame.mod.planespawner.ui.keyselector." .. id .. ".awaiting_key_press", true)
			end
			value = last_pressed
        end
    UiPop()
    return value:lower()
end

function Ui_FloatProperty(x, y, width, current, name, notes, min_max_steps)
	local steps = min_max_steps[3]
	if current == nil then
		current = 0
	end
	local value = (current - min_max_steps[1]) / (min_max_steps[2] - min_max_steps[1]);
	UiPush()
		UiTranslate(x, y)
		UiFont("bold.ttf", 11)
		UiText(notes)
		UiTranslate(0, 22)
		UiFont("regular.ttf", 22)
		UiText(name)
		UiTranslate(500, -18)
		local value_rounded = math.floor((value*(min_max_steps[2] - min_max_steps[1])+min_max_steps[1])/steps+0.5)*steps
		local sliderval = UiColoredSlider(value_rounded, value * width, 0, width, width, 22, {0.5,0.5,0.5,1}, {0,1,0,0.8})
		value = sliderval / width
		value = math.floor((value*(min_max_steps[2] - min_max_steps[1])+min_max_steps[1])/steps+0.5)*steps;
	UiPop()
	if current ~= value then
		return value
	end
	return value
end

function UI_ToggleButton(x, y, text, group, id)
	-- Create a button
	if _UI_Toggle_Buttons[group] == nil then
		_UI_Toggle_Buttons[group] = {}
		_UI_Toggle_Buttons[group][id] = false
	end

	local clicked = _UI_Toggle_Buttons[group][id]
	UiPush()
	UiTranslate(x, y)
	local color = {0.5,0.5,0.5,1}
	if clicked then
		UiFont("bold.ttf", 22)
		color = {0.2,0.8,0.2,1}
	else
		UiFont("regular.ttf", 22)
		color = {0.5,0.5,0.5,1}
	end


	if clicked then
		color = {0.2,0.8,0.2,1}
	end

	if UiTextedButton(text, "center middle", 240, 33, color, {1,1,1,1}) then
		clicked = not clicked
	end
	UiPop()
	for k, v in pairs(_UI_Toggle_Buttons[group]) do
		if k ~= id and clicked then
			_UI_Toggle_Buttons[group][k] = false
		end
	end
	_UI_Toggle_Buttons[group][id] = clicked
	return clicked
end

