class 'HiddenPictureIndicator'

function HiddenPictureIndicator:__init()
    self.SLOTS_COUNT = 5
    self.SLOT_SIZE   = 64
    self.TASKS_X     = -120
    self.TASKS_Y     = 20
    self.TASKS_DX    = 120
    
    self.HINT_TEXT_DX = 20
    self.HINT_TEXT_DY = 0
    
    self.COLOR_SHADOW   = '0xFF000000'
    self.COLOR_DEFAULT  = '0xFFFFFFFF'
    self.COLOR_DRAGGING = '0xFF555555'
    
    self.anchor   = self:_createAnchor()
    self.hintText = nil
    self.objects  = {}
    
    self.slots  = {}
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:GetAnchor()
    return self.anchor
end
-------------------------------------------------------------------------------


function HiddenPictureIndicator:CreateIndicator(taskName, templateObject)
    local panel = GetCurrentLevel():GetPanel()
    local templateAnim = templateObject:GetAnimation()
    
    local info  = {}
    info.anchor = nil
    info.icon   = nil
    info.back   = nil
    
    -- create indicator anchor
    do
        local anim, object
        
        anim = CreateAnimationByTextureName('data/Texture/Common/area.png')
        anim:SetColor('0x00FFFFFF')
        
        object = panel:CreateObject('anchor_'..templateObject:GetName(),anim,0,0,0,0,1,1)
        object:AttachTo(self.anchor)
        object:SetOpacity(1)
        
        panel:IgnoreOnTestPoint(object:GetName())
        
        info.anchor = object
    end
    
    -- create indicator back
    do
        local anim, object
        local scaleX, scaleY
        
        anim = CreateAnimationByTextureName('data/Texture/Common/area.png')
        anim:SetColor('0x01FFFFFF')
        
        scaleX = self.SLOT_SIZE / anim:GetWidth()
        scaleY = self.SLOT_SIZE / anim:GetHeight()
        
        object = panel:CreateObject('back_'..templateObject:GetName(),anim,0,0,1,0,scaleX,scaleY)
        object:AttachTo(info.anchor)
        
        info.back = object
    end
    
    -- create indicator icon
    do
        local anim, object
        local scaleX, scaleY
        
        anim = CreateAnimationByTextureName('data/Texture/Common/area.png')
        
        scaleX = self.SLOT_SIZE / templateObject:GetAnimation():GetWidth()
        scaleY = self.SLOT_SIZE / templateObject:GetAnimation():GetHeight()
        
        object = panel:CreateObject('icon_'..templateObject:GetName(),anim,0,0,2,0,1,1)
        object:SetScale(math.min(scaleX, scaleY))
        object:SetAnimation(templateAnim)
        object:AttachTo(info.anchor)
        
        panel:IgnoreOnTestPoint(object:GetName())
        
        info.icon = object
    end
    
    self.objects[taskName] = info
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:SetIndicatorColor(taskName, color)
    local object = self.objects[taskName].icon
    object:GetAnimation():SetColor(color)
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:SetIndicatorIgnoreTestPoint(taskName, ignore)
    local panel = GetCurrentLevel():GetPanel()
    local info  = self.objects[taskName]
    local objectName = info.back:GetName()
    
    if ignore then
        panel:IgnoreOnTestPoint(objectName)
    else
        panel:DoNotIgnoreOnTestPoint(objectName)
    end
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:ShowIndicator(taskName)
    local slot = self:_getSavedSlot(taskName)
    
    if not slot then
        slot = self:_getFreeSlot()
        self:_saveSlot(slot, taskName)
    end
    
    self.slots[slot] = taskName
    
    local anchor = self.objects[taskName].anchor
    local icon   = self.objects[taskName].icon
    
    anchor:SetX(self.TASKS_X + self.TASKS_DX * (slot-1))
    icon:SetOpacity(1)
    
    anchor:Show()
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:HideIndicator(taskName)
    local slot = self:_getSavedSlot(taskName)
    
    self.slots[slot] = nil
    self:_saveSlot(slot, nil)
    
    local anchor = self.objects[taskName].anchor
    local icon   = self.objects[taskName].icon
    
    anchor:SetX(self.TASKS_X)
    icon:SetOpacity(0)
    
    anchor:Hide()
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:GetTaskPos(task)
    local taskName = task:GetTextSingular()
    local object   = self.objects[taskName].icon
    
    return Vector(object:GetScrX(), object:GetScrY())
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:GetIconScale(taskName)
    return self.objects[taskName].icon:GetScale()
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:GetTaskName(objectName)
    for taskName, info in pairs(self.objects) do
        if info.back:GetName() == objectName then
            return taskName
        end
    end
    
    return nil
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:FreeSlotsExist()
    for i=1, self.SLOTS_COUNT do
        if self.slots[i] == nil then
            return true
        end
    end
    
    return false
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:ClearSlotStates()
    for i=1, self.SLOTS_COUNT do
        local state = self:_getTaskSlotState(i)
        
        if HasGameState(state) then
            RemoveGameState(state)
        end
    end
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:UpdateHintText(key)
    local tf = self:_getHintText()
    
    if key then
        local mouse = GetMousePos()
        
        tf:SetText(GetString(key))
        tf:SetScrX(mouse.x + self.HINT_TEXT_DX)
        tf:SetScrY(mouse.y + self.HINT_TEXT_DY)
        tf:SetVisible(true)
    else
        tf:SetVisible(false)
    end
end
-------------------------------------------------------------------------------



--------------------------------------------------------------------------------
-- PRIVATE METHODS -------------------------------------------------------------
--------------------------------------------------------------------------------

function HiddenPictureIndicator:_createAnchor()
    local panel = GetCurrentLevel():GetPanelLuaObject()
    local obj   = CreateAutoRegisterEntity()

    
    obj:AttachTo(panel:GetObject('panel_ho'))
    obj:SetZ(1)
    obj:SetXY(self.TASKS_X, self.TASKS_Y)
    
    return obj
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:_getSavedSlot(taskName)
    for i=1, self.SLOTS_COUNT do
        local state = self:_getTaskSlotState(i)
        
        if HasGameState(state) then
            if taskName == GetGameState(state):Get(0) then
                return i
            end
        end
    end
    
    return nil
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:_getFreeSlot()
    for i=1, self.SLOTS_COUNT do
        local state = self:_getTaskSlotState(i)
        
        if not HasGameState(state) then
            return i
        end
    end
    
    return nil
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:_saveSlot(slot, taskName)
    self.slots[slot] = taskName
    
    local state = self:_getTaskSlotState(slot)
    
    if taskName then
        SetGameState(state, taskName)
    else
        RemoveGameState(state)
    end
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:_getTaskSlotState(slot)
    return 'TASK_SLOT_'..tostring(slot)..'_'..string.upper(GetCurrentLevel().name)
end
-------------------------------------------------------------------------------

function HiddenPictureIndicator:_getHintText()
    if not self.hintText then
        self.hintText = CreateTextEntity(INVENTORY_FONT, 'EMPTY')
        self.hintText:SetScale(1)
        self.hintText:AttachTo(self:GetAnchor())
        self.hintText:SetTextAlignment(0)
        self.hintText:SetZ(10)
        
        self.hintText:SetVisible(false)
    end
    
    return self.hintText
end
-------------------------------------------------------------------------------