dofile('data/Scripts/Animations/MainMenuAnimation.lua');		--// for PlayMainMenuAnimation() function

function OnCreateContent()
--	mainMenuScreen = screen;

	CreateGraphics();
    CreateMenuButtons();

	PlayMainMenuAnimation(mainMenuScreen);
	PlaySound('MMNS01', true);
	nextAnimationStartDelay = 0.0;
end

function OnFreeContent()
--	mainMenuScreen = nil;
--	mainMenuButtons = nil;
--	nextAnimationStartDelay = nil;
end

function OnUpdate(dt)
	if (mainMenuScreen ~= nil and mainMenuAnimationData ~= nil) then
		nextAnimationStartDelay = (nextAnimationStartDelay - dt > 0.0 and nextAnimationStartDelay - dt or 0.0); 
		if (nextAnimationStartDelay == 0) then
			local animations = {};

			for compositionName, composition in pairs(mainMenuAnimationData) do
				if (composition:IsCyclic() == false) then
					table.insert(animations, composition:GetName());
				end
			end

			local animName = animations[math.random(1, table.getn(animations))];
			if (animName ~= nil) then
				local composition = mainMenuAnimationData[animName];

	            if (composition ~= nil and composition:IsPaused() == true) then
					composition:SetVisible(true);
				    composition:Play();
				end
			end
			nextAnimationStartDelay = math.random(3,8);
		end
	end
	if (GetGameManager():GetProfileManager():HasProfiles() == true and mainMenuScreenButtons[6]:IsVisible() == false) then
		mainMenuScreenButtons[6]:SetVisible(true);
	elseif (GetGameManager():GetProfileManager():HasProfiles() == false and mainMenuScreenButtons[6]:IsVisible() == true) then
		mainMenuScreenButtons[6]:SetVisible(false);
	end
end

function OnUpdateCurrentProfileName()
	if (GetGameManager():GetProfileManager():HasProfiles() == true) then
		local currentProfileName = GetGameManager():GetProfileManager():GetCurrentProfile():GetName();
		assert(welcomeText);
	    welcomeText:SetText(currentProfileName);
	end
end

function OnHighlightButton(buttonName)
	assert(buttonEffects ~= nil);
	if (buttonEffects[buttonName] ~= nil) then
	    local effects = buttonEffects[buttonName];
		for i,effect in pairs(effects) do
		    if (effect:GetAnimation():IsPlaying() == false) then
			    effect:Play();
			end
		end
		StopSound('MMNS02');
		PlaySound('MMNS02');
	end

-- DeBUG !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (_DEBUG) then
	bgtestValue = not bgtestValue;
	local shader = nil;

    if (bgtestValue) then
	    if (math.random(1,2) == 1) then
    		shader = CreateNoiseShader(math.random(1.0, 10.0));
    	else
		    shader = CreateWaveShader(8.0,  10.0 * math.random(),
								      16.0, 10.0 * math.random());
	    end
	end
	bgtest:EnableSubdividedMesh(bgtestValue, shader);
end

end

function OnMenuDestroy()
	StopSound('MMNS01');
	FreeMainMenuAnimationData();
end
--// auxiliary functions - - - - - - - - - - - - - - - - - - - - - - - - - - -
function auxCreateHighlightAnimation(parent)
	assert(parent ~= nil);

	local effect = CreateAnimationEntity();
	effect:SetName('MenuButton'..parent:GetName()..'_HighlightEffect');
	effect:SetAnimCopy('ButtonHighlightAnimation');
	effect:AttachTo(parent);
	effect:SetZ(1.0);
	return effect;
end

function auxCreateButtonHighlightAnimation(parent, scale)
	local res = {auxCreateHighlightAnimation(parent), auxCreateHighlightAnimation(parent)};
	assert(res[2] ~= nil);
	res[1]:SetScale(scale);
	res[2]:SetScale(scale);
	res[2]:GetAnimation():SetFlip(true, true, false);
	return res;
end
--// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function CreateMenuButtons()
	do
	    welcomeText = CreateTextEntity('GUICaptionFont', '');
		welcomeText:SetName('ProfileNameText');
	    welcomeText:AttachTo(mainMenuScreen);
		welcomeText:SetXY(355.0, 690.0);
		welcomeText:SetZ(101.0);
		welcomeText:SetTextAlignment(HGETEXT_CENTER);
		welcomeText:SetVisible(true);

		OnUpdateCurrentProfileName();
	end

	mainMenuScreenButtons = {};
	buttonEffects = {};
	do
	    local button = mainMenuScreen:AddButton(114,313,101, 'MenuButtonPlayUp','MenuButtonPlayOver','MenuButtonPlayDown', 'Play');
		button:AttachTo(mainMenuScreen);
		table.insert(mainMenuScreenButtons, button);
		buttonEffects[button:GetName()] = auxCreateButtonHighlightAnimation(button, 1.0);
	end
	do
	    local button = mainMenuScreen:AddButton(137,422,101,'MenuButtonOptionsUp','MenuButtonOptionsOver','MenuButtonOptionsDown', 'Options');
		button:AttachTo(mainMenuScreen);
		table.insert(mainMenuScreenButtons, button);
		buttonEffects[button:GetName()] = auxCreateButtonHighlightAnimation(button, 1.0);
	end
	do
	    local button = mainMenuScreen:AddButton(385,298,101,'MenuButtonHelpUp','MenuButtonHelpOver','MenuButtonHelpDown', 'Help');
		button:AttachTo(mainMenuScreen);
		table.insert(mainMenuScreenButtons, button);
		buttonEffects[button:GetName()] = auxCreateButtonHighlightAnimation(button, 0.8);
	end
	do
	    local button = mainMenuScreen:AddButton(375,393,101,'MenuButtonQuitUp','MenuButtonQuitOver','MenuButtonQuitDown', 'Exit');
		button:AttachTo(mainMenuScreen);
		table.insert(mainMenuScreenButtons, button);
		buttonEffects[button:GetName()] = auxCreateButtonHighlightAnimation(button, 1.0);
	end
	do
	    local button = mainMenuScreen:AddButton(266,453,101,'MenuButtonCreditsUp','MenuButtonCreditsOver','MenuButtonCreditsDown', 'Credits');
		button:AttachTo(mainMenuScreen);
		table.insert(mainMenuScreenButtons, button);
		buttonEffects[button:GetName()] = auxCreateButtonHighlightAnimation(button, 0.7);
	end
	do
	    local button = mainMenuScreen:AddButton(357,727,101,'MenuButtonChangeProfileUp','MenuButtonChangeProfileOver','MenuButtonChangeProfileDown', 'SelectPlayer');
		button:AttachTo(mainMenuScreen);
		table.insert(mainMenuScreenButtons, button);
	end
end

function CreateGraphics()
-- Debug !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if (_DEBUG) then
	bgtest = CreateSprite(); 
	bgtest:SetName('InfoScreenWindow');
	bgtest:AttachTo(mainMenuScreen);
	bgtest:SetSprite('InfoWindowTexture');
	bgtest:SetXY(SCREEN_WIDTH/2.0, SCREEN_HEIGHT/2.0);
	bgtest:SetZ(100.0);
	bgtest:SetVisible(true);
	bgtestValue = false;
end

end
