class 'HelpScreenBase' (BaseScreen)

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

    self.text_help = {}
    self:setupButtons()
    self:createHelpText()
	
	self.text_help[1]:setOpacity(1)
	self.index_current = 1
	self.index_count   = #self.text_help
	self:updateButtonClick()
end

function HelpScreenBase:setupButtons()
    self:getMainEntity():getEntityNamed('bttn_ok').onPress:add(bind(self.onScreenDestroy, self))
    self:getMainEntity():getEntityNamed('bttn_left').onPress:add(bind(self.onLeftButtonClick, self))
    self:getMainEntity():getEntityNamed('bttn_right').onPress:add(bind(self.onRightButtonClick, self))

    -- add sfx-sound in click
    do
        self:getMainEntity():getEntityNamed('bttn_ok'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
        self:getMainEntity():getEntityNamed('bttn_left'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
        self:getMainEntity():getEntityNamed('bttn_right'):setSFXTrackOnPress( getCommonSoundEntity('ButtonClick') )
    end
end

function HelpScreenBase:createHelpText()
    for i = 1, 100 do
        local text = 'HELP_TEXT_'..(i)
        if not StringManager:hasString(text) then
           break 
        end
        table.insert( self.text_help, self:createText(text) )
    end
end

function HelpScreenBase:__finalize()
end

function HelpScreenBase:onUpdate(dt)
end

function HelpScreenBase:createText(text)
    local textEntity = CreateTextEntity()
    textEntity:attachTo( self:getMainEntity() )
    textEntity:setOrder(20)
    textEntity:setXY(512, 334)
    textEntity:setBounds(370, 0)
    textEntity:setText( StringManager:getString(text) )
    textEntity:setFont('Data/Fonts/Options.fnt')
    textEntity:setOpacity(0)
    
    return textEntity
end

function HelpScreenBase:updateButtonClick()
    if self.index_current == 1 then
        self:getMainEntity():getEntityNamed('bttn_left'):hide()
    else
        self:getMainEntity():getEntityNamed('bttn_left'):show()
    end
    
    if self.index_current == self.index_count then
        self:getMainEntity():getEntityNamed('bttn_right'):hide()
    else
        self:getMainEntity():getEntityNamed('bttn_right'):show()
    end
end

function HelpScreenBase:onLeftButtonClick()
    if self.index_current > 1 then
        local text_fade_in = self.text_help[self.index_current]
        local text_fade_out = self.text_help[self.index_current - 1]
    
        self:changeText(text_fade_in, text_fade_out)
        self.index_current = self.index_current - 1
    end
    
    self:updateButtonClick()
end

function HelpScreenBase:onRightButtonClick()
    if self.index_current < self.index_count then
        local text_fade_in = self.text_help[self.index_current]
        local text_fade_out = self.text_help[self.index_current + 1]
    
        self:changeText(text_fade_in, text_fade_out)
        self.index_current = self.index_current + 1
    end
    
    self:updateButtonClick()
end

function HelpScreenBase:changeText(text_fade_in, text_fade_out)
    local time = ZOOM_ZONE_APPEARING_TIME
    text_fade_in:ChangeOpacity(1, 0, time)
    text_fade_out:ChangeOpacity(0, 1, time)
    self:getMainEntity():getEntityNamed('bttn_left'):setActive(false)
    self:getMainEntity():getEntityNamed('bttn_right'):setActive(false)
       
    self.timer:add(function()
                        self:getMainEntity():getEntityNamed('bttn_left'):setActive(true)
                        self:getMainEntity():getEntityNamed('bttn_right'):setActive(true)                
                    end, time)
end

function HelpScreenBase:onScreenDestroy()
     PopUpScreensMgr:pop()
end