GameOn = true
if Option.Debug.GetInfo == true then UserInfo.AddVal("Manager", 1) end

-- That "Method" variable was really annoying after he became a menu with 6 scenarios, not 3.
dofile("Configuration/LevelMenuManager.cfg")

local BG = Image.load("Game Resources/Backgrounds/CustomGame.png")

local SB = 0 --Main Select
local MaxMap = 2

Arial = Font.load("Font/Arial.TTF")
Arial:setPixelSizes(12,12)

local TimerBool = false

local Directer = nil


local TimerFade = 0

local NewScn = {
	S1 = {
		Name = "",
		DT1 = "",
		DT2 = "",
		DT3 = "",
		MapGenre = "",
		Difficult = nil,
		Version = nil,
		Update = nil,
		Prefix = nil,
	},
	S2 = {
		Name = "",
		DT1 = "",
		DT2 = "",
		DT3 = "",
		MapGenre = "",
		Difficult = nil,
		Version = nil,
		Update = nil,
		Prefix = nil,
	},
	S3 = {
		Name = "",
		DT1 = "",
		DT2 = "",
		DT3 = "",
		MapGenre = "",
		Difficult = nil,
		Version = nil,
		Update = nil,
		Prefix = nil,
	},
	S4 = {
		Name = "",
		DT1 = "",
		DT2 = "",
		DT3 = "",
		MapGenre = "",
		Difficult = nil,
		Version = nil,
		Update = nil,
		Prefix = nil,
	},
	S5 = {
		Name = "",
		DT1 = "",
		DT2 = "",
		DT3 = "",
		MapGenre = "",
		Difficult = nil,
		Version = nil,
		Update = nil,
		Prefix = nil,
	},
	S6 = {
		Name = "",
		DT1 = "",
		DT2 = "",
		DT3 = "",
		MapGenre = "",
		Difficult = nil,
		Version = nil,
		Update = nil,
		Prefix = nil,
	},
}

ColorConfig = "Blue"

spr = {
	uni = {
		--Villager = Image.load("Game Resources/Units/Villager/"..ColorConfig.."/MVillager.png"),
		VillagerMale = {
			Down = nil,
			Right = nil,
			Left = nil,
			Up = nil,
			UpLeft = nil,
			UpRight = nil,
			DownLeft = nil,
			DownRight = nil,
			
			Icon = nil,
		},
		VillagerFemale = {
			Down = nil,
			Right = nil,
			Left = nil,
			Up = nil,
			UpLeft = nil,
			UpRight = nil,
			DownLeft = nil,
			DownRight = nil,
			
			Icon = nil,
		},
		Militia = {
			Down = nil,
			Right = nil,
			Left = nil,
			Up = nil,
			UpLeft = nil,
			UpRight = nil,
			DownLeft = nil,
			DownRight = nil,
			
			Icon = nil,
		},
	},
	
	
	
	build = {
		TC = {
			Dark = nil,
			
			Icon = nil,
		},
		House = {
			DarkTy1 = nil,
			DarkTy2 = nil,
			DarkTy3 = nil,
			
			Icon = nil,
		},
	},
	
	
	
	res = {
		Crystal = {
			Ty1 = nil,
		},
		Food = {
			Ty1 = nil,
			Ty2 = nil,
			Ty3 = nil,
			Icon = nil,
		},
		Tree = {
			Oak1 = nil,
			Oak2 = nil,
			Oak3 = nil,
			Oak4 = nil,
			Oak5 = nil,
		},
		Stone = {
			Ty1 = nil,
			Ty2 = nil,
		},
		Gold = {
			
		},
		Artifact = {
			Ty1 = nil,
			Ty2 = nil,
		},
	},
}

if Method == 1 then dofile("Configuration/Map1.cfg") end
if Method == 2 then LoadCustomMap("CustomMap1", "scn2psp") end
NewScn.S1.Name = Scenario.Name
NewScn.S1.DT1 = Scenario.DT1
NewScn.S1.DT2 = Scenario.DT2
NewScn.S1.DT3 = Scenario.DT3
NewScn.S1.MapGenre = Scenario.MapGenre
NewScn.S1.Difficult = Scenario.Difficult
NewScn.S1.Version = Scenario.Version
NewScn.S1.Update = Scenario.Update
NewScn.S1.Prefix = Scenario.Prefix

