dofile('data/Scripts/Minigames/minigameWinAnimation.lua');	--// auxPlayDoneAnimation() function
------------------------------------------------------------------------------

function OnCreateContent()
-- miniGameScreen

    minigameData = {};
	auxCreateMinigameScreenUI();

	minigameData.prevProgress = -1.0;
	minigameData.finalMovieShown = false;
	minigameCompleteTime = nil;
	auxUpdateProgressBar(0.0);
end

function OnFreeContent()
end

--// auxiliary function - - - - - - - - - - - - - - - - - - - - - - - - - - - 

function auxCreateMinigameScreenUI()
	local layer = 6.0;

	local backbar = CreateSprite(); 
	backbar:AttachTo(miniGameScreen);
	backbar:SetSprite('MinigamePanelBarTexture');
	backbar:SetXY((backbar:GetTextureRect().x2 - backbar:GetTextureRect().x1) / 2.0,
				  SCREEN_HEIGHT - (backbar:GetTextureRect().y2 - backbar:GetTextureRect().y1) / 2.0);
	backbar:SetZ(200.0);
	backbar:SetLayer(layer);
	backbar:SetVisible(true);

	local progressBar = CreateSprite();
	progressBar:AttachTo(backbar);
	progressBar:SetSprite('ProgressBarInnerMatterTexture');
	progressBar:SetXY(-48.0, 6.0);
	progressBar:SetZ(1.0);
	progressBar:SetLayer(layer);
	progressBar:SetVisible(true);
	minigameData.progressBar = progressBar; --// (!) global variable
	
	local inactiveSkip = CreateSprite(); 
	inactiveSkip:AttachTo(backbar);
	inactiveSkip:SetSprite('ButtonSkipInactiveTexture');
	inactiveSkip:SetXY(-48.0, 6.0);
	inactiveSkip:SetZ(1.5);
	inactiveSkip:SetLayer(layer);
	inactiveSkip:SetVisible(true);
	minigameData.inactiveSkip = inactiveSkip; --// (!) global variable

--	local posx = miniGameScreen:GetX();
	local skipButton = CreateButton('ButtonSkipUp','ButtonSkipOver','ButtonSkipDown', 'MSG_MINIGAME_SKIPPED', miniGameScreen);
	skipButton:AttachTo(backbar);
	skipButton:SetXY(-48.0, 6.0);
	skipButton:SetZ(2.0);
	skipButton:SetLayer(layer);
	skipButton:SetVisible(false);
	minigameData.skipButton = skipButton; --// (!) global variable

	local doneButton = CreateButton('ButtonDoneUp','ButtonDoneOver','ButtonDoneDown', 'MSG_MINIGAME_DONE', miniGameScreen);
	doneButton:AttachTo(backbar);
	doneButton:SetXY(-48.0, 6.0);
	doneButton:SetZ(2.0);
	doneButton:SetLayer(layer);
	doneButton:SetVisible(false);
	minigameData.doneButton = doneButton; --// (!) global variable

	local helpButton = CreateButton('ButtonHelpUp','ButtonHelpOver','ButtonHelpDown', 'MSG_MINIGAME_HELP', miniGameScreen);
	helpButton:AttachTo(backbar);
	helpButton:SetXY(-30.0, -67.0);
	helpButton:SetZ(2.0);
	helpButton:SetLayer(layer);
	helpButton:SetVisible(true);
	minigameData.helpButton = helpButton; --// (!) global variable

end

function auxUpdateProgressBar(progress)
	assert(minigameData.progressBar ~= nil and
           minigameData.prevProgress ~= nil);

    if (minigameData.prevProgress >= progress) then
	    if (minigameData.doneButton:IsVisible() == true) then
    		assert(miniGameState.timeElapsed ~= nil);
		    local MINIGAME_FINISH_ON_DONE_DELAY = 8.0;
   
		    if (minigameCompleteTime == nil) then
    			minigameCompleteTime = miniGameState.timeElapsed;
			end
		    if (minigameCompleteTime ~= nil and miniGameState.timeElapsed - minigameCompleteTime > MINIGAME_FINISH_ON_DONE_DELAY) then
    			minigameCompleteTime = nil;
			    miniGameScreen:FinishGame(true);
		    end
	    end
        return;
    end

    local TEXTURE_HEIGHT = 74.0;
	local texRect = minigameData.progressBar:GetTextureRect();
	texRect.y1 = TEXTURE_HEIGHT * (1.0 - progress);
	texRect.y2 = TEXTURE_HEIGHT - texRect.y1;	
	minigameData.progressBar:SetTextureRect(texRect, true);
	minigameData.progressBar:SetY(6.0 + TEXTURE_HEIGHT * (1.0 - progress));
	minigameData.prevProgress = progress;
end

function auxUpdateProgressBarOnDone(progress)
	if (minigameData.prevProgress) then
		auxUpdateProgressBar(progress);
	end
end

function auxShowButton(levelCompleted)
    assert(minigameData.skipButton ~= nil and
		   minigameData.doneButton ~= nil and 
		   minigameData.inactiveSkip ~= nil);

    minigameData.inactiveSkip:SetVisible(false);

	local button = 		 (levelCompleted == true and minigameData.doneButton or minigameData.skipButton);
	local buttonToHide = (levelCompleted == true and minigameData.skipButton or minigameData.doneButton);

	buttonToHide:SetVisible(false);
    local duration = 0.25;
   	button:SetOpacity(0.0);
	button:SetVisible(true);

    local eff = CreateAnimationEffector();
    eff:AttachTo(button);
    eff:SetKey(_TRACK_O, 0.0,      0.0);
    eff:SetKey(_TRACK_O, duration, 1.0);
    eff:Play();

	if (levelCompleted == true) then
        if (minigameData.prevProgress < 1.0) then
			local n = math.ceil(10.0*(1.0 - minigameData.prevProgress)) + 1;
		    local duration = 0.5;
		    for i = 1,n do
    			local progressValue = minigameData.prevProgress + i/n * (1.0 - minigameData.prevProgress);
			    TimerDoString('auxUpdateProgressBarOnDone('..progressValue..')', duration * i/n, miniGamePlayField:GetTimer());
		    end
		end
		if (minigameData.finalMovieShown == false) then
			minigameData.finalMovieShown = true;
			auxPlayDoneAnimation();
		end
	end
end
