dofile("lua/utils.lua")

-- Load the font.
Font = GetDefaultBitmapFont()
BitmapFont_EnableShadow(Font, false)
BitmapFont_EnableLineWrap(Font, false)
LineHeight = BitmapFont_GetLineHeight(Font)

LoadGU()
ShowLoadingScreen(Font, 5)

dofile(ProcessSkinFilePath("$/menus.lua"))
SetAnalogDeadZone(16)
Buttons = GetButtons()

Rot = 0 -- Cube/colour rotation counter
ScrollX = 480 -- Text scroller position
BGSaturation = 1
BGValue = 0
BGValueCount = 127

-- Random scrolling numbers to make it look cool
NumberScrollTimer = 60
NumberScrollYPos = 242
NumberScrollSet = {}
NumberScrollSetSize = 0
NumberScrollSetSizeMax = 25

-- Load background music.
-- '$' will be replaced with the path to this skin
LoadingStep("Loading music")
if EnableSound then
	Music, e = LoadMP3File("$/desktop.mp3");
	if not Music then
		ShowError(e)
		return
	end
	MP3_Loop(Music, -1) --loop forever
	MP3_Play(Music)
	MusicPlaying = true
else MusicPlaying = false
end

-- Load sound effects.
LoadingStep("Loading sounds")
if EnableSound then
	Sound, e = LoadSoundEffect("$/delete.wav");
	if not Sound then
		ShowError(e)
		return
	end
end

-- Load the cube.
Cube, e = AllocVerticesFromFile("$/cube.vtx")
if not Cube then
	ShowError(e)
	return
end

-- Read notes file.
LoadingStep("Reading notes file")
Notes = {}
for line in io.lines("notes.txt") do table.insert(Notes, line) end

-- Set up the background polygon. We draw it in 2D mode, which applies no
-- coordinate transformations, so we use screen coordinates.
BG = AllocVertices(4)
bg1 = {u=0.0, v=0.0, r=  0, g=255, b=192, a=255, x=  0, y=  0, z=0} -- top left
bg2 = {u=0.0, v=256, r=  0, g=127, b=255, a=255, x=  0, y=272, z=0} -- bottom left
bg3 = {u=480, v=0.0, r=  0, g=255, b=192, a=255, x=480, y=  0, z=0} -- top right
bg4 = {u=480, v=256, r=  0, g=127, b=255, a=255, x=480, y=272, z=0} -- bottom right
SetVertices(BG, 0, bg1, bg2, bg3, bg4)

-- Get installed apps
LoadingStep("Reading app list")
_Apps = GetInstalledPrograms()
if not _Apps then
	ShowError("Error getting program list!")
	return
elseif TableSize(_Apps) == 0 then
	_Apps = {["No programs found!"] = ""}
end

Apps = NestTable(_Apps, "dir", "name")
SortBySubTable(Apps, "name")

-- Get built-in apps
_BuiltinApps = GetBuiltinApps()
if not _BuiltinApps then
	ShowError("Error getting built-in program list!")
	return
elseif TableSize(_BuiltinApps) == 0 then
	_BuiltinApps = {["No programs found!"] = ""}
end

BuiltinApps = NestTable(_BuiltinApps, "id", "name")
SortBySubTable(BuiltinApps, "name")

-- Set up the menus.
LoadingStep("Initializing menus")
AppBase = "ms0:/PSP/GAME/"
AppExt = "/EBOOT.PBP"
CurDir = "ms0:/"
MenuID = 1
MenuTitle = {"select_program", "select_program", "select_file", "notes"}
MenuX = {100, 100, 100, 100}
MenuY = {16, 16, 16, 16}
MenuTable = {Apps, BuiltinApps, Files, Notes}
MenuDispKey = {"name", "name", nil, nil}
MenuNumOpts = {#Apps, #BuiltinApps, 0, 0}
MenuAcceptFunc = {RunThisApp, RunThisBuiltinApp, OpenThisFile, DoNothing} -- run when Accept pressed
MenuCancelFunc = {DoNothing, DoNothing, PrevDir, DoNothing} -- when Cancel pressed
MenuTriangleFunc = {DoNothing, DoNothing, PrevDir, DoNothing}
MenuSquareFunc = {ToggleMusic, ToggleMusic, ToggleMusic, ToggleMusic}
NumMenus = #MenuNumOpts -- max valid menu ID
MenuYMax = 250
Cursor = 0 -- Cursor index
IgnoreCursor = false -- Used to make the cursor move only once each button press
IgnoreButtons = false -- And the accept/cancel/etc buttons
Skip = 0 -- Number of menu items to leave off the top of the menu.
NumLines = 20 -- Number of menu items to display at once.
SelectedItem = 1
FileMenuID = 3 -- Which menu ID is the Select File menu

if DebugMode then MenuCancelFunc[1] = ExitProgram end

Days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}