-- Rykord - ImGui Media Explorer

-- Include necessary sub-functions
function GetSubdirectories(dir)
    local subdirs = {}
    local i = 0
    repeat
        local subdir = reaper.EnumerateSubdirectories(dir, i)
        if subdir then
            table.insert(subdirs, dir .. "/" .. subdir)
        end
        i = i + 1
    until not subdir
    return subdirs
end

function GetFiles(dir)
    local files = {}
    local i = 0
    repeat
        local file = reaper.EnumerateFiles(dir, i)
        if file then
            table.insert(files, dir .. "/" .. file) -- Ensure full file path is stored
        end
        i = i + 1
    until not file
    return files
end

function DrawDirectoryTree(dir)
    if reaper.file_exists(dir) then
        reaper.ImGui_Text(ctx, "File: " .. dir)
        return
    end

    local open = reaper.ImGui_TreeNode(ctx, dir)
    if open then
        local subdirs = GetSubdirectories(dir)
        for _, subdir in ipairs(subdirs) do
            DrawDirectoryTree(subdir)
        end

        local files = GetFiles(dir)
        for _, file in ipairs(files) do
            if reaper.ImGui_Selectable(ctx, file) then
                selected = file
            end
            
            if reaper.ImGui_BeginDragDropSource(ctx, reaper.ImGui_DragDropFlags_None()) then
                reaper.ImGui_SetDragDropPayload(ctx, "DND_TEXT", file)
                reaper.ImGui_Text(ctx, file)
                reaper.ImGui_EndDragDropSource(ctx)
                dragging = true
            end
        end

        reaper.ImGui_TreePop(ctx)
    end
end

function InsertMediaToTrackAtMouse(file)
    -- Check if the file path is valid
    if not file or file == "" then return end

    -- Get mouse position in arrange view and convert to project time position
    local mouse_x, mouse_y = reaper.GetMousePosition()
    local trackview = reaper.JS_Window_FindChildByID(reaper.GetMainHwnd(), 1000)
    
    if trackview then
        local proj_x, proj_y = reaper.JS_Window_ScreenToClient(trackview, mouse_x, mouse_y)
        local pos = reaper.BR_GetMouseCursorContext_Position()
        local track = reaper.GetSelectedTrack(0, 0)
        if not track then track = reaper.GetTrack(0, 0) end -- Fallback to the first track if none selected
        if track then
            reaper.Undo_BeginBlock()
            reaper.InsertMedia(file, 0)
            reaper.ShowConsoleMsg("oi")
            local item = reaper.GetSelectedMediaItem(0, 0)
            if item then
                reaper.SetMediaItemPosition(item, pos, false)
            end
            reaper.Undo_EndBlock("Insert Media at Mouse Position", -1)
        end
    end
end

function main()
    reaper.ImGui_SetNextWindowDockID(ctx, -7)
    local retval, is_open = reaper.ImGui_Begin(ctx, "Media Explorer")

    if retval then
        local search_buf = reaper.ImGui_InputText(ctx, "##search", "")

        local root_dir = "E:\\Sample Packs\\COLLECTION"
        DrawDirectoryTree(root_dir)

        -- Check mouse release outside ImGui window
        if dragging and not reaper.ImGui_IsWindowHovered(ctx, reaper.ImGui_HoveredFlags_AnyWindow()) then
            if not reaper.ImGui_IsMouseDown(ctx, 0) then
                if selected then
                    InsertMediaToTrackAtMouse(selected)
                end
                dragging = false -- Reset dragging state
            end
        end
        
        reaper.defer(main)
    end

    reaper.ImGui_End(ctx)
end

if not reaper.APIExists("JS_ReaScriptAPI_Version") then
    reaper.ShowMessageBox("This script requires the JS ReaScriptAPI extension. Please install it and try again.", "Error", 0)
else
    ctx = reaper.ImGui_CreateContext("ctx")
    dragging = false
    selected = nil
    main()
end

the drag and dropping into reaper timeline is not working. give me only the part of the solution to this script