
-- Constants
BUTTON_SIZE = 90
BUTTON_PADDING = 8
NUMBER_Y = 120
BUTTON_Y = 300
NUMBER_FONT_SIZE = 100
BUTTON_FONT_SIZE = 45
ARROW_X = 120
ARROW_WIDTH = 90
MAX_DIGITS = 2

numberBuffer = ""   -- Keep track of what digits the player is pressing
keyPressTimeout = 0 -- For if a single digit is pressed

function tick()	
	screen = UiGetScreen()
    elevatorId = GetTagValue(screen, "id")
end

function draw()
	if screen == 0 then
		return
	end
	
	if GetPlayerScreen() ~= screen then
		resetBuffer()
	end

	UiButtonHoverColor(0.8, 0.8, 0.8)
	UiButtonPressColor(1.6, 1.5, 1.2)
	
	UiTranslate(0, 0)
	UiImage("MOD/elevator/panel-background.png")
	
	UiAlign("center middle")
	
	-- Buttons
	UiPush()
	UiTranslate(UiCenter() - BUTTON_SIZE - BUTTON_PADDING, BUTTON_Y)
	UiFont("regular.ttf", BUTTON_FONT_SIZE)
	UiColor(1.0, 1.0, 0.95)
	UiButtonImageBox("MOD/elevator/button.png", 25, 25)
	local sizeAndPadding = BUTTON_SIZE + BUTTON_PADDING
	for y=0,2 do
		for x=0,2 do
			local i = (y * 3) + x + 1
			UiPush()
			UiTranslate(x * sizeAndPadding, y * sizeAndPadding)
			if button(i) then
				keyPressTimeout = GetTime() + 2
				addToBuffer(i)
			end
			UiPop()
		end
	end
	UiTranslate(0, 3 * sizeAndPadding)
	if button("G") then
		numberBuffer = "G"
	end
	UiTranslate(sizeAndPadding, 0)
	if button(0) then
		keyPressTimeout = GetTime() + 2
		addToBuffer(0)
	end
	UiTranslate(sizeAndPadding, 0)
	if button("-") then
		-- Do nothing
	end
	UiPop()
	
	-- Floor number
	local currentFloor = getCurrentFloor()
	local destFloor = getDestinationFloor()
	UiFont("regular.ttf", NUMBER_FONT_SIZE)
	UiColor(1.0, 1.0, 0.6)
	UiTranslate(ARROW_X , NUMBER_Y)
	if currentFloor < getDestinationFloor() then
		UiImage("MOD/elevator/arrow.png")
	elseif currentFloor > getDestinationFloor() then
		UiPush()
		UiRotate(180)
		UiImage("MOD/elevator/arrow.png")
		UiPop()
	end
	UiTranslate(ARROW_WIDTH, 0)
	if GetPlayerScreen() == screen then
		-- Display the number that the player is entering
		if numberBuffer == "" then
			UiText("__")
		else
			UiText(numberBuffer)
		end
	else
		-- Display the current floor
		local valueToShow = currentFloor
		if valueToShow == 0 then
			valueToShow = "G"
		end
		UiText(valueToShow)
	end
	
	-- Check if ok to set destination
	if GetPlayerScreen() == screen and (numberBuffer == "G" or #numberBuffer >= MAX_DIGITS or (keyPressTimeout > 0 and GetTime() > keyPressTimeout)) then
		keyPressTimeout = 0
		if numberBuffer == "G" then
			setDestinationFloor(0)
		else
			setDestinationFloor(tonumber(numberBuffer))
		end
		SetPlayerScreen(0)
	end
end


function getCurrentFloor()
	return GetInt("level.elevator" .. elevatorId .. ".currentFloor")
end

function getDestinationFloor()
	return GetInt("level.elevator" .. elevatorId .. ".destinationFloor")
end

function setDestinationFloor(num)
	SetInt("level.elevator" .. elevatorId .. ".destinationFloor", num)
end


function button(label)
	return UiTextButton(tostring(label), BUTTON_SIZE, BUTTON_SIZE) or InputPressed(tostring(label))
end

function addToBuffer(num)
	numberBuffer = numberBuffer .. tostring(num)
end

function resetBuffer()
	numberBuffer = ""
end