-- Данный файл предназначен для хранения общеиспользуемых функций встроенных скриптов

--                 ======= Блок настройки интерактивных локаций =======
function CreateInteractiveLocation(f, name, interactiveObjects)
    local objectsList = {};
    ConcatArrayAnyNesting(objectsList, interactiveObjects)
    return f.InteractiveLocation(name, objectsList)
end

function ConcatArrayAnyNesting(target, newElements)
    for i = 1, #newElements do
        local elements = newElements[i]
        if type(elements) == "table" then
            ConcatArrayAnyNesting(target, elements)
        else
            table.insert(target, elements)
        end
    end
end

function CreateSameActions(f, differences, creator, ...)
    local actions = {}

    for i = 1, #differences do
        table.insert(actions, creator(f, differences[i], ...));
    end

    return actions
end

function CreateInteractiveObjects(f, names, actions)
    local interactiveObjects = {}
    for i = 1, #names do
        table.insert(interactiveObjects, f.InteractiveObject(names[i], { actions[i] }));
    end

    return interactiveObjects;
end

function CreateSameInteractiveObject(f, names, sameAction)
    local interactiveObjects = {}
    for i = 1, #names do
        table.insert(interactiveObjects, f.InteractiveObject(names[i], { sameAction }));
    end

    return interactiveObjects;
end

function SherlockWalkAndSpeech(f, point, phrases, ...)
    local actions = {}
    local allowedCharacters, allowLocked, restrictions = GetRestrictions(f, ...)
    for i = 1, #phrases do
        table.insert(actions,
            f.InteractiveAction(
                function()
                    Walk('Sherlock', point)
                    Speech('Sherlock', { phrases[i] })
                end, restrictions, allowedCharacters, allowLocked)
        )
    end

    return actions
end

function SherlockWalkToObjectAndSpeech(f, isoViewName, phrases, ...)
    local actions = {}
    local allowedCharacters, allowLocked, restrictions = GetRestrictions(f, ...)
    for i = 1, #phrases do
        table.insert(actions,
                f.InteractiveAction(
                        function()
                            WalkToObject('Sherlock', isoViewName)
                            Speech('Sherlock', { phrases[i] })
                        end, restrictions, allowedCharacters, allowLocked)
        )
    end

    return actions
end

function SherlockSpeech(f, phrases, ...)
    return CharacterSpeech(f, 'Sherlock', phrases, ...)
end

function CharacterSpeech(f, characterName, phrases, ...)
    local actions = {}
    local allowedCharacters, allowLocked, restrictions = GetRestrictions(f, ...)
    for i = 1, #phrases do
        table.insert(actions,
                f.InteractiveAction(
                        function()
                            Speech(characterName, { phrases[i] })
                        end, restrictions, allowedCharacters, allowLocked)
        )
    end

    return actions
end

function CreateSameInteractiveObjectsByName(f, names, creator)
    local interactiveObjects = {}
    for i = 1, #names do
        table.insert(interactiveObjects, f.InteractiveObject(names[i], creator(f, names[i])));
    end

    return interactiveObjects;
end

function GetUnion(f, args)
    if args == nil or #args == 0 then
        return nil
    end

    if #args == 1 then
        local element = args[1]
        local elementString = tostring(element)
        local isUnion = string.find(elementString, "InteractiveActionRestrictionUnionData") ~= nil
        if isUnion then
            return element
        end
    end

    return f.InvestigationRestrictionUnion("And", args)
end

function GetRestrictions(f, ...)
    local args = { ... }
    local allowedCharacters = nil;
    local allowLocked = false;

    if args ~= nil and #args > 0 then
        local element = args[1];
        if type(element) == "table" then
            table.remove(args, 1);
            allowedCharacters = element;
        end
        if #args > 0 then
            local allow = args[1];
            if type(allow) == "boolean" then
                table.remove(args, 1);
                allowLocked = allow;
            end
        end
    end

    return allowedCharacters, allowLocked, GetUnion(f, args)
end

function UnionAny(f, ...)
    return Union(f, "Or", ...)
end

function UnionAll(f, ...)
    return Union(f, "And", ...)
end

function Union(f, logic, ...)
    local arguments = { ... }
    if arguments == nil or #arguments == 0 then
        return nil
    end
    local restrictions = arguments

    return f.InvestigationRestrictionUnion(logic, restrictions)
end

--                 ======= Конец блока настройки интерактивных локаций =======

-- перемещение камеры с тряской
function CameraShakeTo(entityName, durationSeconds, zoom, strength, vibrato, zoomTimeSeconds,
    zoomWithEasing)
    zoomTimeSeconds = zoomTimeSeconds or -1
    zoomWithEasing = zoomWithEasing or true
    Parallel(function()
        MoveCamera(entityName, durationSeconds, zoom, zoomTimeSeconds, zoomWithEasing)
        ShakeCamera(durationSeconds, strength, vibrato, false, true, entityName)
    end)
end