#include "ui/platform.lua"
#include "helpers/table.lua"
#include "helpers/math.lua"
#include "helpers/input.lua"
#include "helpers/positioning.lua"
#include "helpers/touch.lua"

UI_EVENT_LANGUAGE_CHANGED = "LanguageChanged"

UI_DEVICE_NONE = 0
UI_DEVICE_MOUSE = 1
UI_DEVICE_GAMEPAD = 2
UI_DEVICE_TOUCHSCREEN = 3

UI_CURSOR_SHOW = 0
UI_CURSOR_HIDE = 1
UI_CURSOR_HIDE_AND_LOCK = 2

function rgb(r,g,b,a)
    return r / 256,g / 256,b / 256, (a or 256) / 256
end

ToListItems = function(items, calculator, drawer)
    local values = {}
        
    for idx = 1, #items do
        local value = items[idx]
        local iw,ih = calculator(value)
        table.insert(values, {data = value, w = iw, h = ih, draw = function(value) drawer(value, idx) end})
    end
    return values
end

--[[
The DebugQuad function can be used for visual debugging. 
It draws a rectangle of a given width and height, splitting 
it into quarters with lines. 
]]
function DebugQuad(rectWidth, rectHeight)
	local lineWidth = 2
	UiColorFilter(1,1,1,0.5)
	UiPush()
		UiColor(1,1,1,1)
		UiPush()
			UiRect(lineWidth,rectHeight)
			UiTranslate(rectWidth - lineWidth,0)
			UiRect(lineWidth,rectHeight)
		UiPop()
		UiPush()
			UiPush()
				UiTranslate(rectWidth / 2,0)
				UiRect(lineWidth,rectHeight)
			UiPop()
			UiTranslate(0,rectHeight / 2)
			UiRect(rectWidth,lineWidth)
		UiPop()
		UiColor(1,0,0,1)
		UiRect(rectWidth,rectHeight)
	UiPop()
end

function hexrgb(hex)
	local r, g, b, a
	-- Check for the presence of an alpha channel
	if hex > 16777215 then
	    a = hex % 256
	    r = math.floor(hex / 16777216) % 256
	    g = math.floor(hex / 65536) % 256
	    b = math.floor(hex / 256) % 256
	else
	    a = 255
	    r = math.floor(hex / 65536) % 256
	    g = math.floor(hex / 256) % 256
	    b = hex % 256
	end
    return r / 255, g / 255, b / 255, a / 255
end

function drawLocalUserTag(option)
	if not IsRunningOnPC() and not IsRunningOnApple() then
        UiPush()
            if option and option.offset and option.offset.x and option.offset.y then
                UiTranslateToPositionOnScreen(UiWidth() - option.offset.x, UiHeight() - option.offset.y)
            else
                UiTranslateToPositionOnScreen(UiWidth() - 70, UiHeight() - 50)
            end
            UiAlign("middle right")

            UiFont("arial.ttf", 27)
            local userTag = GetUserNickname()
            local wTag, _ = UiGetTextSize(userTag)
            local wIcon, hIcon = UiGetImageSize("ui/menu/user_icon.png")
            local iconScale = 0.4

            if option and option.withBackground then
                local padding = 15
                UiPush()
                    UiColor(0, 0, 0, 0.55)
                    UiTranslate(padding, 0)
                    UiImageBox("ui/common/box-solid-10.png", wTag + wIcon * iconScale / 2 + 2 * padding + 8, hIcon * iconScale / 2 + 2 * padding, 6, 6)
                UiPop()
            end

            UiTranslate(-wTag + 3, 0)
            UiPush()
                UiScale(iconScale)
                UiImage("ui/menu/user_icon.png")
            UiPop()
            UiTranslate(wTag + 4, 1)

            UiColor(1,1,1)
            UiText(userTag)
        UiPop()
    end
end

function randomFloat(lower, greater)
    return lower + math.random()  * (greater - lower);
end

function randomColor()
    local r = randomFloat(0,1)
    local g = randomFloat(0,1)
    local b = randomFloat(0,1)
    local a = 1
    return r,g,b,a
end

