class 'BlackBar'

local APPEAR_TIME = 0.3

function BlackBar:__init()
    self.textFields = {}
    
    self.visible = false
    self.tf      = nil
end
-------------------------------------------------------------------------------

function BlackBar:Show(textKey, ignoreGetString)
    if self.visible then
        self:Hide()
    end
    
    local panel = GetCurrentLevel():GetPanelLuaObject()
    local tf    = self:GetTextField()
    local text  = (ignoreGetString) and textKey or GetString(textKey)
    
    tf:SetText(text)
    tf:ChangeOpacity(tf:GetOpacity(), 1, APPEAR_TIME)
    
    AddUniqueGameState(BLACK_BAR_SHOWN, textKey)
    panel:SetBlackBarVisible(true, APPEAR_TIME)
    
    self.textFields[tf] = true
    self.visible        = true
    self.tf             = tf
end
-------------------------------------------------------------------------------

function BlackBar:Hide(instantly)
    if self.visible then
        local level    = GetCurrentLevel()
        local panel    = level:GetPanelLuaObject()
        local hideTime = (instantly) and 0 or APPEAR_TIME
        
        local tf = self.tf
        tf:DestroyChildren()
        
        if hideTime == 0 then
            tf:SetOpacity(0)
            self.textFields[tf] = false
        else
            tf:ChangeOpacity(tf:GetOpacity(), 0, hideTime)
            
            level.timer:Add(function()
                                self.textFields[tf] = false
                            end, hideTime)
        end
        
        panel:SetBlackBarVisible(false, hideTime)
        self.visible = false
        self.tf      = nil
    end
end
-------------------------------------------------------------------------------

function BlackBar:IsVisible()
    return self.visible
end
-------------------------------------------------------------------------------

function BlackBar:GetTextField()
    local tf = nil
    
    for textField, visible in pairs(self.textFields) do
        if not visible then
            tf = textField
        end
    end
    
    if not tf then
        local target = GetCurrentLevel():GetPanelLuaObject():GetObject('black_bar')
        tf = CreateTextEntity(BLACK_BAR, '')
        
        tf:AttachTo        (target)
        tf:SetScrScaleX    (1)
        tf:SetScrScaleY    (1)
        tf:SetScrX         (512)
        tf:SetScrY         (target:GetScrY())
        
        tf:SetTextAlignment(HGETEXT_CENTER + HGETEXT_TOP)
        tf:SetBorderSize   (980, 90)
        tf:SetSpacing      (0.8)
        tf:SetTracking     (-2)
        tf:SetScale        (0.8)
        tf:SetZ            (450)
        --tf:SetColor        ('0xFF9FE7E6')
        tf:SetOpacity      (0)
        
        self.textFields[tf] = false
    end
    
    return tf
end
-------------------------------------------------------------------------------



-------------------------------------------------------------------------------
-- AUXILIARY FUNCTIONS
-------------------------------------------------------------------------------

function ShowBlackBar(textKey, once)
    if not (once and IsBlackBarShown(textKey)) then
        GetCurrentLevel().blackBar:Show(textKey)
    end
end
-------------------------------------------------------------------------------

function HideBlackBar(instantly)
    GetCurrentLevel().blackBar:Hide(instantly)
end
-------------------------------------------------------------------------------

function IsBlackBarShown(textKey)
    return HasGameState(BLACK_BAR_SHOWN, textKey)
end
-------------------------------------------------------------------------------