if Method == 1 then dofile("Configuration/Map2.cfg") end
if Method == 2 then LoadCustomMap("CustomMap2", "scn2psp") end
NewScn.S2.Name = Scenario.Name
NewScn.S2.DT1 = Scenario.DT1
NewScn.S2.DT2 = Scenario.DT2
NewScn.S2.DT3 = Scenario.DT3
NewScn.S2.MapGenre = Scenario.MapGenre
NewScn.S2.Difficult = Scenario.Difficult
NewScn.S2.Version = Scenario.Version
NewScn.S2.Update = Scenario.Update
NewScn.S2.Prefix = Scenario.Prefix

if Method == 1 then dofile("Configuration/Map3.cfg") end
if Method == 2 then LoadCustomMap("CustomMap3", "scn2psp") end
NewScn.S3.Name = Scenario.Name
NewScn.S3.DT1 = Scenario.DT1
NewScn.S3.DT2 = Scenario.DT2
NewScn.S3.DT3 = Scenario.DT3
NewScn.S3.MapGenre = Scenario.MapGenre
NewScn.S3.Difficult = Scenario.Difficult
NewScn.S3.Version = Scenario.Version
NewScn.S3.Update = Scenario.Update
NewScn.S3.Prefix = Scenario.Prefix

if Method == 2 then
LoadCustomMap("CustomMap4", "scn2psp")
NewScn.S4.Name = Scenario.Name
NewScn.S4.DT1 = Scenario.DT1
NewScn.S4.DT2 = Scenario.DT2
NewScn.S4.DT3 = Scenario.DT3
NewScn.S4.MapGenre = Scenario.MapGenre
NewScn.S4.Difficult = Scenario.Difficult
NewScn.S4.Version = Scenario.Version
NewScn.S4.Update = Scenario.Update
NewScn.S4.Prefix = Scenario.Prefix

LoadCustomMap("CustomMap5", "scn2psp")
NewScn.S5.Name = Scenario.Name
NewScn.S5.DT1 = Scenario.DT1
NewScn.S5.DT2 = Scenario.DT2
NewScn.S5.DT3 = Scenario.DT3
NewScn.S5.MapGenre = Scenario.MapGenre
NewScn.S5.Difficult = Scenario.Difficult
NewScn.S5.Version = Scenario.Version
NewScn.S5.Update = Scenario.Update
NewScn.S5.Prefix = Scenario.Prefix

LoadCustomMap("CustomMap6", "scn2psp")
NewScn.S6.Name = Scenario.Name
NewScn.S6.DT1 = Scenario.DT1
NewScn.S6.DT2 = Scenario.DT2
NewScn.S6.DT3 = Scenario.DT3
NewScn.S6.MapGenre = Scenario.MapGenre
NewScn.S6.Difficult = Scenario.Difficult
NewScn.S6.Version = Scenario.Version
NewScn.S6.Update = Scenario.Update
NewScn.S6.Prefix = Scenario.Prefix
end

