class 'StrategyGuideScreenBase' (BaseScreen)

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

	self.currentPage	= nil
	self.currentIndex	= -1
	self.pagesOrder		= 10
	self.changePageTime	= 0.25

	self.map		= self:createMap()
	self.homePage	= self:createHomePage(self.map.home)

	self.pageNumberEntity = self:getEntityNamed('text_page_number')

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

function StrategyGuideScreenBase:createMap()
	local path	= isDemoMode() and 'Data/ExtraContent/Text/StrategyGuide_demo.xml'
								or 'Data/ExtraContent/Text/StrategyGuide.xml'
	
	return self:parseXML( path )
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:createHomePage(data)
	local page = StrategyGuideHomePage(self, 'sg_home_page', self.pagesOrder)

	for index, topic in ipairs(data.topics) do
		page:createTopicButton(topic.text, topic.link, Vector2f(512, 384))
	end

	if isDemoMode() then
		page:placeTopicButtonsInDemo()
	else
		page:placeTopicButtons()
	end

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

function StrategyGuideScreenBase:parseXML(path)
	local currentTopicTitle
	local currentTopicIndex = 0
	local currentPage

	local result		= {}
	result.home			= {}
	result.home.title	= ''
	result.home.topics	= {}
	result.pages		= {}
	-------------------------

	local document = XMLDocument('StrategyGuide')
	document:Load(path)
	-------------------------

	local parsePageText = function(element)
		assert(element:GetName() == 'li')

		local str = string.format('* %s\n', element:GetValue())
		currentPage.text = currentPage.text..str
	end
	-------------------------

	local parsePage = function(element)
		local name = element:GetName()

		if name == 'img' then
			local img =
			{
				['name' ] = element:GetAttribute('src').value,
				['index'] = element:GetAttribute('position').value
			}
			table.insert(currentPage.imgs, img)
		elseif name == 'ul' then
			XMLHelpers.forallChilds(element, parsePageText)
		end
	end
	-------------------------

	local parseChapter = function(element)
		local name = element:GetName()

		if name == 'h2' then
			currentTopicTitle = element:GetValue()
		elseif name == 'page' then
			currentPage		  = {}
			currentPage.imgs  = {}
			currentPage.text  = ''
			currentPage.title = currentTopicTitle
			table.insert(result.pages, currentPage)

			local topic	= result.home.topics[currentTopicIndex]
			if not topic.link then
				topic.link	= #result.pages
			end

			XMLHelpers.forallChilds(element, parsePage)
		end
	end
	-------------------------

	local parseGuide = function(element)
		assert(element:GetName() == 'chapter')
		currentTopicIndex = currentTopicIndex + 1

		XMLHelpers.forallChilds(element, parseChapter)
	end
	-------------------------

	local parseTopics = function(element)
		assert(element:GetName() == 'li')
		local topic	= {}
		topic.text	= element:GetValue()
		topic.link	= nil

		table.insert(result.home.topics, topic)
	end
	-------------------------

	local parseToc = function(element)
		local name = element:GetName()

		if name == 'h2' then
			result.home.title = element:GetValue()
		elseif name == 'ul' then
			XMLHelpers.forallChilds(element, parseTopics)
		end
	end
	-------------------------

	local parseRoot = function(element)
		local name = element:GetName()

		if name == 'toc' then
			XMLHelpers.forallChilds(element, parseToc)
		elseif name == 'guide' then
			XMLHelpers.forallChilds(element, parseGuide)
		end
	end
	-------------------------

	XMLHelpers.forallChilds(document:GetRootElement(), parseRoot)

	return result
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:createButtonsTable()
	local buttons =
	{
		['close'] =
		{
			entity	= self:getEntityNamed('bttn_close'),
			press	= bind(self.onCloseButtonPress, self)
		},
		['right'] =
		{
			entity	= self:getEntityNamed('bttn_right'),
			press	= bind(self.onRightButtonPress, self)
		},
		['left'] =
		{
			entity	= self:getEntityNamed('bttn_left'),
			press	= bind(self.onLeftButtonPress, self)
		},
		['home'] =
		{
			entity	= self:getEntityNamed('bttn_home'),
			press	= bind(self.showHomePage, self)
		},
	}
	return buttons
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:setupButtons()
	local buttons = self:createButtonsTable() or {}

	for id, info in pairs(buttons) do
		info.entity:setSFXTrackOnPress( info.sfx or getCommonSoundEntity('ButtonClick') )

		if info.press then
			info.entity.onPress:add( info.press )
		end

		if info.bHide then
			info.entity:hide()
		end
	end
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:restorePage()
	local index = 0
	local state = ProfilesManager:getCurrentProfile().gameState

	if not state:hasState(SG_HOME_STATE, EGameStateTag.GENERAL) then
		index = self:getSavedPageIndex() or 0
	end

	self:showPage(index)
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:getSavedPageIndex()
	local index
	local state = ProfilesManager:getCurrentProfile().gameState

	if state:hasState(SG_PAGE_INDEX_STATE, EGameStateTag.GENERAL) then
		index = tonumber(state:getState(SG_PAGE_INDEX_STATE, EGameStateTag.GENERAL):get(0))

		if index > #self.map.pages then
			index = nil
		end
	end

	return index
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:savePageIndex()
	local state = ProfilesManager:getCurrentProfile().gameState	

	if self:isHomePage() then
		state:setState(SG_HOME_STATE, 'true', EGameStateTag.GENERAL)
	else
		state:setState(SG_PAGE_INDEX_STATE, tostring(self.currentIndex), EGameStateTag.GENERAL)

		if state:hasState(SG_HOME_STATE, EGameStateTag.GENERAL) then
			state:removeState(SG_HOME_STATE, EGameStateTag.GENERAL)
		end
	end
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:showPage(index)
	if self.currentPage then
		self.currentPage:hide(self.changePageTime)
	end

	self.currentIndex = index

	if self.currentIndex == 0 then
		self.currentPage = self.homePage

		self.pageNumberEntity:setVisible(false)
	else
		local data = self.map.pages[index]
		self.currentPage = StrategyGuidePage(self, 'sg_page_'..index, self.pagesOrder)
		self.currentPage:setInstructionsText(data.text)
		self.currentPage:setImgs(data.imgs)

		local text = string.format('%d/%d', self.currentIndex, #self.map.pages)
		self.pageNumberEntity:setText( text )
		self.pageNumberEntity:setVisible(true)
	end

	self.currentPage:show(self.changePageTime)
	self:savePageIndex()
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:showHomePage()
	self:showPage(0)
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:isHomePage()
	return self.currentIndex == 0
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:showPrevPage()
	local index = (self.currentIndex > 1) and (self.currentIndex - 1)
										   or #self.map.pages
	self:showPage(index)
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:showNextPage()
	local index = (self.currentIndex < #self.map.pages) and (self.currentIndex + 1)
														 or 1
	self:showPage(index)
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:onRightButtonPress(target, screenFocus)
	if self:isHomePage() then
		local index = self:getSavedPageIndex() or 1
		self:showPage(index)
	else
		self:showNextPage()
	end
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:onLeftButtonPress(target, screenFocus)
	if self:isHomePage() then
		local index = self:getSavedPageIndex() or #self.map.pages
		self:showPage(index)
	else
		self:showPrevPage()
	end
end
-------------------------------------------------------------------------------

function StrategyGuideScreenBase:onCloseButtonPress(target, screenFocus)
	PopUpScreensMgr:pop()
end
-------------------------------------------------------------------------------