GameOn = true
dofile("Functions.lua")
dofile("Configuration/Options.cfg")
if Option.Debug.GetInfo == true then UserInfo.AddVal("Menu", 1) end

Red    = Color.new(255, 0,		0)
Blue   = Color.new(0, 	0,		255)
Green  = Color.new(0, 	255, 	0)
Black  = Color.new(0, 	0, 		0)
White  = Color.new(255,	255,	255)
Gray   = Color.new(180,	180,	180)
DaGray = Color.new(100, 100,	100)
Purple = Color.new(91,	16,		50)
Yellow = Color.new(255,	255,	0)

red    = Color.new(255, 0,	0)
blue   = Color.new(0, 	0,		255)
green  = Color.new(0, 	255, 	0)
black  = Color.new(0, 	0, 		0)
white  = Color.new(255,	255,	255)
purple = Color.new(91,	16,		50)
yellow = Color.new(255,	255,	0)

-- Global Variables
MV = 0.1 -- MainVersion
DevState = "indev"
UpdData = "19.09.2019"
Update = 3
Prefix = "[F]"
UpdateName = "Better and Faster Play Update"
Music = 1
-- 1 - Main Theme
-- 2 - Silent Theme
Tick = 0

Textures("Delete")
LoadMenuParts("MainMenu")

-- this guy saw more than you

local Select = {
	SP = 0,
	Edit = 0,
	Option = 0,
}

local Page = 1

local DT1 = "Standart Game With Optional" -- DT = Description Text
local DT2 = "Rules"
local DT3 = ""

Arial = Font.load("Font/Arial.TTF")

-- Thanks to Exnonull for theese variables for an aditional "press button way".
-- Originally pad:button() is "Held" but this type is "Pressed"
pressed={
	left=false,
	up=false,
	right=false,
	down=false,
	r=false,
	l=false,
	start=false,
	sel=false,
	cross=false,
	triangle=false,
	square=false,
	circle=false
}

--[[
if Music == 1 then
	Mp3me.stop()
	Mp3me.load("Sounds/stream/xopen.mp3")
	Mp3me.play()
end
if Music == 2 then
	Mp3me.stop()
	Mp3me.load("Sounds/stream/xtown.mp3")
	Mp3me.play()
end
--]]

TimerBool = false
Directer = nil
TimerFade = 0

-- MusicPlay = true (for debug)

if MusicPlay == true then
	Tick = 0
elseif MusicPlay == false then
	Tick = 690
end

CrState = false
-- Credits State
-- It's "Page = 2"
Credits = 1
-- 1 = Main Information
-- 2 = Second Information
-- 3 = Third Information

AllowNavigate = true

