
--------------------------------------------------------------------------------
class 'CConceptArtPage'
							--// folder, count, scale
local conceptArtListRMInfo = {'data/Texture/GUI/Extras/PageConceptArt/RM/', 22,	800.0 / 1024.0};
local conceptArtListBCInfo = {'data/Texture/GUI/Extras/PageConceptArt/BC/', 12,	1.0};

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

	local zOffset = 50;
	local imagePosition = {1024.0/2.0 + 65.0, 768.0/2.0};

	local auxCreateArtGroup = function(parent, listInfo)
		local groupRoot = screen:CreateAnimation('data/Texture/Scenes/Common/area.png');
		groupRoot:AttachTo(parent);
		groupRoot:SetColor('0xFFFFFFFF');

		groupRoot.currentImage = 1;
		groupRoot.images = {};

		assert(listInfo and table.getn(listInfo) == 3);
		for i = 1,listInfo[2] do
			local image = screen:CreateAnimation(listInfo[1]..tostring(i)..'.jpg');
			image:SetXY(imagePosition[1], imagePosition[2]);
			image:SetZ(zOffset);
			image:AttachTo(groupRoot);
			image:SetScale(listInfo[3]);
			image:SetOpacity(0);
			image:SetVisible(false);

			table.insert(groupRoot.images, image);
		end

		return groupRoot;
	end

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

	self.artGroups = {};
	self.activeGroup = nil;
	do
		table.insert(self.artGroups, auxCreateArtGroup(screen, conceptArtListRMInfo));
		table.insert(self.artGroups, auxCreateArtGroup(screen, conceptArtListBCInfo));
		do
			local bcBack = screen:CreateAnimation(conceptArtListBCInfo[1]..'Background.png');
			bcBack:AttachTo(self.artGroups[2]);
			bcBack:SetPosXY(1024.0/2.0 + 65.0, 768.0/2.0);
			bcBack:SetZ(40.0);
		end		
	end

	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;
	   	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]);

	   	    local button = MakeButton(buttonsPosition[1] - buttonsGap, buttonsPosition[2] + buttonsYShift, 282, 166);
			button:DisableIfHidden(true);

			button:UseRectAndTextureTestpoint(true);
			local buttonRectX, buttonRectY = buttonsPosition[1] - buttonsGap, buttonsPosition[2];
			local buttonRectWidth, buttonRectHeight = 320, 410;
			button:SetRect(buttonRectX - buttonRectWidth/ 2, buttonRectY - buttonRectHeight / 2, buttonRectWidth, buttonRectHeight);

	   	    button:GetCaption():SetFont(BUTTONS_FONT);
	   		button:GetCaption():SetScale(0.8);
	   		button:SetZ(2.0);
    	
	   		button:AttachTo(self.buttonsRoot);
			button:SetAction('OnShowRMConceptArtGallery');
	   		button:SetSpriteUp('ConceptRMButton');
	   		button:SetSpriteOver('ConceptRMButtonOver');
	   		button:SetSpriteDown('ConceptRMButtonDown');
	   		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]);

	   	    local button = MakeButton(buttonsPosition[1] + buttonsGap, buttonsPosition[2] + buttonsYShift, 282, 166);
			button:DisableIfHidden(true);

			button:UseRectAndTextureTestpoint(true);
			local buttonRectX, buttonRectY = buttonsPosition[1] + buttonsGap, buttonsPosition[2];
			local buttonRectWidth, buttonRectHeight = 320, 410;
			button:SetRect(buttonRectX - buttonRectWidth/ 2, buttonRectY - buttonRectHeight / 2, buttonRectWidth, buttonRectHeight);

	   	    button:GetCaption():SetFont(BUTTONS_FONT);
	   		button:GetCaption():SetScale(0.8);
	   		button:SetZ(2.0);
    	
	   		button:AttachTo(self.buttonsRoot);
	   		button:SetAction('OnShowBCConceptArtGallery');
	   		button:SetSpriteUp('ConceptBCButton');
	   		button:SetSpriteOver('ConceptBCButtonOver');
	   		button:SetSpriteDown('ConceptBCButtonDown');
	   		button:SetActionSound('Menu_button_click');
	   	end
	end

	local eventMap = {};
	eventMap.Prev = 'OnPrevImage';
	eventMap.Next = 'OnNextImage';
	eventMap.Save = 'OnSaveImage';
	CreateNavigaionPanel(screen, self, self.pageRoot, 235,725, eventMap);

	self.navigationPanel:Hide();

