class 'l_FactsScreen' (BaseScreen)

function l_FactsScreen:__init(name)
    BaseScreen.__init(self, name)
    
	self.facts              = {}
	--self.notes_pages        = nil
	self.facts_pages        = nil
	
	self:restorePages()
    
    do -- buttons
		self.buttons = {}
		self.buttons.button_prev = self:getMainEntity():getEntityNamed('bttn_prev')
		self.buttons.button_next = self:getMainEntity():getEntityNamed('bttn_next')
		self.buttons.button_close = self:getMainEntity():getEntityNamed('bttn_close')
		self.buttons.button_notes = self:getMainEntity():getEntityNamed('bttn_notes')
		
		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))
		self.buttons.button_notes.onPress:add(bind(self.onNotesButtonClick, self))
		
        -- add sfx-sound on click
        self:getMainEntity():getEntityNamed('bttn_prev'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
        self:getMainEntity():getEntityNamed('bttn_next'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
        self:getMainEntity():getEntityNamed('bttn_close'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
        self:getMainEntity():getEntityNamed('bttn_notes'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )

		self:setButtonsEnabled()
    end
    
    self:getEntityNamed('fade_in_on_load').onClickBegin:add(bind(self.onCloseButtonClick, self))
    
	local activePageId, type = self:getActivePageId()
	self.active_page_type = type
	self:showPage(activePageId, type)	
end
-------------------------------------------------------------------------------
function l_FactsScreen:__finalize()
end
-------------------------------------------------------------------------------
function l_FactsScreen:onUpdate(dt)
end
-------------------------------------------------------------------------------
function l_FactsScreen:onPrevButtonClick()
    local id, type   = self:getActivePageId()
    local slot = self:getPageSlot(id)
    
    if slot and slot > 1 then
        local newId = self.facts_pages[slot - 1]
        
        self:hidePage(id)
        self:showPage(newId, type)
		self:setButtonsEnabled()
    end

    playCommonSound('DiaryTurnOver')
end
-------------------------------------------------------------------------------
function l_FactsScreen:onNextButtonClick()
    local id, type   = self:getActivePageId()
    local slot = self:getPageSlot(id)
    local size = #self.facts_pages
        
    if slot and slot < size then
        local newId = self.facts_pages[slot + 1]
        
        self:hidePage(id)
        self:showPage(newId, type)
		self:setButtonsEnabled()
    end

    playCommonSound('DiaryTurnOver')
end
--------------------------------------------------------------------------------
function l_FactsScreen:onNotesButtonClick()
    --[[self:hidePagingButtons()
	createInvestigationScreen(self.onScreenDestroy)

	self:getWindow():setActive(true)--]]
	self:onScreenDestroy()
	createNotesScreen(self.onScreenDestroy)
end
-------------------------------------------------------------------------------
function l_FactsScreen:onCloseButtonClick()
    self:onScreenDestroy()
end
-------------------------------------------------------------------------------
function l_FactsScreen:onScreenDestroy()
	PopUpScreensMgr:pop()
	if HasGameState('NotebookActive', '') then
		RemoveGameState('NotebookActive', '')
	end
	if current_scene then
		current_scene:getPanel():getControlsPanel():onNotebookClosed()
	end
end
-------------------------------------------------------------------------------
function l_FactsScreen:restorePages()
    self.facts_pages = {}
    
    if HasGameState('FactPageSlot') then
        local size = GetGameState('FactPageSlot'):getSize()
        
        for i = 0, size - 1 do
            local id = GetGameState('FactPageSlot'):get(i)
            table.insert(self.facts_pages, id)
        end
    end    
    
end
-------------------------------------------------------------------------------
function l_FactsScreen:getActivePageId()
    if HasGameState('NotebookActivePageFact') then
        return GetGameState('NotebookActivePageFact'):get(0), 'fact'
    else
        return nil
    end
end
-------------------------------------------------------------------------------
function l_FactsScreen:getPageSlot(id)
    for slot, pageId in ipairs(self.facts_pages) do
        if id == pageId then
            return slot
        end
    end
    
    return nil
end
-------------------------------------------------------------------------------
function l_FactsScreen:setButtonsEnabled()
	local id   = self:getActivePageId()
    local slot = self:getPageSlot(id)
    local size = #self.facts_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 l_FactsScreen:showPage(id, type)
    if id == nil then
        return
    end
    
    if not self:isPageCreated(id) then
		self:addFactPage(id)
    end
    
    local fact   = self.facts[id]
    fact:show()
    
    --[[if type == 'note' then
        if HasGameState('NotebookActivePageFact') then
            RemoveGameState('NotebookActivePageFact')
        end
        SetGameState('NotebookActivePageNote', id)
    else--]]if type == 'fact' then
        --[[if HasGameState('NotebookActivePageNote') then
            RemoveGameState('NotebookActivePageNote')
        end--]]
        SetGameState('NotebookActivePageFact', id)
    end
end
--------------------------------------------------------------------------------
function l_FactsScreen:hidePage(id)
    local fact   = self.facts[id]
    fact:hide()
end
--------------------------------------------------------------------------------
function l_FactsScreen:isPageCreated(id)
    return (self.facts[id] ~= nil)
end
--------------------------------------------------------------------------------
function l_FactsScreen:addFactPage(id)
    if not HasGameState('FACT_WAS_ADDED') then
        AddUniqueGameState('FACT_WAS_ADDED', 'true')
    end
    if not self:isPageCreated(id) then
		local filePath = FACTS_FOLDER..'/'..id..'.lua'
		assert(resourceExists(filePath))
		dofile(filePath)
		self.facts[id] = Helpers.CallFunctionByName(id, self, id)
		self.facts[id]:loadPage()
    end
end

--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function l_FactsScreen:CreateLoaderTable()
	local objects =
	{
		{ name = 'NotesBackground',  entityType = 'SpriteEntity',  x = 512,  y = 384,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 502, y = 340},  texturePath = 'Data/Textures/Screens/Notes/NotesBackground.png',  width = 1004,  height = 680,  },
		{ name = 'new text',  entityType = 'TextEntity',  x = 316,  y = 114,  order = 0,  color = {r = 0.0784314, g = 0.0784314, b = 0.980392, a = 0.65098},  editingLocked = false,  treeItemIsExpanded = true,  localizationKey = 'INTERESTING_FACTS',  font = 'Data/Fonts/tool_found.fnt',  height = 35,  },
		{ name = 'facts_picture',  entityType = 'SpriteEntity',  x = 181,  y = 140,  order = 0,  scale = 0.7,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Notes/facts_picture.png',  width = 119,  height = 119,  },
		{ name = 'bttn_notes',  entityType = 'ButtonEntity',  x = 222,  y = 631,  order = 5,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Notes/button_notes.png',  text = '',  activeRectX = 90,  activeRectY = 87,  },
		{ name = 'bttn_prev',  entityType = 'ButtonEntity',  x = 154,  y = 540,  order = 20,  scaleX = -1,  scaleY = 1,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Notes/right_button.png',  text = '',  activeRectX = 120,  activeRectY = 50,  },
		{ name = 'bttn_next',  entityType = 'ButtonEntity',  x = 887,  y = 540,  order = 20,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Notes/right_button.png',  text = '',  activeRectX = 120,  activeRectY = 50,  },
		{ name = 'bttn_close',  entityType = 'ButtonEntity',  x = 897,  y = 127,  order = 20,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Notes/close_button.png',  text = '',  activeRectX = 44,  activeRectY = 50,  },
	}
	return objects
end
