#include "missions.lua"
#include "script/common.lua"
#include "ui/ui_helpers.lua"
#include "activities.lua"
#include "components/pros_view.lua"
#include "components/graphic_options_view.lua"
#include "components/audio_options_view.lua"
#include "components/game_options_view.lua"
#include "components/display_options_view.lua"
#include "components/input_options_view.lua"
#include "options_style.lua"

gOptionsShown = false

function reloadOptions()
	display_options_logic_init()
	graphic_options_logic_init()
	audio_options_logic_init()
	game_options_logic_init()
	input_options_logic_init()
end

function drawOptions(scale)

	if scale < 0.1 then
		if gOptionsShown and Options.selectedTab == "pros" then
			PermitScreenRecord()
		end

		if gOptionsShown then
			DisplaySettings:resetToCurrent()
		end

		gOptionsShown = false
		return true
	end

	if scale < 1.0 then
        UiForceFocus("")
		UiIgnoreNavigation()

        if Options.closeRequested == true then
            Options:OnClose()
            Options.closeRequested = false
        end
	end

	if not gOptionsShown then
		UiSound("common/options-on.ogg")
		gOptionsShown = true
	
		if Options.selectedTab == "pros" then
			ProhibitScreenRecord()
		end
	end

	UiModalBegin()
    -- we want to update mapping on every frame, where options are visible
    SetBool("game.updateinputmapping", true)

	if Options.selectedTab == "" then
		Options.selectedTab = Options.Tabs.Names[1].name
	end

	UiPush()
		UiFont("regular.ttf", 26)
		UiColorFilter(1, 1, 1, scale * gOptionTemporaryHideScale)

		UiTranslate(UiCenter(), UiMiddle())
		UiAlign("center middle")

		UiScale(1, scale)
		UiWindow(Options.Width, Options.Height)
		UiAlign("top left")

        -- request to close
		if wasAnyActionPressed( { "esc", "menu_cancel"} ) or (GetBool("game.cursor.enabled") and not UiIsMouseInRect(Options.Width, Options.Height) and InputPressed("lmb")) then
			UiSound("common/options-off.ogg")
			Command("pros.eventSettings")
            Options:Reset()
            Options.closeRequested = true
		end

		UiColor(.0, .0, .0, 0.55)
		UiImageBox("common/box-solid-shadow-50.png", Options.Width, Options.Height, -50, -50)
		--UiRoundedRect(Options.Width, Options.Height, 30) -- TODO: requires "drop shadow" property
		UiColor(0.96, 0.96, 0.96)

		UiPush()
			-- draw header text
			UiTranslate(0, 30)
			UiPush()
				UiAlign("middle center")
				UiFont("bold.ttf", 37)
				local ox,oy = UiCenter()
				UiTranslate(ox, oy)
				local header = GetTranslatedStringByKey('UI_BUTTON_OPTIONS_UPPERCASE')
				UiText(header)
			UiPop()
			UiTranslate(0, 30)

			UiIgnoreNavigation()

			UiFont("regular.ttf", 26)
			UiButtonImageBox("common/box-outline-fill-6.png", Options.Tabs.Radius, Options.Tabs.Radius, 0.96, 0.96, 0.96, 0.8)
			UiAlign("top left")
			UiScale(1)
			UiTranslate(Options.Tabs:StartPosition(), 18)
			
			local oldTab = Options.selectedTab

			Options:DrawTabs()

			if InputPressed("menu_next") then
				Options:SelectNextTab()
			end

			if InputPressed("menu_prev") then
				Options:SelectPreviousTab()
			end

			-- tab changed handling
			if Options.selectedTab ~= oldTab then
				UiForceFocus("");
				UiSound("common/click.ogg")

				if Options.selectedTab == "pros" then
					ProhibitScreenRecord()
				end

				if Options.selectedTab == "pros" then
					PermitScreenRecord()
				end

				DisplaySettings:resetToCurrent()
			end
		UiPop()

		UiPush()
			UiFont("regular.ttf", 22)
			UiTranslate(0, 200)

			local x0 = UiCenter() - 20
			
			UiTranslate(x0, 0)
			UiAlign("right")

			if Options.selectedTab == "display" then			
				DisplaySettings:draw()
				DisplaySettings:update()
			end

			if Options.selectedTab == "gfx" then
				drawGraphicsOptions()
			end
			
			if Options.selectedTab == "audio" then
				drawAudioOptions()
			end
			
			if Options.selectedTab == "game" then
				drawGameOptions()
			end
			
			if Options.selectedTab == "input" then
				if not IsRunningOnPC() and not IsRunningOnApple() then
				    drawGamepadOptions()
				else
					drawInputOptions()
				end	
			end

			if Options.selectedTab == "pros" then
				drawProsTab()
			end
		UiPop()
	UiPop()

	UiModalEnd()	
	return not Options.closeRequested
end
