-- SRB1 Level Select Code

-- Set up vars.
local mx = 160
local my = 100

local submenu = 1

local mapselect = 1

local mouse1 = 100

local warptics = 175

local prevmap = 1

local thrumenu = false
local canexit = false

local mapwarps = {
	[1] = 101,
	[2] = 102,
	[3] = 103,
	[4] = 104,
	[5] = 105,
	[6] = 106,
	[7] = 107,
	[8] = 108,
	[9] = 109,
	[10] = 110,
	[11] = 111,
	[12] = 112,
	[13] = 113,
	[14] = 114,
	[15] = 115,
	[16] = 116,
	[17] = 117,
	[18] = 118,
	[19] = 119,
	[20] = 120,
	[21] = 123,
	[22] = 124,
	[23] = 127,
	[24] = 128,
	[25] = 129,
	[26] = 130,
	[27] = 131,
	[28] = 132,
}

local mappics = {
	[1] = "MAPA1P",
	[2] = "MAPA2P",
	[3] = "MAPA3P",
	[4] = "MAPA4P",
	[5] = "MAPA5P",
	[6] = "MAPA6P",
	[7] = "MAPA7P",
	[8] = "MAPA8P",
	[9] = "MAPA9P",
	[10] = "MAPAAP",
	[11] = "MAPABP",
	[12] = "MAPACP",
	[13] = "MAPADP",
	[14] = "MAPAEP",
	[15] = "MAPAFP",
	[16] = "MAPAGP",
	[17] = "MAPAHP",
	[18] = "MAPAIP",
	[19] = "MAPAJP",
	[20] = "MAPAKP",
	[21] = "MAPANP",
	[22] = "MAPAOP",
	[23] = "MAPARP",
	[24] = "MAPASP",
	[25] = "MAPATP",
	[26] = "MAPAUP",
	[27] = "MAPAVP",
	[28] = "MAPAWP",
}

-- Reset vars when map S1 is loaded.
addHook("MapChange", function(mapnum)
	if mapnum ~= 749 then return end

    mx = 8
    my = 8

    submenu = 1

    mouse1 = 0

	prevmap = gamemap
	warptics = 175
end)

addHook("MapLoad", function(mapnum)
	if mapnum ~= 749 then return end
	consoleplayer.lives = 5
end)

-- Main HUD function
hud.add(function(v, p)

    -- Only do this if were on map S1
    hud.enable("score")
    hud.enable("time")
    hud.enable("rings")
    --hud.enable("lives") This should already be handled in LUA_HUD.

	-- Draw a thingy if you went to an SRB1 map through the menu
	if canexit == true then
		v.drawString(312, 8, "Press\x82 C1 \x80to go back\nto the menu.", V_SNAPTOTOP|V_SNAPTORIGHT|V_ALLOWLOWERCASE, "right")
	end

    if gamemap ~= 749 then return end

	-- Draw the menus over the screen
	if submenu == 1 then
		local mainmenu = v.cachePatch("SRB1MAIN")
		v.drawScaled(0, 0, FRACUNIT/2, mainmenu)
	end

	if submenu == 2 then
		local diffmenu = v.cachePatch("SRB1DIFF")
		v.drawScaled(0, 0, FRACUNIT/2, diffmenu)
	end

	if submenu == 3 then
		local levelpic = v.cachePatch(mappics[mapselect])
		v.drawScaled(0, 0, FRACUNIT*2, levelpic)

		local scrollpic = v.cachePatch("SCROLL")
		v.draw(0, 0, scrollpic)

		v.drawString(2, 24, mapheaderinfo[mapwarps[mapselect]].lvlttl, V_YELLOWMAP)
		if mapheaderinfo[mapwarps[mapselect]].actnum ~= 0 then
			v.drawString(2, 32, "ACT " .. mapheaderinfo[mapwarps[mapselect]].actnum, V_YELLOWMAP)
		end
	end

	if submenu == 4 then
		local netgameerr = v.cachePatch("NGAMEERR")
		v.draw(0, 0, netgameerr)
	end

    hud.disable("score")
    hud.disable("time")
    hud.disable("rings")
    hud.disable("lives")

    -- Change mx and my by the movement of the mouse.
    mx = $ + mouse.dx / 12
    my = $ + mouse.dy / 12

	-- Get the screen width and height, and scale it to 320x200
	local sw = v.width() / v.dupx()
	local sh = v.height() / v.dupy()
	--print(sw .. ", " .. sh)

	-- Get the difference from 320 and 200 and sw and sh, for the left and top edges
	local sdw = 320 - sw
	local sdh = 200 - sh

	if mx > sw then mx = sw end
	if mx < sdw then mx = sdw end
	if my > sh then my = sh end
	if my < sdh then my = sdh end

    -- Draw the mouse.
    local mouseimg = v.cachePatch("MOUSE")
    v.drawScaled(mx * FRACUNIT, my * FRACUNIT, FRACUNIT/4, mouseimg)

end, "game")

