class 'MoreGamesScreenBase' (BaseScreen)

function MoreGamesScreenBase:__init(name)
	BaseScreen.__init(self, name)

	self.parseGamesPath = 'Data/Textures/Screens/MoreGames/Icons/'
	self.iconsOnPage	= 8
	self.pagesCount		= -1
	self.changePageTime	= 0.3

	self.pageButtonSettings =
	{
		deltaX		= 40,
		width		= 30,
		centerPos	= Vector2f(512, 600),
		color		= Colorf(0.95, 0.93, 0.74,  1),
		overColor	= Colorf(0.97, 0.68, 0.054, 1),
		scale		= 1,
		scaleSelect	= 1.2,
		scaleTime	= 0.2,
		textHeight	= 40,
		textFont	= 'Data/Fonts/Options.fnt'
	}

	self.games			= {}
	self.pageButtons	= {}
	self.prevPageButton = nil
	self.nextPageButton = nil

	self.page		= nil
	self.pageIndex	= 0

	self:setupButtons()
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:show(...)
	self:parseGames()
	self:initPageButtons()
	self:showPage(1)

	BaseScreen.show(self, ...)
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:setupButtons()
	local buttonClose = self:getEntityNamed('bttn_close')
	buttonClose.onPress:add( bind(self.onCloseButtonPress, self) )
	buttonClose:setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:parseGames()
	local files		= getResourceFolderFiles( self.parseGamesPath )
	local priority	= self:getIconsPriority()

	for i=0, files:size()-1 do
		local name, index = string.match(files:get(i), "(Game(%d+)[%w_]*)")

		if name then
			local data		= {}
			data.name		= name
			data.index		= tonumber(index)
			data.priority	= priority[name] or 1
			table.insert(self.games, data)
		end
	end

	local comp = function(data1, data2)
		return	 data1.priority > data2.priority or
				(data1.priority == data2.priority and data1.index > data2.index)
	end
	table.sort(self.games, comp)
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:getIconsPriority()
	local priorityTable =
	{
		--[ 'Game55_GrimFacade_SE'] = 2,
		--[ 'Game54_GrimFacade'] = 2,
		--[ 'Game33_GFMOV_SE'] = 2,
		--[ 'Game32_GFMOV_CE'] = 2
	}
	return priorityTable
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:initPageButtons()
	self.pagesCount = math.ceil(#self.games / self.iconsOnPage)

	local pagesRangeX = (self.pagesCount-1) * self.pageButtonSettings.deltaX
	local pos		  = Vector2f(self.pageButtonSettings.centerPos.x - pagesRangeX/2,
								 self.pageButtonSettings.centerPos.y)

	for i=1, self.pagesCount do
		local button = self:createPageButton(i)
		button:setPosition(pos)

		pos.x = pos.x + self.pageButtonSettings.deltaX
		table.insert(self.pageButtons, button)
	end

	self:createAdditionPageButtons()
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:createAdditionPageButtons()
	-- button prev
	do
		local firstButtonPos = self:getPageButton( 1 ):getPosition()

		local action = bind(self.showPrevPage, self)
		local pos	 = Vector2f(firstButtonPos.x - self.pageButtonSettings.deltaX,
								firstButtonPos.y)

		self.prevPageButton = self:createAdditionPageButton('<', pos, action)
	end

	-- button next
	do
		local lastButtonPos = self:getPageButton( self.pagesCount ):getPosition()

		local action = bind(self.showNextPage, self)
		local pos	 = Vector2f(lastButtonPos.x + self.pageButtonSettings.deltaX,
								lastButtonPos.y)

		self.nextPageButton = self:createAdditionPageButton('>', pos, action)
	end
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:updateAdditionPageButtons()
	if self.prevPageButton then
		if self.pageIndex == 1 then
			self.prevPageButton:hide()
		else
			self.prevPageButton:show()
		end
	end

	if self.nextPageButton then
		if self.pageIndex == self.pagesCount then
			self.nextPageButton:hide()
		else
			self.nextPageButton:show()
		end
	end
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:createPageButton(index)
	local button = CreateButtonEntity('')
	local title	 = button:getOverlayTextEntity()

	title:setText		( tostring(index) )
	title:setColor		( self.pageButtonSettings.color )
	title:setFont		( (self.custom_font or self.pageButtonSettings.textFont) ) 
	title:setHeight		( self.pageButtonSettings.textHeight )
	title:setOrder		( 1 )
	title:setHotSpotType( ETextHotSpotTypeBottom )
	title:setXY			( 0, title:getHeight()/2 )

	button.properties = {}
	button.properties.pageIndex = index

	button:attachTo				( self:getMainEntity() )
	button:setName				( 'button_page_'..tostring(index) )  
	button:setOrder				( 10 )
	button:setActiveRectSize	( self.pageButtonSettings.width, title:getHeight() )
	button:setSFXTrackOnPress	( getCommonSoundEntity('ButtonClick') )
	
	button.onPress:add			( bind(self.showPage, self, index) )
	button.onFocusIn:add		( bind(self.onPageButtonFocusIn,	self) )
	button.onFocusOut:add		( bind(self.onPageButtonFocusOut,	self) )

	return button
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:createAdditionPageButton(text, pos, action)
	local button = CreateButtonEntity('')
	local title	 = button:getOverlayTextEntity()

	title:setText	( text )
	title:setColor	( self.pageButtonSettings.color )
	title:setFont	( (self.custom_font or self.pageButtonSettings.textFont) ) 
	title:setHeight	( self.pageButtonSettings.textHeight )
	title:setOrder	( 1 )

	button.properties = {}
	button.properties.pageIndex = nil

	button:attachTo				( self:getMainEntity() )
	button:setName				( 'button_page_'..text )  
	button:setOrder				( 10 )
	button:setActiveRectSize	( self.pageButtonSettings.width, title:getHeight() )
	button:setSFXTrackOnPress	( getCommonSoundEntity('ButtonClick') )
	button:setPosition			( pos )
	
	button.onPress:add			( action )
	button.onFocusIn:add		( bind(self.onPageButtonFocusIn,	self) )
	button.onFocusOut:add		( bind(self.onPageButtonFocusOut,	self) )

	return button
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:getPageButton(index)
	return self.pageButtons[index]
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:showPage(index)
	assert(index > 0 and index <= self.pagesCount)

	if self.page then
		self.page:hide( self.changePageTime )

		local pageButton = self:getPageButton( self.pageIndex )
		pageButton:getOverlayTextEntity():setColor( self.pageButtonSettings.color )
		pageButton:setRespondable(true)

		local title = pageButton:getOverlayTextEntity()
		title:destroyEffectors()
		title:changeScale(	title:getScale().y,
							self.pageButtonSettings.scale,
							self.pageButtonSettings.scaleTime )
	end

	self.pageIndex = index
	self.page = self:createPage(index)
	self.page:show( self.changePageTime )

	local pageButton = self:getPageButton( self.pageIndex )
	pageButton:getOverlayTextEntity():setColor( self.pageButtonSettings.overColor )
	pageButton:setRespondable(false)
	
	local title = pageButton:getOverlayTextEntity()
	title:destroyEffectors()
	title:changeScale(	title:getScale().y,
						self.pageButtonSettings.scaleSelect,
						self.pageButtonSettings.scaleTime )

	self:updateAdditionPageButtons()
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:showPrevPage()
	local newIndex = math.max(1, self.pageIndex - 1)
	self:showPage(newIndex)
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:showNextPage()
	local newIndex = math.min(self.pagesCount, self.pageIndex + 1)
	self:showPage(newIndex)
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:createPage(pageIndex)
	local page = MoreGamesPage(self, 'moreGamesPage'..tostring(pageIndex), 1)

	local gameIndex	   = self.iconsOnPage * (pageIndex - 1) + 1
	local endGameIndex = math.min(gameIndex + self.iconsOnPage - 1, #self.games)

	while gameIndex <= endGameIndex do
		local game = self.games[gameIndex]
		page:addGame( game.name, gameIndex )
		gameIndex = gameIndex + 1
	end

	return page
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:onPageButtonFocusIn(button)
	if button.properties.pageIndex ~= self.pageIndex then
		local title = button:getOverlayTextEntity()
		title:setColor( self.pageButtonSettings.overColor )
	end
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:onPageButtonFocusOut(button)
	if button.properties.pageIndex ~= self.pageIndex then
		local title = button:getOverlayTextEntity()
		title:setColor( self.pageButtonSettings.color )
	end
end
-------------------------------------------------------------------------------

function MoreGamesScreenBase:onCloseButtonPress(button)
	PopUpScreensMgr:pop()
end
-------------------------------------------------------------------------------