
--------------------------------------------------------------------------------
class 'CWallpapersPage'

local wallpaperList = {
	'DarkTales-Dock',
	'DarkTales-House',
	'DarkTales-Mill',
	'DarkTales-Owl',
	'DarkTales-Stairs'
};

function CWallpapersPage:__init(screen)
	assert(screen);

	local zOffset = 50;
	local wallpaperPosition = {1024.0/2.0 + 65.0, 768.0/2.0};
	local PREVIEW_SCALE = 800.0/1024.0;

	local auxLoadWallpaper = function(screen, parent, name, visible)
		local image = screen:CreateAnimation('data/Texture/GUI/Extras/Wallpapers/'..name..'-1024x768.bmp');
		image:SetXY(wallpaperPosition[1], wallpaperPosition[2]);
		image:SetZ(1.0);
		image:AttachTo(parent);
		image:SetScale(PREVIEW_SCALE);
		image:SetVisible(visible);

		return image;
	end

	self.pageRoot = screen:CreateAnimation('data/Texture/Scenes/Common/area.png');
	self.pageRoot:SetColor('0x10FFFFFF');
	self.pageRoot:SetPosZ(zOffset);

	do
		local buttonsRoot = screen:CreateAnimation('data/Texture/Scenes/Common/area.png');
		buttonsRoot:AttachTo(self.pageRoot);
		buttonsRoot:SetColor('0xFFFFFFFF');
	self.buttonsRoot = buttonsRoot;

		local buttonsPosition = {600, 370};
		local buttonsGap = 160; 
		local buttonsYShift = 110;
		local previewPos = {0.0, -86.0};
	   	do
			local panel = screen:CreateAnimation('data/Texture/GUI/Extras/PageConceptArt/RMPanel.png');
			panel:AttachTo(self.buttonsRoot);
			panel:SetZ(1.0);
			panel:SetXY(buttonsPosition[1] - buttonsGap, buttonsPosition[2]);

			do
				local auxAssignSlideShowEffector = function(slide, duration, slideCount, slideIndex)
					local slideDuration = duration / slideCount * 1.98;
					local middleKey =	{(slideIndex - 1)/slideCount * duration, 		1.0};
					local startKey = 	{middleKey[1] - slideDuration/2.0, 				0.0};
					local endKey = 		{middleKey[1] + slideDuration/2.0,				0.0};

				    local eff = CreateAnimationEffector();
			        eff:AttachTo(slide);
					if (slideIndex == 1) then
				        eff:SetKey(_TRACK_O, 0.0,							middleKey[2]);
				        eff:SetKey(_TRACK_O, endKey[1],						endKey[2]);
				        eff:SetKey(_TRACK_O, duration - slideDuration/2.0,	startKey[2]);
				        eff:SetKey(_TRACK_O, duration,						middleKey[2]);
					else						
				        eff:SetKey(_TRACK_O, 0.0,			startKey[2]);
				        eff:SetKey(_TRACK_O, startKey[1],	startKey[2]);
				        eff:SetKey(_TRACK_O, middleKey[1],	middleKey[2]);
				        eff:SetKey(_TRACK_O, endKey[1],		endKey[2]);
				        eff:SetKey(_TRACK_O, duration,		endKey[2]);
					end
					eff:SetMode(_ANIM_LOOP);
			        eff:Play();
				end

				local slideshowPeriod = 5.0;
				local slideCount = 5;
				for i = 1,slideCount do
					local slide = screen:CreateAnimation('data/Texture/GUI/Extras/PageWallpapers/wallpaperPreview'..tostring(i)..'.png');
					slide:AttachTo(panel);
					slide:SetZ(1.5);
					slide:SetXY(previewPos[1], previewPos[2]);
					auxAssignSlideShowEffector(slide, slideshowPeriod, slideCount, i);
				end
			end

	   	    local button = MakeButton(buttonsPosition[1] - buttonsGap, buttonsPosition[2] + buttonsYShift, 282, 166);
			button:DisableIfHidden(true);
	   	    button:GetCaption():SetFont(BUTTONS_FONT);
	   		button:GetCaption():SetScale(0.8);
	   		button:SetZ(2.0);
    	
	   		button:AttachTo(self.buttonsRoot);
			button:SetAction('OnShowWallpapers');
	   		button:SetSpriteUp('WallpapersButton');
	   		button:SetSpriteOver('WallpapersButtonOver');
	   		button:SetSpriteDown('WallpapersButtonDown');
	   		button:SetActionSound('Menu_button_click');
	   	end
	   	do
			local panel = screen:CreateAnimation('data/Texture/GUI/Extras/PageConceptArt/BCPanel.png');
			panel:AttachTo(self.buttonsRoot);
			panel:SetZ(1.0);
			panel:SetXY(buttonsPosition[1] + buttonsGap, buttonsPosition[2]);

			do
				local preview = screen:CreateAnimation('data/Texture/GUI/Extras/PageWallpapers/screensaverPreview.png');
				do
					local anim = preview:GetAnimation();
					anim:Stop();
					anim:SetTextureRect(0,0, 267,194);
					anim:SetFrames(20);
					anim:SetFrame(0);
					anim:SetSpeed(12.0);
					anim:SetMode(HGEANIM_FWD + HGEANIM_LOOP);
					anim:Play();
				end
				preview:AttachTo(panel);
				preview:SetZ(1.5);
				preview:SetHotSpot(267.0/2.0, 194.0/2.0);
				preview:SetXY(previewPos[1], previewPos[2]);
			end

	   	    local button = MakeButton(buttonsPosition[1] + buttonsGap, buttonsPosition[2] + buttonsYShift, 282, 166);
			button:DisableIfHidden(true);
	   	    button:GetCaption():SetFont(BUTTONS_FONT);
	   		button:GetCaption():SetScale(0.8);
	   		button:SetZ(2.0);
    	
	   		button:AttachTo(self.buttonsRoot);
	   		button:SetAction('OnSetScreensaver');
	   		button:SetSpriteUp('ScreensaverButton');
	   		button:SetSpriteOver('ScreensaverButtonOver');
	   		button:SetSpriteDown('ScreensaverButtonDown');
	   		button:SetActionSound('Menu_button_click');
	   	end
	end

	do
		self.wallpapersRoot = screen:CreateAnimation('data/Texture/Scenes/Common/area.png');
		self.wallpapersRoot:AttachTo(self.pageRoot);
		self.wallpapersRoot:SetColor('0x10FFFFFF');
		self.wallpapersRoot:SetPosZ(zOffset);
		self.wallpapersRoot:Hide();

		self.wallpapers = {};

		self.currentWallpaperIndex = 1;
		for wIndex,wallpaperFileName in pairs(wallpaperList) do
			table.insert(self.wallpapers, auxLoadWallpaper(screen, self.wallpapersRoot, wallpaperFileName, wIndex == self.currentWallpaperIndex));
	    end
	end
    
	local eventMap = {};
	eventMap.Prev = 'OnPrevWallpaper';
	eventMap.Next = 'OnNextWallpaper';
	eventMap.Set  = 'OnSetWallpaper';
	CreateNavigaionPanel(screen, self, self.wallpapersRoot, 235,725, eventMap);