-------------------------------------------------------------------------------------------------
while GameOn do ---------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
	pad = Controls.read()
	Arial:setPixelSizes(13,13)
	
	-- Tracks
	if Tick == 0 then
		Mp3me.stop()
		Mp3me.load("Sounds/stream/xopen.mp3")
		Mp3me.play()
	end
	if Tick == 725 then
		Mp3me.stop()
		Mp3me.load("Sounds/stream/xtown.mp3")
		Mp3me.play()
	end
	if Tick == 2100 then Tick = 500 end
	
	screen:blit(0,0,BG)
	screen:blit(209,198,P1)
	screen:blit(266,0,P2)
	
	-- Graphic Part
	if Page == 3 then
		screen:fontPrint(Arial,333-1,18+1,"Single Player",black)
		screen:fontPrint(Arial,333,18,"Single Player",white)
		
		screen:blit(274,48,EB)
		screen:blit(274,88,EB)
		screen:blit(274,128,EB)
		--screen:blit(274,168,EB)
	end
	if Page == 4 then
		screen:fontPrint(Arial,348-1,18+1,"Editor",black)
		screen:fontPrint(Arial,348,18,"Editor",white)
		
		screen:blit(274,48,EB)
		screen:blit(274,88,EB)
		screen:blit(274,140,EB)
	end
	if Page == 5 then
		screen:fontPrint(Arial,348-1,18+1,"Options",black)
		screen:fontPrint(Arial,348,18,"Options",white)
		
		screen:blit(274,48,EB)
		screen:fontPrint(Arial,322,67,"Open Options",yellow)
	end
	
	-- Actions Part
	
	if Page == 1 then
		DT1 = ""
		DT2 = ""
		DT3 = ""
	end
	
	if Page == 2 then
		DT1 = "Game originally developed by Ensemble"
		DT2 = "Studios for Microsoft corp. in 2000"
		DT3 = ""
	end
	
	-- Single Player
	if Page == 3 then
	
		Arial:setPixelSizes(13,13)
		if Select.SP == 0 then 
			screen:fontPrint(Arial,322,67,"Standart Game",yellow)
			screen:fontPrint(Arial,332,107,"Load Game",white)
			screen:fontPrint(Arial,332,147,"Custom Map",white)
		elseif Select.SP == 1 then
			screen:fontPrint(Arial,322,67,"Standart Game",white)
			screen:fontPrint(Arial,332,107,"Load Game",yellow)
			screen:fontPrint(Arial,332,147,"Custom Map",white)
		elseif Select.SP == 2 then
			screen:fontPrint(Arial,322,67,"Standart Game",white)
			screen:fontPrint(Arial,332,107,"Load Game",white)
			screen:fontPrint(Arial,332,147,"Custom Map",yellow)
		end
		
		if Select.SP == 0 then
			DT1 = "Standart game with optional rules"
			DT2 = ""
			DT3 = ""
		elseif Select.SP == 1 then
			DT1 = "Play saved game!"
			DT2 = ""
			DT3 = ""
		elseif Select.SP == 2 then
			DT1 = "Play your custom maps, made"
			DT2 = "in game editor"
			DT3 = ""
		elseif Select.SP == 3 then
			DT1 = "Edit or create or play your own maps"
			DT2 = "and publish it to community!"
			DT3 = ""
		else
			DT1 = ""
			DT2 = ""
			DT3 = ""
		end
		
	end
	
	-- Editor
	if Page == 4 then
		if Select.Edit == 0 then 
			screen:fontPrint(Arial,320,67,"Create a Scenario",yellow)
			screen:fontPrint(Arial,325,107,"Edit a Scenario",white)
			screen:fontPrint(Arial,325,159,"Campaign Editor",white)
		elseif Select.Edit == 1 then
			screen:fontPrint(Arial,320,67,"Create a Scenario",white)
			screen:fontPrint(Arial,325,107,"Edit a Scenario",yellow)
			screen:fontPrint(Arial,325,159,"Campaign Editor",white)
		elseif Select.Edit == 2 then
			screen:fontPrint(Arial,320,67,"Create a Scenario",white)
			screen:fontPrint(Arial,325,107,"Edit a Scenario",white)
			screen:fontPrint(Arial,325,159,"Campaign Editor",yellow)
		end
		
		if Select.Edit == 0 then
			DT1 = "Create a new scenario with your own story"
			DT2 = ""
			DT3 = ""
		elseif Select.Edit == 1 then
			DT1 = "Edit your own scenarios"
			DT2 = "or edit community's scenarios"
			DT3 = ""
		elseif Select.Edit == 2 then
			DT1 = "Campaign Editor allow you to make a"
			DT2 = "combinations of your scenarios to make one"
			DT3 = "big story"
		else
			DT1 = ""
			DT2 = ""
			DT3 = ""
		end
		
	end
	
	if Page == 5 then
		
		if Select.Option == 0 then
			DT1 = "Change your game options."
			DT2 = "You can change sound options, game options"
			DT3 = "or even a graphic options!"
		end
	end
	
	Arial:setPixelSizes(11,11)
	screen:fontPrint(Arial,240,222,DT1,white)
	screen:fontPrint(Arial,240,234,DT2,white)
	screen:fontPrint(Arial,240,244,DT3,white)
	
	Tick = Tick + 1
	
	Arial:setPixelSizes(9,9)
	--screen:fontPrint(Arial,5,235,"Tick = "..Tick,white)
	--screen:fontPrint(Arial,5,249,"Lua Edition  -  "..UpdateName,white)
	screen:fontPrint(Arial,5,257,"Lua Edition  -  "..UpdateName,white)
	screen:fontPrint(Arial,5,265,"Age Of Empires II "..DevState.." "..MV.." By MasterMen. Update from "..UpdData.."  (Update "..Update..")  "..Prefix,white)
	
	Arial:setPixelSizes(9,9)
	if Page == 1 then
		screen:blit(1, 1, Training)
		screen:blit(1, 1, CHTraining)
	else
		screen:blit(1, 1, Training)
	end
	screen:fontPrint(Arial,13-1,98+1,"Tutorial",black)
	screen:fontPrint(Arial,13,98,"Tutorial",white)
	
	if Page == 2 then
		screen:blit(58, 7, About)
		screen:blit(58, 7, CHAbout)
	else
		screen:blit(58, 7, About)
	end
	
	if Page == 3 then
		screen:blit(141, 0, SinglePlayer)
		screen:blit(141, 0, CHSinglePlayer)
	else
		screen:blit(141, 0, SinglePlayer)
	end
	screen:fontPrint(Arial,192-1,12+1,"Single Player",black)
	screen:fontPrint(Arial,192,12,"Single Player",white)
	
	if Page == 4 then
		screen:blit(110, 86, Editor)
		screen:blit(110, 86, CHEditor)
	else
		screen:blit(110, 86, Editor)
	end
	screen:fontPrint(Arial,121-1,96+1,"Editor",black)
	screen:fontPrint(Arial,121,96,"Editor",white)
	
	if Page == 5 then
		screen:blit(55, 130, Options)
		screen:blit(55, 130, CHOptions)
	else
		screen:blit(55, 130, Options)
	end
	screen:fontPrint(Arial,65-1,140+1,"Options",black)
	screen:fontPrint(Arial,65,140,"Options",white)
	
	
	
	
	
	
	Arial:setPixelSizes(13,13)
	
	if AllowNavigate == true then
		if pad:l() and CrState == false then
			if pressed.l == false then
				pressed.l = true
				Page = Page - 1
				if Page < 1 then Page = 5 end
			end
		else
			pressed.l = false
		end
		
		if pad:r() and CrState == false then
			if pressed.r == false then
				pressed.r = true
				Page = Page + 1
				if Page > 5 then Page = 1 end
			end
		else
			pressed.r = false
		end
	end
	
	if TimerBool == true then
		TimerFade = TimerFade + 1
	end
	
	-- 322, 67
	-- 322, 107
	-- 322, 147
	-- 322, 187
	
	if Page == 1 or Page == 5 then
		DT1 = ""
		DT2 = ""
		DT3 = ""
	end
	
	-- Single Player 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
	
		-- Directer = 0 - Standart Game
		-- Directer = 1 - Load Game
		-- Directer = 2 - Custom Map
		-- Directer = 3 - Editor Menu
		-- Directer = 4 - Editor Menu
		
		if TimerFade > 11 then
			AllowNavigate = false -- From here: You can't push any buttons until the new script will be loaded
			AllowNavigate = false -- From here: You can't push any buttons until the new script will be loaded
			Fade(35)
			if Directer == 0 then
				TimerBool = false
				TimerFade = 0
				ClearRAM("MainMenu")
				MusicPlay = false
				dofile("CreateGame.lua")
			elseif Directer == 1 then
				TimerBool = false
				TimerFade = 0
				LevelManager = io.open("Configuration/LevelMenuManager.cfg", "w")
				LevelManager:write("Method = 1 \n-- 1 = Load Saved Game \n-- 2 = Load Custom Map")
				LevelManager:close()
				ClearRAM("MainMenu")
				MusicPlay = false
				dofile("MapManager.lua")
			elseif Directer == 2 then
				TimerBool = false
				TimerFade = 0
				LevelManager = io.open("Configuration/LevelMenuManager.cfg", "w")
				LevelManager:write("Method = 2 \n-- 1 = Load Saved Game \n-- 2 = Load Custom Map")
				LevelManager:close()
				ClearRAM("MainMenu")
				MusicPlay = false
				dofile("MapManager.lua")
			elseif Directer == 3 then
				TimerBool = false
				TimerFade = 0
				ClearRAM("MainMenu")
				MusicPlay = false
				dofile("EditorMenu.lua")
			elseif Directer == 4 then
				TimerBool = false
				TimerFade = 0
				ClearRAM("MainMenu")
				MusicPlay = false
				dofile("EditorMenu.lua")
			elseif Directer == 5 then
				TimerBool = false
				TimerFade = 0
				-- Not needed
				MusicPlay = false
				dofile("Options.lua")
				
			end
		end
	
	if pad:select() then Screenshot() end
	
	
	
	
	
	-- Main Navigate and Actions
	if AllowNavigate == true then
		
		if pad:up() then
			if pressed.up == false then
				pressed.up = true
				if Page == 3 then
					Select.SP = Select.SP - 1
					if Select.SP < 0 then Select.SP = 2 end
				end
				if Page == 4 then
					Select.Edit = Select.Edit - 1
					if Select.Edit < 0 then Select.Edit = 2 end
				end
			end
		else
			pressed.up = false
		end
		
		if pad:down() then 
			if pressed.down == false then
				pressed.down = true
				if Page == 3 then
					Select.SP = Select.SP + 1
					if Select.SP > 2 then Select.SP = 0 end
				end
				if Page == 4 then
					Select.Edit = Select.Edit + 1
					if Select.Edit > 2 then Select.Edit = 0 end
				end
			end
		else
			pressed.down = false
		end
		
		
		
		-- Actions
		if pad:cross() then
			
			if Page == 3 then
				Mp3me.stop()
				Mp3me.load("Sounds/select2.mp3")
				Mp3me.play()
				if Select.SP == 0 then
					Directer = 0
					TimerBool = true
				elseif Select.SP == 1 then
					Directer = 1
					TimerBool = true
				elseif Select.SP == 2 then
					Directer = 2
					TimerBool = true
				elseif Select.SP == 3 then
					Directer = 3
					TimerBool = true
				end
			end
			
			if Page == 4 then
				Mp3me.stop()
				Mp3me.load("Sounds/select2.mp3")
				Mp3me.play()
				if Select.Edit == 0 then
					Directer = 3
					TimerBool = true
				elseif Select.Edit == 1 then
					Directer = 4
					TimerBool = true
					
				--elseif Select.Edit == 2 then
					--Directer = 5
					--TimerBool = true
				end
			end
			
			if Page == 5 then
				Mp3me.stop()
				Mp3me.load("Sounds/select2.mp3")
				Mp3me.play()
				Directer = 5
				TimerBool = true
			end
			
			
		end
		
		if Page == 2 then
			if pad:cross() then
				if pressed.cross == false then
					pressed.cross = true
					
					if CrState == false then
						CrState = true
					end
				end
			else
				pressed.cross = false
			end
			
			if pad:l() then
				if pressed.l == false then
					pressed.l = true
					
					Credits = 1
				end
			else
				pressed.l = false
			end
			
			if pad:r() then
				if pressed.r == false then
					pressed.r = true
					
					Credits = 2
				end
			else
				pressed.r = false
			end
			
			if CrState == true then
				screen:blit(0,0,CreditPanel)
				if Credits == 1 then
					screen:blit(70,29,Sign1)
				elseif Credits == 2 then
					screen:blit(70,29,Sign2)
				end
				if pad:circle() then
					CrState = false
				end
			end
		end
		
		
	end

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