-- Freeze the player on map S1.
addHook("PlayerThink", function(p)
    if gamemap == 749 then
        p.powers[pw_nocontrol] = 1

		if netgame then
			submenu = 4
			warptics = $ - 1
			if warptics < 0 then
				G_SetCustomExitVars(prevmap, 2)
				G_ExitLevel()
			end
		end

        -- Only trigger stuff when the mouse is clicked, not held.
        if mouse.buttons == MB_BUTTON1 then
            mouse1 = $ + 1
        else
            mouse1 = 0
        end
		--print(mouse1)

		-- Check if the player has clicked START GAME.
		-- Yes, I know im setting variables in the HUD hook, It doesn't matter,
		-- since you're only supposed to be here in singleplayer anyway.
		-- Set the mouse1 var to 100 too, to prevent accidental clicks
		if mx > 96 and mx < 224 and my > 64 and my < 80 and mouse1 == 1 and submenu == 1 then
			mouse1 = 100
			thrumenu = true
			G_SetCustomExitVars(101, 2)
			G_ExitLevel()
		end

		-- Check if they clicked DIFFICULTY SELECT.
		if mx > 96 and mx < 224 and my > 80 and my < 104 and mouse1 == 1 and submenu == 1 then
			mouse1 = 100
			submenu = 2
            S_ChangeMusic("MAPARM", true, p, 0x4000, 0) -- Reset the music too
		end

		-- Check if they clicked LEVEL SELECT.
		if mx > 96 and mx < 224 and my > 104 and my < 128 and mouse1 == 1 and submenu == 1 then
			mouse1 = 100
			submenu = 3 -- Change to 3 when needed
            S_ChangeMusic("MAPARM", true, p, 0x4000, 0)
		end


        -- Check for stuff on submenu 2, the difficulty menu.
        if mx > 64 and mx < 196 and my > 64 and my < 96 and mouse1 == 1 and submenu == 2 then
            p.lives = 9
			mouse1 = 100
			submenu = 1
            S_ChangeMusic("MAPARM", true, p, 0x4000, 0)
		end

        if mx > 64 and mx < 196 and my > 96 and my < 132 and mouse1 == 1 and submenu == 2 then
            p.lives = 5
			mouse1 = 100
			submenu = 1
            S_ChangeMusic("MAPARM", true, p, 0x4000, 0)
		end

        if mx > 64 and mx < 196 and my > 132 and my < 168 and mouse1 == 1 and submenu == 2 then
            p.lives = 3
			mouse1 = 100
			submenu = 1
            S_ChangeMusic("MAPARM", true, p, 0x4000, 0)
		end

		-- Check on submenu 3, the level select.
		if mx < 160 and my < 24 and (mouse1 == 1 or (mouse1 > 20 and leveltime % 3 == 1)) or mouse.buttons == MB_SCROLLUP and submenu == 3 then
            mapselect = $ - 1
            if mapselect < 1 then mapselect = 28 end
		end
        if mx > 160 and my < 24 and (mouse1 == 1 or (mouse1 > 20 and leveltime % 3 == 1)) or mouse.buttons == MB_SCROLLDOWN and submenu == 3 then
            mapselect = $ + 1
            if mapselect > 28 then mapselect = 1 end
		end

		if my > 24 and mouse1 == 1 and submenu == 3 then
			thrumenu = true
			G_SetCustomExitVars(mapwarps[mapselect], 2)
			G_ExitLevel()
		end
    end
end)

-- Code for pressing C1 to return to the menus
addHook("MapLoad", function(mapnum)
	if mapnum > 100 and mapnum < 133 and thrumenu == true then
		thrumenu = false
		canexit = true
	else
		thrumenu = false
	end
	if mapnum < 100 or mapnum > 133 then
		canexit = false
	end
end)

addHook("PlayerThink", function(p)
	if canexit == true then
		if p.cmd.buttons & BT_CUSTOM1 then
			G_SetCustomExitVars(749, 2)
			G_ExitLevel()
			thrumenu = false
			canexit = false
		end
	end
end)