--------------------------------------------------------------------------------
-- HIDDEN-OBJECT HELPERS -------------------------------------------------------
--------------------------------------------------------------------------------
function RegisterHOInteractives(...)
    local Interactives = { Functions = {} }
    local level = GetCurrentLevel()
    
    for i,Func in pairs({...}) do
        Interactives.Functions[Func] = true
    end
    
    Interactives.Using = function(...)
        local UsingInter   = {...}
        local Functions = table.copy(Interactives.Functions)
        
        for i,Func in ipairs(UsingInter) do
            Func(level, true)
            Functions[Func] = nil
        end
        
        for Func,v in pairs(Functions) do
            Func(level, false)
        end
    end
    
    return Interactives
end
--------------------------------------------------------------------------------

function GetTask(objectName)
    return play_field:GetLevelScreen():GetTask(objectName)
end
--------------------------------------------------------------------------------


function GetActiveTaskStateName(task)
	return GetCurrentLevel().name.."("..task:GetTextSingular()..")"
end
--------------------------------------------------------------------------------

function IsActiveTask(task)
    return HasGameState(TASK_IS_ACTIVE, GetActiveTaskStateName(task))
end
--------------------------------------------------------------------------------

function SetActiveTaskState(task)
    if not IsActiveTask(task) then
        AddGameState(TASK_IS_ACTIVE, GetActiveTaskStateName(task))
    end
end
--------------------------------------------------------------------------------

function RemoveActiveTaskStates()
    local tasks = play_field:GetLevelScreen():GetTasks()
    
    for task in tasks do
        local value = GetActiveTaskStateName(task)
        RemoveGameState(TASK_IS_ACTIVE, value)
    end
end
--------------------------------------------------------------------------------



function GetTaskObjectFoundState(objectName)
    return GetCurrentLevel().name..'('..objectName..')'
end
--------------------------------------------------------------------------------

function IsTaskObjectFound(objectName)
    return HasGameState(TASK_OBJECT_FOUND, GetTaskObjectFoundState(objectName))    
end
--------------------------------------------------------------------------------

function SetTaskObjectFound(objectName)
    local stateValue = GetTaskObjectFoundState(objectName)
    
    if not HasGameState(TASK_OBJECT_FOUND, stateValue) then
        AddGameState(TASK_OBJECT_FOUND, stateValue)
    end
end
--------------------------------------------------------------------------------

function RemoveTaskObjectsFound()
    local createdTaskObjects = GetCurrentLevel():GetAdditionalLogic().createdTaskObjects
    
    for i, objectName in ipairs(createdTaskObjects) do
	    local stateValue = GetTaskObjectFoundState(objectName)
	    
		if HasGameState(TASK_OBJECT_FOUND, stateValue) then
		    RemoveGameState(TASK_OBJECT_FOUND, stateValue)
        end
    end
end
--------------------------------------------------------------------------------



function SetTaskActionObject(objName, bAction)
    local level = GetCurrentLevel()
    local panel = level:GetPanelLuaObject()
    
    if play_field:GetLevelScreen():IsTaskObject(objName) and not IsTaskObjectFound(objName) then
        local task = GetTask(objName)
        
        task:SetActionObject(objName, bAction)
        level.logic:GetIndicator():UpdateCaptionColor(task)	
    end
end
--------------------------------------------------------------------------------

function DefineToolFindOrder(...)
    GetCurrentLevel():GetAdditionalLogic():DefineToolFindOrder(...)
end
--------------------------------------------------------------------------------

function RegisterTasks(keyPrefix, randomObjectsName, actionTasksObjectsName, ...)
    local level = GetCurrentLevel()
    
    assert(level:GetPanelLuaObject():GetMode() == EPanelMode.PM_HIDDENOBJECT)
    
    local logic = level:GetAdditionalLogic()
    logic:RegisterTasks(keyPrefix, randomObjectsName, actionTasksObjectsName, ...)
end

--------------------------------------------------------------------------------

function RegisterTaskHP(...)
    local level = GetCurrentLevel()
    local logic = level:GetAdditionalLogic()
    
    assert(logic)
    logic:RegisterTask(...)
end
--------------------------------------------------------------------------------

function RegisterTaskObjectsShadow(objectName, shadowName)
    if IsTaskObjectFound(objectName) then
        Object(shadowName):SetOpacity(0)
        return
    end
    
    local level = GetCurrentLevel()
    
    if not level.task_objects_shadows then
        level.task_objects_shadows = {}
    end
    level.task_objects_shadows[objectName] = shadowName
    
    play_field:IgnoreOnTestPoint(shadowName)
end
--------------------------------------------------------------------------------

function RegisterItemFoundAction(objName, action)
    assert(action)
    
    local level = GetCurrentLevel()
    level.item_found_actions[objName] = action
    
    if IsTaskObjectFound(objName) then
        local event   = {}
        event.objName = objName
        event.save    = true
        
        action(event)
    end
end
--------------------------------------------------------------------------------