conceptArtPage = self;
end

function CConceptArtPage:Show()
	self.pageRoot:Show();
	self:ShowButtons();
	self:ShowGalleryGroup(nil);
end
function CConceptArtPage:Hide()
	self.pageRoot:Hide();
	self:HideButtons();
	self:ShowGalleryGroup(nil);
end

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

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


function auxShowImageInGroup(group, imageIndexToShow)
	assert(group.currentImage and group.images and table.getn(group.images) >= imageIndexToShow);
	local auxMakeSpriteInvisible = function(sprite)
		assert(sprite);
		sprite:SetVisible(false);
	end
	local auxChangeOpacity = function(object, startOpacity, endOpacity, duration)
	    local eff = CreateAnimationEffector();
        eff:AttachTo(object);
        eff:SetKey(_TRACK_O, 0.0,       startOpacity);
        eff:SetKey(_TRACK_O, duration,  endOpacity);
        eff:Play();

		if (endOpacity == 0.0) then
			TimerDo(GM:GetTimer(), duration, auxMakeSpriteInvisible, object);
		else
			object:SetVisible(true);
		end
	end

	for _,image in pairs(group.images) do
		image:SetOpacity(0.0);
	end
	if (group.currentImage ~= imageIndexToShow) then
	    auxChangeOpacity(group.images[group.currentImage], 1.0, 0.0, 0.5);
	end
	group.currentImage = imageIndexToShow;
    auxChangeOpacity(group.images[imageIndexToShow], 0.0, 1.0, 0.5);
end

function CConceptArtPage:ShowGalleryGroup(groupIndex)
	assert(self.artGroups);

    if (groupIndex) then
	    assert(self.artGroups[groupIndex]);
	    
	    self:HideButtons();
	    self.activeGroup = self.artGroups[groupIndex];
	    auxShowImageInGroup(self.activeGroup, self.activeGroup.currentImage);
	else
	    self.activeGroup = nil;
    end
	for gIndex,group in pairs(self.artGroups) do
		if (groupIndex and gIndex == groupIndex) then
			group:Show();
		else
			group:Hide();
		end
	end
end

-- Event handlers ------------------------------------------------------------
function OnShowRMConceptArtGallery()
	assert(conceptArtPage);
	conceptArtPage:ShowGalleryGroup(1);
end

function OnShowBCConceptArtGallery()
	assert(conceptArtPage);
	conceptArtPage:ShowGalleryGroup(2);
end


function OnPrevImage()
	assert(conceptArtPage and conceptArtPage.activeGroup);
	local group = conceptArtPage.activeGroup;
	local imageIndex = (group.currentImage > 1 and group.currentImage - 1 or table.getn(group.images));

	auxShowImageInGroup(group, imageIndex);
end

function OnNextImage()
	assert(wallpapersPage and conceptArtPage.activeGroup);
	local group = conceptArtPage.activeGroup;
	local imageIndex = (group.currentImage < table.getn(group.images) and group.currentImage + 1 or 1);

	auxShowImageInGroup(group, imageIndex);
end

function OnSaveImage()
	assert(wallpapersPage and conceptArtPage.activeGroup);

	local sourceImageName = (table.getn(conceptArtPage.activeGroup.images) == conceptArtListRMInfo[2] and
							 'data/Texture/GUI/Extras/PageConceptArt/RM/' or 'data/Texture/GUI/Extras/PageConceptArt/BC/')..
							tostring(conceptArtPage.activeGroup.currentImage)..'.jpg';
	local destinationImageName = 'Edgar Allan Poe - '..
                                 (table.getn(conceptArtPage.activeGroup.images) == conceptArtListRMInfo[2] and
								 'Murders in the Rue Morgue' or 'Black Cat')..'_ConceptArt_'..tostring(conceptArtPage.activeGroup.currentImage)..'.jpg';
	ShowResourceSavingConfirmationScreen('EXTRACONTENT_IMAGE_SAVE_TEXT', 
										 sourceImageName,
										 destinationImageName);
end