wallpapersPage = self;
end

function CWallpapersPage:Show()
	self.pageRoot:Show();
	self:ShowButtons();
	self:HideWallpapers();
end
function CWallpapersPage:Hide()
	self.pageRoot:Hide();
	self:HideButtons();
	self:HideWallpapers();
end

function CWallpapersPage:ShowButtons()
	assert(self.buttonsRoot);
	self.buttonsRoot:Show();
	self:HideNavigationPanel();
end
function CWallpapersPage:HideButtons()
	assert(self.buttonsRoot);
	self.buttonsRoot:Hide();
	self:ShowNavigationPanel();
end

function CWallpapersPage:ShowNavigationPanel()
	self.navigationPanel:Show();
end
function CWallpapersPage:HideNavigationPanel()
	self.navigationPanel:Hide();
end

function CWallpapersPage:ShowWallpapers()
	assert(self.wallpapersRoot);
	self.wallpapersRoot:Show();
end
function CWallpapersPage:HideWallpapers()
	assert(self.wallpapersRoot);
	self.wallpapersRoot:Hide();
end

function CWallpapersPage:SetActiveWallpaper(index)
	assert(self.wallpapers ~= nil and 
		   index <= table.getn(self.wallpapers));

	self.currentWallpaperIndex = index;
	for wIndex,wallpaper in pairs(self.wallpapers) do
		wallpaper:SetVisible(wIndex == index and true or false);
	end
end

-- Event handlers ------------------------------------------------------------
function OnShowWallpapers()
	assert(wallpapersPage);
	wallpapersPage:HideButtons();
	wallpapersPage:ShowWallpapers();
end

function OnSetScreensaver()
	assert(wallpapersPage);

	local screenZOffset = 300;
    local confirmationScreen = BuildYesNoScreen('EXTRACONTENT_SCREENSAVER_APPLY_TEXT',
                                          		'OnScreensaverSettingComfirmed',
                                          		'PopCurrentPopUpScreen');
	confirmationScreen:SetLayer(POPUP_WINDOWS_LAYER + screenZOffset);
	confirmationScreen.textBox:SetBorderSize(550.0 / confirmationScreen.textBox:GetScale(), 0);
	confirmationScreen.textBox:SetY(confirmationScreen.textBox:GetY() - 60.0);

    PopUpScreensMgr:Push(confirmationScreen, false);
end
function OnScreensaverSettingComfirmed()
	assert(wallpapersPage);

    SetSystemScreenSaver('data/Texture/GUI/Extras/Dark Tales Edgar Allen Poe Murders of Rue Morgue Screensaver.scr');
	PopCurrentPopUpScreen();
end

function OnPrevWallpaper()
	assert(wallpapersPage);

	local index = wallpapersPage.currentWallpaperIndex;
	index = (index > 1 and index - 1 or table.getn(wallpapersPage.wallpapers));
	wallpapersPage:SetActiveWallpaper(index);
end

function OnNextWallpaper()
	assert(wallpapersPage);

	local index = wallpapersPage.currentWallpaperIndex;
	index = (index < table.getn(wallpapersPage.wallpapers) and index + 1 or 1);
	wallpapersPage:SetActiveWallpaper(index);
end

function OnSetWallpaper()
	local screenZOffset = 300;
    local confirmationScreen = BuildYesNoScreen('EXTRACONTENT_SET_WALLPAPER_CONFIRMATION',
                                          		'OnWallpaperSettingComfirmed',
                                          		'PopCurrentPopUpScreen');
	confirmationScreen:SetLayer(POPUP_WINDOWS_LAYER + screenZOffset);
	confirmationScreen.textBox:SetBorderSize(550.0 / confirmationScreen.textBox:GetScale(), 0);
	confirmationScreen.textBox:SetY(confirmationScreen.textBox:GetY() - 60.0);

    PopUpScreensMgr:Push(confirmationScreen, false);
end

function OnWallpaperSettingComfirmed()
	assert(wallpapersPage);

	local index = wallpapersPage.currentWallpaperIndex;
	SetSystemWallpaper('data/Texture/GUI/Extras/Wallpapers/'..wallpaperList[index]);

	PopCurrentPopUpScreen();
end