while GameOn do
	pad = Controls.read()

	screen:blit(0,0,BG)
	
	if Method == 1 then screen:fontPrint(Arial,185,20,"Load Saved Game",white) end
	if Method == 2 then screen:fontPrint(Arial,185,20,"Load Custom Map",white) end
	
	screen:fontPrint(Arial,280,50,"Name: ",white)
	
	
	
	screen:fontPrint(Arial,280,130,"Genre: ",white)
	screen:fontPrint(Arial,280,150,"Difficult: ",white)
	screen:fontPrint(Arial,280,170,"Version: ",white)
	screen:fontPrint(Arial,280,190,"Update: ",white)
	
	if Method == 1 then
		if SB == 0 then
			screen:fontPrint(Arial,50,50,NewScn.S1.Name,yellow)
			screen:fontPrint(Arial,50,70,NewScn.S2.Name,white)
			screen:fontPrint(Arial,50,90,NewScn.S3.Name,white)
			
			screen:fontPrint(Arial,320,50,NewScn.S1.Name,white)
			screen:fontPrint(Arial,280,70,"| "..NewScn.S1.DT1,white)
			screen:fontPrint(Arial,280,90,"| "..NewScn.S1.DT2,white)
			screen:fontPrint(Arial,280,110,"| "..NewScn.S1.DT3,white)
			screen:fontPrint(Arial,340,130,NewScn.S1.MapGenre,white)
			screen:fontPrint(Arial,340,150,NewScn.S1.Difficult,white)
			screen:fontPrint(Arial,340,170,NewScn.S1.Version,white)
			screen:fontPrint(Arial,340,190,NewScn.S1.Update,white)
		elseif SB == 1 then
			screen:fontPrint(Arial,50,50,NewScn.S1.Name,white)
			screen:fontPrint(Arial,50,70,NewScn.S2.Name,yellow)
			screen:fontPrint(Arial,50,90,NewScn.S3.Name,white)
			
			screen:fontPrint(Arial,320,50,NewScn.S2.Name,white)
			screen:fontPrint(Arial,280,70,"| "..NewScn.S2.DT1,white)
			screen:fontPrint(Arial,280,90,"| "..NewScn.S2.DT2,white)
			screen:fontPrint(Arial,280,110,"| "..NewScn.S2.DT3,white)
			screen:fontPrint(Arial,340,130,NewScn.S2.MapGenre,white)
			screen:fontPrint(Arial,340,150,NewScn.S2.Difficult,white)
			screen:fontPrint(Arial,340,170,NewScn.S2.Version,white)
			screen:fontPrint(Arial,340,190,NewScn.S2.Update,white)
		elseif SB == 2 then
			screen:fontPrint(Arial,50,50,NewScn.S1.Name,white)
			screen:fontPrint(Arial,50,70,NewScn.S2.Name,white)
			screen:fontPrint(Arial,50,90,NewScn.S3.Name,yellow)
			
			screen:fontPrint(Arial,320,50,NewScn.S3.Name,white)
			screen:fontPrint(Arial,280,70,"| "..NewScn.S3.DT1,white)
			screen:fontPrint(Arial,280,90,"| "..NewScn.S3.DT2,white)
			screen:fontPrint(Arial,280,110,"| "..NewScn.S3.DT3,white)
			screen:fontPrint(Arial,340,130,NewScn.S3.MapGenre,white)
			screen:fontPrint(Arial,340,150,NewScn.S3.Difficult,white)
			screen:fontPrint(Arial,340,170,NewScn.S3.Version,white)
			screen:fontPrint(Arial,340,190,NewScn.S3.Update,white)
		end
	end
	if Method == 2 then
		if SB == 0 then
			screen:fontPrint(Arial,50,50,NewScn.S1.Name,yellow)
			screen:fontPrint(Arial,50,70,NewScn.S2.Name,white)
			screen:fontPrint(Arial,50,90,NewScn.S3.Name,white)
			screen:fontPrint(Arial,50,110,NewScn.S4.Name,white)
			screen:fontPrint(Arial,50,130,NewScn.S5.Name,white)
			screen:fontPrint(Arial,50,150,NewScn.S6.Name,white)
			
			screen:fontPrint(Arial,320,50,NewScn.S1.Name,white)
			screen:fontPrint(Arial,280,70,"| "..NewScn.S1.DT1,white)
			screen:fontPrint(Arial,280,90,"| "..NewScn.S1.DT2,white)
			screen:fontPrint(Arial,280,110,"| "..NewScn.S1.DT3,white)
			screen:fontPrint(Arial,340,130,NewScn.S1.MapGenre,white)
			screen:fontPrint(Arial,340,150,NewScn.S1.Difficult,white)
			screen:fontPrint(Arial,340,170,NewScn.S1.Version,white)
			screen:fontPrint(Arial,340,190,NewScn.S1.Update.." "..Prefix,white)
		elseif SB == 1 then
			screen:fontPrint(Arial,50,50,NewScn.S1.Name,white)
			screen:fontPrint(Arial,50,70,NewScn.S2.Name,yellow)
			screen:fontPrint(Arial,50,90,NewScn.S3.Name,white)
			screen:fontPrint(Arial,50,110,NewScn.S4.Name,white)
			screen:fontPrint(Arial,50,130,NewScn.S5.Name,white)
			screen:fontPrint(Arial,50,150,NewScn.S6.Name,white)
			
			screen:fontPrint(Arial,320,50,NewScn.S2.Name,white)
			screen:fontPrint(Arial,280,70,"| "..NewScn.S2.DT1,white)
			screen:fontPrint(Arial,280,90,"| "..NewScn.S2.DT2,white)
			screen:fontPrint(Arial,280,110,"| "..NewScn.S2.DT3,white)
			screen:fontPrint(Arial,340,130,NewScn.S2.MapGenre,white)
			screen:fontPrint(Arial,340,150,NewScn.S2.Difficult,white)
			screen:fontPrint(Arial,340,170,NewScn.S2.Version,white)
			screen:fontPrint(Arial,340,190,NewScn.S2.Update.." "..Prefix,white)
		elseif SB == 2 then
			screen:fontPrint(Arial,50,50,NewScn.S1.Name,white)
			screen:fontPrint(Arial,50,70,NewScn.S2.Name,white)
			screen:fontPrint(Arial,50,90,NewScn.S3.Name,yellow)
			screen:fontPrint(Arial,50,110,NewScn.S4.Name,white)
			screen:fontPrint(Arial,50,130,NewScn.S5.Name,white)
			screen:fontPrint(Arial,50,150,NewScn.S6.Name,white)
			
			screen:fontPrint(Arial,320,50,NewScn.S3.Name,white)
			screen:fontPrint(Arial,280,70,"| "..NewScn.S3.DT1,white)
			screen:fontPrint(Arial,280,90,"| "..NewScn.S3.DT2,white)
			screen:fontPrint(Arial,280,110,"| "..NewScn.S3.DT3,white)
			screen:fontPrint(Arial,340,130,NewScn.S3.MapGenre,white)
			screen:fontPrint(Arial,340,150,NewScn.S3.Difficult,white)
			screen:fontPrint(Arial,340,170,NewScn.S3.Version,white)
			screen:fontPrint(Arial,340,190,NewScn.S3.Update.." "..Prefix,white)
		elseif SB == 3 then
			screen:fontPrint(Arial,50,50,NewScn.S1.Name,white)
			screen:fontPrint(Arial,50,70,NewScn.S2.Name,white)
			screen:fontPrint(Arial,50,90,NewScn.S3.Name,white)
			screen:fontPrint(Arial,50,110,NewScn.S4.Name,yellow)
			screen:fontPrint(Arial,50,130,NewScn.S5.Name,white)
			screen:fontPrint(Arial,50,150,NewScn.S6.Name,white)
			
			screen:fontPrint(Arial,320,50,NewScn.S4.Name,white)
			screen:fontPrint(Arial,280,70,"| "..NewScn.S4.DT1,white)
			screen:fontPrint(Arial,280,90,"| "..NewScn.S4.DT2,white)
			screen:fontPrint(Arial,280,110,"| "..NewScn.S4.DT3,white)
			screen:fontPrint(Arial,340,130,NewScn.S4.MapGenre,white)
			screen:fontPrint(Arial,340,150,NewScn.S4.Difficult,white)
			screen:fontPrint(Arial,340,170,NewScn.S4.Version,white)
			screen:fontPrint(Arial,340,190,NewScn.S4.Update.." "..Prefix,white)
		elseif SB == 4 then
			screen:fontPrint(Arial,50,50,NewScn.S1.Name,white)
			screen:fontPrint(Arial,50,70,NewScn.S2.Name,white)
			screen:fontPrint(Arial,50,90,NewScn.S3.Name,white)
			screen:fontPrint(Arial,50,110,NewScn.S4.Name,white)
			screen:fontPrint(Arial,50,130,NewScn.S5.Name,yellow)
			screen:fontPrint(Arial,50,150,NewScn.S6.Name,white)
			
			screen:fontPrint(Arial,320,50,NewScn.S4.Name,white)
			screen:fontPrint(Arial,280,70,"| "..NewScn.S4.DT1,white)
			screen:fontPrint(Arial,280,90,"| "..NewScn.S4.DT2,white)
			screen:fontPrint(Arial,280,110,"| "..NewScn.S4.DT3,white)
			screen:fontPrint(Arial,340,130,NewScn.S4.MapGenre,white)
			screen:fontPrint(Arial,340,150,NewScn.S4.Difficult,white)
			screen:fontPrint(Arial,340,170,NewScn.S4.Version,white)
			screen:fontPrint(Arial,340,190,NewScn.S4.Update.." "..Prefix,white)
		elseif SB == 5 then
			screen:fontPrint(Arial,50,50,NewScn.S1.Name,white)
			screen:fontPrint(Arial,50,70,NewScn.S2.Name,white)
			screen:fontPrint(Arial,50,90,NewScn.S3.Name,white)
			screen:fontPrint(Arial,50,110,NewScn.S4.Name,white)
			screen:fontPrint(Arial,50,130,NewScn.S5.Name,white)
			screen:fontPrint(Arial,50,150,NewScn.S6.Name,yellow)
			
			screen:fontPrint(Arial,320,50,NewScn.S4.Name,white)
			screen:fontPrint(Arial,280,70,"| "..NewScn.S4.DT1,white)
			screen:fontPrint(Arial,280,90,"| "..NewScn.S4.DT2,white)
			screen:fontPrint(Arial,280,110,"| "..NewScn.S4.DT3,white)
			screen:fontPrint(Arial,340,130,NewScn.S4.MapGenre,white)
			screen:fontPrint(Arial,340,150,NewScn.S4.Difficult,white)
			screen:fontPrint(Arial,340,170,NewScn.S4.Version,white)
			screen:fontPrint(Arial,340,190,NewScn.S4.Update.." "..Prefix,white)
		end
	end
	
	if TimerBool == true then
		TimerFade = TimerFade + 1
	end
	if Method == 1 then
		if TimerFade > 36 then
			Fade(25)
			TimerBool = false
			if Directer == 0 then
				TimerFade = 0
				SavePath("1")
			elseif Directer == 1 then
				TimerFade = 0
				SavePath("2")
			elseif Directer == 2 then
				TimerFade = 0
				SavePath("3")
			end
			dofile("GameScript.lua")
		end
	end
	
	if Method == 2 then
		if TimerFade > 36 then
			Fade(25)
			TimerBool = false
			if Directer == 0 then
				TimerFade = 0
				SavePath("E1")
			elseif Directer == 1 then
				TimerFade = 0
				SavePath("E2")
			elseif Directer == 2 then
				TimerFade = 0
				SavePath("E3")
			elseif Directer == 3 then
				TimerFade = 0
				SavePath("E4")
			elseif Directer == 4 then
				TimerFade = 0
				SavePath("E5")
			elseif Directer == 5 then
				TimerFade = 0
				SavePath("E6")
			end
			dofile("GameScript.lua")
		end
	end
	
	if pad:up() then
		if pressed.up == false then
			pressed.up = true
			SB = SB - 1
		end
	else
		pressed.up = false
	end
	
	if pad:down() then 
		if pressed.down == false then
			pressed.down = true
			SB = SB + 1 
		end
	else
		pressed.down = false
	end
	
	--[[
	if SB == 0 then
		DT1 = "Standart Game With Optional"
		DT2 = "Rules"
		DT3 = ""
	elseif SB == 1 then
		DT1 = "Play Loaded Game!"
		DT2 = ""
		DT3 = ""
	elseif SB == 2 then
		DT1 = "Play Custom Maded Maps!"
		DT2 = ""
		DT3 = ""
	end
	--]]
	
	if Method == 1 then
		if SB > MaxMap then SB = 0 end
		if SB < 0 then SB = MaxMap end
	elseif Method == 2 then
		if SB > MaxMap+3 then SB = 0 end
		if SB < 0 then SB = MaxMap+3 end
	end
	
	if Method == 1 then
		if pad:cross() then
			Mp3me.stop()
			Mp3me.load("Sounds/select2.mp3")
			Mp3me.play()
			TimerBool = true
			if SB == 0 then Directer = 0
			elseif SB == 1 then Directer = 1
			elseif SB == 2 then Directer = 2
			--elseif SB == 3 then Directer = 3
			--elseif SB == 4 then Directer = 4
			--elseif SB == 5 then Directer = 5
			--elseif SB == 6 then Directer = 6
			end
		end
	elseif Method == 2 then
		if pad:cross() then
			Mp3me.stop()
			Mp3me.load("Sounds/select2.mp3")
			Mp3me.play()
			TimerBool = true
			if SB == 0 then Directer = 0
			elseif SB == 1 then Directer = 1
			elseif SB == 2 then Directer = 2
			elseif SB == 3 then Directer = 3
			elseif SB == 4 then Directer = 4
			elseif SB == 5 then Directer = 5
			elseif SB == 6 then Directer = 6
			end
		end
	end
	
	if pad:circle() then LoadMenuParts("MainMenu") dofile("MainMenu.lua") end
	
	if pad:select() then
		Screenshot()
	end

	screen.waitVblankStart(1)
	screen.flip()
end