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

    if not SafeSkipMiniGame() then
        return "no minigame active";
    end
end

ConsoleCommands['toggleobjecttracking'] = function()

    SetTrackObjectUnderMouse(not GetTrackObjectUnderMouse());

end

function RegisterConsoleCommands()

    Console:RegisterCommand('exit');
    Console:RegisterCommand('skip');
    Console:RegisterCommand('loadlevel');
    Console:RegisterCommand('findtool');
    Console:RegisterCommand('toggleobjecttracking');
    Console:RegisterCommand('soundvolume');
    Console:RegisterCommand('musicvolume');
    Console:RegisterCommand('addgamestate');
    Console:RegisterCommand('setgamestate');
    Console:RegisterCommand('removegamestate');

end

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

    local params = ExplodeString(' ', command);

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

    if (params:get(0) == 'loadlevel')
    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
                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 not HasGameState('ToolFound', tool_name) then
                AddGameState('ToolFound', tool_name)
                AddGameState('ToolInfoShown', tool_name)
                AddTool(tool_name)
            else
                return "Tool already found"
            end
        else
            return "Not enough arguments"
        end
        return "";
    end
    
    if (params:get(0) == 'soundvolume')
    then
        if (params:size() > 1)
        then
            local vol = CorrectValue(tonumber(params:get(1)), 0, 1);
            GM:SetSoundVolume(vol);            
        else
            return "Not enough arguments"
        end
        return "";
    end
    
    if (params:get(0) == 'musicvolume')
    then
        if (params:size() > 1)
        then
            local vol = CorrectValue(tonumber(params:get(1)), 0, 1);
            GM:SetMusicVolume(vol);            
        else
            return "Not enough arguments"
        end
        return "";
    end
    
    if (params:get(0) == 'addgamestate')
    then
        if (params:size() > 2)
        then
            if HasGameState(params:get(1), params:get(2))
            then
                return "Game state already exists";
            end
            AddGameState(params:get(1), params:get(2))
        else
            return "Not enough arguments"
        end
        return "";
    end
    
    if (params:get(0) == 'setgamestate')
    then
        if (params:size() > 2)
        then
            SetGameState(params:get(1), params:get(2))
        else
            return "Not enough arguments"
        end
        return "";
    end
    
    if (params:get(0) == 'removegamestate')
    then
        if (params:size() > 2)
        then
            if not HasGameState(params:get(1), params:get(2))
            then
                return "No such game state";
            end
            RemoveGameState(params:get(1), params:get(2))
        else
            return "Not enough arguments"
        end
        return "";
    end
    
    return "Command not found: " .. command;

end

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

function ObjectNotFound()

    local bp = 0;
    bp = bp + 1;

end

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


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