dofile('data/lua/Helpers.lua');
dofile('data/lua/screens/Extras/NavigationPanel.lua');
dofile('data/lua/screens/Extras/ResourceSavingConfirmationScreen.lua');
dofile('data/lua/screens/Extras/MusicPage.lua');
dofile('data/lua/screens/Extras/WallpapersPage.lua');
dofile('data/lua/screens/Extras/MoviesPage.lua');
dofile('data/lua/screens/Extras/ConceptArtPage.lua');
dofile('data/lua/screens/Extras/OriginalStoryPage.lua');

--------------------------------------------------------------------------------
local GUI_CONTROLS_Z_OFFSET = 50;

--------------------------------------------------------------------------------
function ShowExtraContentScreen()
	if (EXTRAS_DUILD or EXTRAS_BUILD) then
		if (IsExtrasFinished()) then
--		if (not IsExtrasFinished()) then	--// (!) DEBUG
		    --// stops main menu animation
		    assert(mainMenu);
		    StopMusic('menu_music', true);
		    mainMenu:Stop();
		    
		    CreateExtraContentScreen();
		else
			local screen = BuildOKScreen('EXTRACONTENT_BONUS_SECTION_HASNT_COMPLETE', 'PopCurrentPopUpScreen');
			screen.textBox:SetBorderSize(550.0 / screen.textBox:GetScale(), 0);
	        screen:SetLayer(POPUP_WINDOWS_LAYER);
		    PopUpScreensMgr:Push(screen);
		end
	else
		local screen = BuildOKScreen('SE_PRESENT_EXTRAS_TEXT', 'PopCurrentPopUpScreen');
		screen.textBox:SetBorderSize(550.0 / screen.textBox:GetScale(), 0);
        screen:SetLayer(POPUP_WINDOWS_LAYER);

		-- link to the collector's edition web page
		do
			local pos = {1024.0/2.0, 768.0/2.0 + 40.0};
			local size = {540, 120};
		    ceWebPage = MakeButton(pos[1], pos[2], size[1], size[2]);
--			ceWebPage:SetName('ceWebPage');
			ceWebPage:SetRect(pos[1] - size[1] / 2,
							  pos[2] - size[2] / 2 + 40.0,
			                  size[1],
			                  size[2]);

		    ceWebPage:GetCaption():SetFont(INGAME_MENU_FONT);
		    ceWebPage:GetCaption():SetColor('0xFF0080FF');
		    ceWebPage:GetCaption():SetHighlightColor('0xFFF2A5A5');
		    ceWebPage:SetZ(100);
			ceWebPage:SetScale(0.6);
    
		    ceWebPage:AttachTo(screen);
		    ceWebPage:SetActionSound("Menu_button_click");
		    ceWebPage:SetTitle(GetString('EXTRACONTENT_CE_WEB_PAGE_TEXT'));
		    ceWebPage:SetAction('OnGoToCollectorEditionWebPage');
		end
	    PopUpScreensMgr:Push(screen);
	end
end

function OnGoToCollectorEditionWebPage()
	APP:OpenUrlFromFile('data/Links/CollectorsEditionWebPageURL.txt');
end


function CloseExtraContentScreen()
	assert(extraContentScreen);

    --// resume main menu animation
	extraContentScreen = nil;
	StopAmbientMusic();

    assert(mainMenu);
	mainMenu:Go();
	MainMenuComposition:Play();
	RestartMusic('menu_music');

	PopCurrentPopUpScreen();
end

