class 'NotesScreenBase' (BaseScreen)

function NotesScreenBase:__init(name)
	BaseScreen.__init(self, name)
		
	self:setStateNames()

	self.notes              = {}
	self.pages              = nil
	
	self:restorePages()
	self:setupButtons()
	
	self.timer:add(	function()
						local fade_in_on_load = self:getEntityNamed('fade_in_on_load')
						fade_in_on_load.onClickBegin:add(bind(self.onCloseButtonClick, self))
						fade_in_on_load:setRespondable(true)
					end, 0.6)
	
	local activePageId = self:getActivePageId()

	if activePageId == nil and #self.pages > 0 then
		activePageId = self.pages[#self.pages]
	end

	self:showPage(activePageId)
end

-------------------------------------------------------------------------------
function NotesScreenBase:__finalize()
end

-------------------------------------------------------------------------------
function NotesScreenBase:setStateNames()
	self.pageSlotState 		= 'PageSlot'
	self.activePageState	= 'NotebookActivePage'
end

-------------------------------------------------------------------------------
function NotesScreenBase:setupButtons()
	self.buttons = {}
	self.buttons.button_prev = self:getEntityNamed('bttn_prev')
	self.buttons.button_next = self:getEntityNamed('bttn_next')
	self.buttons.button_close = self:getEntityNamed('bttn_close')
	
	self.buttons.button_close.onPress:add(bind(self.onCloseButtonClick, self))
	self.buttons.button_prev.onPress:add(bind(self.onPrevButtonClick, self))
	self.buttons.button_next.onPress:add(bind(self.onNextButtonClick, self))
	
	-- add sfx-sound on click
	self.buttons.button_prev:setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
	self.buttons.button_next:setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
	self.buttons.button_close:setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )

	self:setButtonsEnabled()
end

-------------------------------------------------------------------------------
function NotesScreenBase:onPrevButtonClick()
	local id   = self:getActivePageId()
	local slot = self:getPageSlot(id)
	
	if slot and slot > 1 then
		local newId = self.pages[slot - 1]
		
		self:hidePage(id)
		self:showPage(newId)
		self:setButtonsEnabled()
	end

	playCommonSound('DiaryTurnOver')
end

-------------------------------------------------------------------------------
function NotesScreenBase:onNextButtonClick()
	local id   = self:getActivePageId()
	local slot = self:getPageSlot(id)
	local size = #self.pages
		
	if slot and slot < size then
		local newId = self.pages[slot + 1]
		
		self:hidePage(id)
		self:showPage(newId)
		self:setButtonsEnabled()
	end

	playCommonSound('DiaryTurnOver')
end

-------------------------------------------------------------------------------
function NotesScreenBase:onCloseButtonClick()
	self:getEntityNamed('fade_in_on_load'):setRespondable(false)
	self:onScreenDestroy()
end

-------------------------------------------------------------------------------
function NotesScreenBase:onScreenDestroy()
	PopUpScreensMgr:pop()
	if HasGameState('NotebookActive', '') then
		RemoveGameState('NotebookActive', '')
	end
	if current_scene then
		current_scene:getPanel():getControlsPanel():onNotebookClosed()
	end
end

-------------------------------------------------------------------------------
function NotesScreenBase:restorePages()
	self.pages = {}
	
	if HasGameState(self.pageSlotState) then
		local size = GetGameState(self.pageSlotState):getSize()
		
		for i = 0, size - 1 do
			local id = GetGameState(self.pageSlotState):get(i)
			table.insert(self.pages, id)
		end
	end
end

-------------------------------------------------------------------------------
function NotesScreenBase:getActivePageId()
	return (HasGameState(self.activePageState)) and GetGameState(self.activePageState):get(0) or nil
end

-------------------------------------------------------------------------------
function NotesScreenBase:getPageSlot(id)
	for slot, pageId in ipairs(self.pages) do
	 	if id == pageId then
	 		return slot
		end
	end

	if #self.pages >= 1 then
		return #self.pages
	end
	
	return nil
end

-------------------------------------------------------------------------------
function NotesScreenBase:setButtonsEnabled()
	local id   = self:getActivePageId()
	local slot = self:getPageSlot(id)
	local size = #self.pages
	
	local prevValue  = (slot ~= nil) and (slot > 1)
	local nextValue = (slot ~= nil) and (slot < size)
	
	self.buttons.button_prev:setRespondable(prevValue)
	self.buttons.button_prev:setVisible(prevValue)
	
	self.buttons.button_next:setRespondable(nextValue)
	self.buttons.button_next:setVisible(nextValue)
end

-------------------------------------------------------------------------------
function NotesScreenBase:showPage(id, time)
	if id == nil then
		return
	end
	
	self:addNotePage(id)
	
	local note   = self.notes[id]
	note:show()
	
	SetGameState(self.activePageState, id)
end

--------------------------------------------------------------------------------
function NotesScreenBase:hidePage(id)
	local note   = self.notes[id]
	note:hide()
end
--------------------------------------------------------------------------------

function NotesScreenBase:isPageCreated(id)
	return (self.notes[id] ~= nil)
end
--------------------------------------------------------------------------------

function NotesScreenBase:addNotePage(id)
	if not self:isPageCreated(id) then
		local filePath = NOTES_FOLDER..id..'.lua'
		if IsLevelCollectorsEdition(current_scene:getPlayField().name) then
		    filePath    =   NOTES_FOLDER_CE..id..'.lua'
		end
		assert(resourceExists(filePath))
		dofile(filePath)
		self.notes[id] = Helpers.CallFunctionByName(id, self, id)
		self.notes[id]:loadPage()
	end
end

