inventory = inventory or {}

----------------------------------------------------
-- объекты, собираемые
inventory.formed_objects = {}

-- склеиваемые объекты
inventory.linked_objects = {}


function inventory.IsFormed(_object_id)

    for i,o in pairs(inventory.formed_objects) do

        for j,obj in pairs(o) do
            if obj == _object_id then
                return i
            end
        end
    end
    return nil
end

function inventory.form_object_form_parts(_res_object, ...)
    local new_record = {}
    
    for _,v in ipairs({...}) do
        table.insert(new_record, v)
    end
    inventory.formed_objects[_res_object] = new_record

end

function inventory.NeedLink()


    local invent_list = {}

    local current_object = quest.global_level_box.GetObjects()
    while not current_object.IsEnd() do
        table.insert(invent_list, {current_object.Get().Item.GetId().c_str(), current_object.Get().InventState.GetId().c_str()} )
        current_object.Next()
    end

    for i,o in pairs(inventory.linked_objects) do
        local good_list = true
        local res_table = {}
        res_table.final_object = i
        res_table.link_objects = {}
        for j,obj in pairs(o) do
            local find = false
            if type(obj) == "table" then
                for inv_index,inv_obj in pairs(invent_list) do
                    if inv_obj[1] == obj[1] and inv_obj[2] == obj[2] then
                    find = true
                    break
                    end
                end
            else
                for inv_index,inv_obj in pairs(invent_list) do
                    if inv_obj[1] == obj then
                    find = true
                    break
                    end
                end
            end
            if find == false then   
                good_list = false
                break
            end
            local inserted_obj = obj
            if type(obj) == "table" then
                inserted_obj = obj[1]
            end
            table.insert(res_table.link_objects, inserted_obj)
        end
        if good_list == true then
            return res_table
        end
    end
    return nil
end

-- объединение объектов
function inventory.unite_objects(_res_object, ...)
local find_all_objects = true
    local new_record = {}
    
    for _,v in ipairs({...}) do
        table.insert(new_record, v)
    end
    inventory.linked_objects[_res_object] = new_record
    local qwe = inventory.linked_objects
    local a = 0
end

-------------------------
function inventory.take(_object, _do_not_assert)
    local current_object = quest.global_current_script_object 
    if _object ~= nil then
        current_object = quest.FindRootObjectById(_object)
        if not _do_not_assert then        
            quest.show_object_find_error(_object, current_object)
        end
    end
    if current_object ~= nil then
        quest.global_modify_level_flag = true 
        add_score("take_type", 1)
        commit_score()
        quest.global_level_box.InsertObject(current_object, nil)
        objects_box_lua_widget.take(current_object.GetId())
		return current_object.GetId()
    end
end

function inventory.momental_take(_object, _do_not_assert)
    local current_object = quest.global_current_script_object 
    if _object ~= nil then
        current_object = quest.FindRootObjectById(_object)
        if not _do_not_assert then        
            quest.show_object_find_error(_object, current_object)
        end
    end
    if current_object ~= nil then
        quest.global_level_box.InsertObject(current_object, nil)
        objects_box_lua_widget.take(current_object.GetId(), true)
		return current_object.GetId()
    end
end


function inventory.move_to_scene(_object_id)
    quest.global_modify_level_flag = true 
    local current_object = quest.global_level_box.GetObjects()
    local removed_object = nil
    while not current_object.IsEnd() do
        if current_object.Get().Item.GetId().c_str() == _object_id then 
            removed_object = current_object.Get()
            break
        end
        current_object.Next()
    end

    if removed_object ~= nil then
        local object = quest.FindRootObjectById(_object_id, true)
        if object then
            object.ClearObjectFlag(qe.CBaseSceneObject.ObjectFlagRemoved)
        end
        quest.global_cursor.ClearCursor(false)
        objects_box_lua_widget.remove(_object_id)
    end
end

function inventory.is_object_in_inventory(_object, _state)
    if _object then
        local current_object = quest.global_level_box.GetObjects()
        while not current_object.IsEnd() do

            local object_id = current_object.Get().Item.GetId().c_str()
            if _object == object_id then
                if _state == nil then
                    return true
                else
                    local state_id = current_object.Get().InventState.GetId().c_str()
                    if state_id == _state then
                        return true
                    end
                    return false
                end
            end
            current_object.Next()
        end
    end
    return false
end

function inventory.change_invent_state(_id)
    objects_box_lua_widget.change_state(_id)
end

function inventory.change_invent_state_by_id(_id, _state_id)
    objects_box_lua_widget.change_state_by_id(_id, _state_id)
end

function inventory.shadow_take(_object_id, _take_removed)
    local current_object = nil
    if   _object_id ~= nil then
        current_object = quest.FindRootObjectById(_object_id, _take_removed)
    end
    if current_object ~= nil then
        quest.global_level_box.InsertObject(current_object, nil)
        quest.RemoveObject(current_object.GetId().c_str())
    else
        if _take_removed then
            __assert(true, "shadow_take: Невозможно взять обект в инвентарь. Объект ".._object_id.." не найден на сцене")
        else
            __assert(true, "shadow_take: Невозможно взять обект в инвентарь. Объект ".._object_id.." не найден на сцене(возможно он уже удален)")
        end
    end

end

function inventory.remove(_object)
    quest.global_modify_level_flag = true 
    local cursor_object =  quest.global_cursor.GetCursorObject()
    local cursor_object_id = ""
    if cursor_object ~= nil then
        cursor_object_id = cursor_object.GetId().c_str()
    end

    if cursor_object_id~=nil and ( _object == nil or cursor_object_id == _object )  then
        add_score("remove_type", 1)
        --drop_incorect_click()
    end

    local removed_object = nil
    if _object == nil then
        if cursor_object_id ~= "" then
            --removed_object = quest.global_cursor.GetCursorObject()
            --local removed_object_id = removed_object.GetId().c_str()
            objects_box_lua_widget.remove(cursor_object_id)
            quest.global_cursor.ClearCursor(false)
            quest.RemoveObject(cursor_object_id)
        else
            __message("Неопределенн объект для удаления", "Ошибка")
        end
    else
        if cursor_object_id == _object then
            quest.global_cursor.ClearCursor(false)
        end
        objects_box_lua_widget.remove(_object)
        quest.RemoveObject(_object)
    end
end

function inventory.is_object_on_level(_object)
    local obj = quest.FindRootObjectById(_object)
    if obj then
        return true
    end
    return false
end