--------------------------------------------------------------------------------
function CreateExtraContentScreen()
	extraContentScreen = nil;

    -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    function auxBuildExtrasScreen(message, okCallback)
        local screen = CreateScreen();
        screen:SetName('ExtrasScreen ' .. message);
        screen:SetLayer(POPUP_WINDOWS_LAYER);
        
        local bg = screen:CreateAnimation('data/Texture/GUI/Extras/screenBack.jpg');
        bg:SetHotSpot(512, 384);
        bg:SetPosXY(512, 384);
        bg:SetPosZ(0);

        local frame = screen:CreateAnimation('data/Texture/GUI/Extras/activeAreaFrame.png');
        frame:SetHotSpot(836.0/2.0, 630.0/2.0);
        frame:SetPosXY(1024.0/2.0 + 65.0, 384);
        frame:SetPosZ(300);

    	do    
    	    local CloseButton = MakeButton(920, 725, 100, 40);
			CloseButton:DisableIfHidden(true);
    	    CloseButton:GetCaption():SetFont(BUTTONS_FONT);
    		CloseButton:GetCaption():SetScale(0.8);
    		CloseButton:SetZ(GUI_CONTROLS_Z_OFFSET);
    	
    		CloseButton:AttachTo(screen);
    		CloseButton:SetAction(okCallback);
    		CloseButton:SetSpriteUp('OKButton');
    		CloseButton:SetSpriteOver('OKButtonOver');
    		CloseButton:SetSpriteDown('OKButtonDown');
    		CloseButton:SetActionSound('Menu_button_click');
    	end
    	do
    		local caption = CreateTextEntity(INGAME_MENU_FONT, '');
    		caption:SetXY(512 + 80, 40);
    		caption:SetZ(GUI_CONTROLS_Z_OFFSET);
    		caption:SetScale(1.0);
    		caption:AttachTo(screen);
    		caption:SetTextAlignment(HGETEXT_CENTER + HGETEXT_MIDDLE);
    		screen.caption = caption;
    	end
        
        return screen;    
    end
    -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	local auxCreateTabButton = function(parent, x,y, name, pageConstructor)
		assert(extraContentScreen ~= nil);

	    local button = MakeButton(x, y, 100, 40);
		button:DisableIfHidden(true);
	    button:GetCaption():SetFont(BUTTONS_FONT);
		button:GetCaption():SetScale(0.8);
		button:SetZ(GUI_CONTROLS_Z_OFFSET);
	
		button:AttachTo(parent);
		button:SetAction('OnShowPage'..name);
		button:SetSpriteUp('Button'..name);
		button:SetSpriteOver('Button'..name..'Over');
		button:SetSpriteDown('Button'..name..'Down');
		button:SetActionSound('Menu_button_click');

		if (extraContentScreen.tabs == nil) then
			extraContentScreen.tabs = {};
		end
		extraContentScreen.tabs[name] = {};
		extraContentScreen.tabs[name].button = button;
		if (pageConstructor ~= nil) then
			extraContentScreen.tabs[name].page = pageConstructor(extraContentScreen);
		end
	end


    extraContentScreen = auxBuildExtrasScreen('MORE_INFO', 'CloseExtraContentScreen');
    local screen = extraContentScreen;
	local tabButtonsOffset = {80, 130};
	local tabButtonHeight = 122;
	local tabButtonsSpacing = tabButtonHeight + 3;
	local buttonIndex = 0;


	local button =
    auxCreateTabButton(screen, tabButtonsOffset[1], tabButtonsOffset[2] + buttonIndex * tabButtonsSpacing, 'Music',			CMusicPage);
	buttonIndex = buttonIndex + 1;
    auxCreateTabButton(screen, tabButtonsOffset[1], tabButtonsOffset[2] + buttonIndex * tabButtonsSpacing, 'Wallpapers',	CWallpapersPage);
	buttonIndex = buttonIndex + 1;
    auxCreateTabButton(screen, tabButtonsOffset[1], tabButtonsOffset[2] + buttonIndex * tabButtonsSpacing, 'OriginalStory',	COriginalStoryPage);
	buttonIndex = buttonIndex + 1;
	auxCreateTabButton(screen, tabButtonsOffset[1], tabButtonsOffset[2] + buttonIndex * tabButtonsSpacing, 'Movies',		CMoviesPage);
	buttonIndex = buttonIndex + 1;
    auxCreateTabButton(screen, tabButtonsOffset[1], tabButtonsOffset[2] + buttonIndex * tabButtonsSpacing, 'ConceptArt',	CConceptArtPage);

	OnShowPageMusic();

    PopUpScreensMgr:Push(extraContentScreen);
end

--------------------------------------------------------------------------------
-- Tab button handlers
function auxShowPage(name)
	assert(extraContentScreen ~=nil and extraContentScreen.tabs ~= nil);
	for tabName,tab in pairs(extraContentScreen.tabs) do
		if (tab ~=nil and tab.page ~= nil) then
			local duration = 0.35;
			if (tabName == name) then
				tab.page:Show();
			else
				tab.page:Hide();
			end
		end
	end

end

function OnShowPageMusic()
	assert(extraContentScreen and extraContentScreen.caption);
	extraContentScreen.caption:SetText(GetString('EXTRACONTENT_SCR_TAB_MUSIC_CAPTION'));
	auxShowPage('Music');
end
function OnShowPageWallpapers()
	assert(extraContentScreen and extraContentScreen.caption);
	extraContentScreen.caption:SetText(GetString('EXTRACONTENT_SCR_TAB_WALLPAPERS_CAPTION'));
	auxShowPage('Wallpapers');
end
function OnShowPageOriginalStory()
	assert(extraContentScreen and extraContentScreen.caption);
	extraContentScreen.caption:SetText(GetString('EXTRACONTENT_SCR_TAB_ORIGSTORY_CAPTION'));
	auxShowPage('OriginalStory');
end
function OnShowPageMovies()
	assert(extraContentScreen and extraContentScreen.caption);
	extraContentScreen.caption:SetText(GetString('EXTRACONTENT_SCR_TAB_MOVIES_CAPTION'));
	auxShowPage('Movies');
end
function OnShowPageConceptArt()
	assert(extraContentScreen and extraContentScreen.caption);
	extraContentScreen.caption:SetText(GetString('EXTRACONTENT_SCR_TAB_CONCEPTART_CAPTION'));
	auxShowPage('ConceptArt');
end
