local ConsoleCommands = {};
ConsoleCommands['exit'] = AppExit;
ConsoleCommands['skip'] = function()
    if play_field and play_field:GetLevelScreen():GetPanel():GetMode() == PM_MINIGAME
    then
        SkipMiniGame();
        return "";
    end
    
    return "no minigame active";
end

ConsoleCommands['toggleobjecttracking'] = function()

    SetTrackObjectUnderMouse(not GetTrackObjectUnderMouse());

end

ConsoleCommands['start'] = function(params)
    local CurrPlayer = GetGameManager():GetCurrPlayer()
    if CurrPlayer and CurrPlayer.m_gameState:Clear() then
        CurrPlayer.m_gameState:Clear()
    end
    
    local levelNum = tonumber(params:get(1))
    local levelPath = levels[levelNum]
    if levelPath ~= nil then
        if mainMenu ~= nil then
            mainMenu:Finish()
            mainMenu:Destroy()
            mainMenu = nil
                    
            PopUpScreensMgr:DestroyAll()
        end
        
        __EXTRAS_SCENE = (levelNum > 500);
        
        StartLevel(levelPath)
        return ""
    else
        return "Wrong level index"
    end
end

function RegisterConsoleCommands()

    Console:RegisterCommand('exit');
    Console:RegisterCommand('start')
    Console:RegisterCommand('skip')
    Console:RegisterCommand('loadlevel');
    Console:RegisterCommand('findtool');
    Console:RegisterCommand('addnote');
    Console:RegisterCommand('toggleobjecttracking');
end


--------------------------------------------------------------------------------
function ProcessConsoleCommand(console, command)

    local params = ExplodeString(' ', command);

    for name, command in pairs(ConsoleCommands)
    do
        if (params:get(0) == name)
        then
            local res = command(params);
            return res or "";
        end
    end

    if(params:get(0) == 'loadlevel' or params:get(0) == 'start')
    then
        if (params:size() > 1)
        then
            local levelNum  = tonumber(params:get(1));
            local levelPath = levels[levelNum];
            if (levelPath ~= nil)
            then
                if (mainMenu ~= nil)
                then
                    mainMenu:Finish();
                    mainMenu:Destroy();
                    mainMenu = nil;
                    
                    PopUpScreensMgr:DestroyAll();
                end
                
                __EXTRAS_SCENE = (levelNum > 500);
                
                StartLevel(levelPath)
                --load_level(levelPath);
                return "";
            else
                return "Wrong level index";
            end
        else
            return "Not enough arguments"
        end
        return "";
    end
    
    if (params:get(0) == 'findtool')
    then
        if (params:size() > 1)
        then
            local tool_name = params:get(1)
            if GetTool(tool_name) == false then
                return "Error in tool name"
            elseif not HasGameState('ToolFound', tool_name) then
                AddGameState('ToolFound', tool_name)
                AddGameState('ToolInfoShown', tool_name)
                AddTool(tool_name);
                InventoryOnToolFound(tool_name);
            else
                return "Tool already found"
            end
        else
            return "Not enough arguments"
        end
        return "";
    end
    
    if (params:get(0) == 'addnote')
    then
        if (params:size() > 1)
        then
            if panel ~= nil then
                local noteId = params:get(1)
                
                if not panel.notebook.IsNoteAdded(noteId) then
                    panel.notebook.AddNote(noteId);
                else
                    return "Note already added"
                end
            else
                return "panel not created"
            end
        else
            return "Not enough arguments"
        end
        return "";
    end
    
    return "Command not found: " .. command;

end

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