-- PROFILER

local profiler = dofile(reaper.GetResourcePath() ..
  '/Scripts/ReaTeam Scripts/Development/cfillion_Lua profiler.lua')
reaper.defer = profiler.defer

-- Start of CFillion's Vertical Slider script)

package.path = reaper.ImGui_GetBuiltinPath() .. '/?.lua'
local ImGui = require 'imgui' '0.9.1'

local v = 16

local function VSliderDouble_Speed(ctx, label, width, height, v, min, max, speed, format)
  local changed = false
  format = format or '%.02f'

  local draw_list = ImGui.GetWindowDrawList(ctx)
  local inner_spacing_x, inner_spacing_y =
    ImGui.GetStyleVar(ctx, ImGui.StyleVar_ItemInnerSpacing)
  local frame_padding_x, frame_padding_y =
      ImGui.GetStyleVar(ctx, ImGui.StyleVar_FramePadding)

  ImGui.BeginGroup(ctx)

  -- hit box
  ImGui.InvisibleButton(ctx, label, width, height)
  local x1, y1 = ImGui.GetItemRectMin(ctx)
  local x2, y2 = ImGui.GetItemRectMax(ctx)
  local active = ImGui.IsItemActive(ctx)
  local hovered = ImGui.IsItemHovered(ctx)

  -- compute value
  local grab_pad, grab_sz = 2, ImGui.GetStyleVar(ctx, ImGui.StyleVar_GrabMinSize)
  local usable_h = height - (grab_pad * 2) - grab_sz
  local frac = 1 - ((v - min) / (max - min))
  if ImGui.IsItemActivated(ctx) then
    local mouse_y = select(2, ImGui.GetMousePos(ctx)) - (y1 + grab_pad + grab_sz / 2)
    changed, frac = true, mouse_y / usable_h
  elseif active then
    local mouse_dy = select(2, ImGui.GetMouseDragDelta(ctx, nil, nil,
      ImGui.MouseButton_Left, 0))
    ImGui.ResetMouseDragDelta(ctx)
    local range = 1 / (max - min)
    changed, frac = true, frac + (mouse_dy * range * speed)
  end
  if changed then
    frac = math.max(0, math.min(1, frac))
    v = ((max - min) * (1 - frac)) + min
  end

  -- background
  ImGui.DrawList_AddRectFilled(draw_list, x1, y1, x2, y2,
    ImGui.GetColor(ctx, active and ImGui.Col_FrameBgActive or
    hovered and ImGui.Col_FrameBgHovered or ImGui.Col_FrameBg))

  -- grab box
  local grab_x1, grab_y1 = x1 + grab_pad, y1 + grab_pad + math.floor(usable_h * frac)
  local grab_x2, grab_y2 = x2 - grab_pad, grab_y1 + grab_sz
  ImGui.DrawList_AddRectFilled(draw_list, grab_x1, grab_y1, grab_x2, grab_y2,
    ImGui.GetColor(ctx, active and ImGui.Col_SliderGrabActive or ImGui.Col_SliderGrab),
    ImGui.GetStyleVar(ctx, ImGui.StyleVar_GrabRounding))

  -- value
  local v_f = (format):format(v)
  local v_w = ImGui.CalcTextSize(ctx, v_f)
  local v_x = math.max(x1, x1 + ((width - v_w) / 2))
  
  -- Enable these 2 lines to get the visual value of the slider
  --ImGui.DrawList_AddTextEx(draw_list, nil, 0, v_x, y1 + frame_padding_y,
    --ImGui.GetColor(ctx, ImGui.Col_Text), v_f, 0, x1, y1, x2, y2)

  -- label
  ImGui.SameLine(ctx, nil, inner_spacing_y)
  ImGui.AlignTextToFramePadding(ctx)
  ImGui.Text(ctx, label)

  ImGui.EndGroup(ctx)

  return changed, v
end

-- end of CFillion's Vertical Slider
function get_files_in_directory(directory, files)
    files = files or {}
    local idx = 0
    local file = reaper.EnumerateFiles(directory, idx)
    
    while file do
        if not file:match("^%.") then  -- Skip hidden files
            table.insert(files, directory .. "\\" .. file)
        end
        idx = idx + 1
        file = reaper.EnumerateFiles(directory, idx)
    end
    
    idx = 0
    local subdirectory = reaper.EnumerateSubdirectories(directory, idx)
    
    while subdirectory do
        if not subdirectory:match("^%.") then  -- Skip hidden directories
            get_files_in_directory(directory .. "\\" .. subdirectory, files)
        end
        idx = idx + 1
        subdirectory = reaper.EnumerateSubdirectories(directory, idx)
    end
    
    return files
end

function files_variables()
    idx_min = 0
    idx_max = 30 -- change this to show more items
    idx = 0
end
    
function fonts()
    font1 = reaper.ImGui_CreateFont("Arial", 16)
    font2 = reaper.ImGui_CreateFont("Arial", 14)
    reaper.ImGui_Attach(ctx, font1)
    reaper.ImGui_Attach(ctx, font2)
end

function images()
    fader_image = reaper.ImGui_CreateImage("C:\\Users\\franc\\Downloads\\mcp_volthumb.png")
    fader_line_image = reaper.ImGui_CreateImage("C:\\Users\\franc\\Downloads\\gen_volbg_vert.png")
    preferences_image = reaper.ImGui_CreateImage("C:\\Users\\franc\\Downloads\\preference.png")
    reaper.ImGui_Attach(ctx, fader_image)
    reaper.ImGui_Attach(ctx, fader_line_image)
    reaper.ImGui_Attach(ctx, preferences_image)
end

function default_preferences()
    dark_mode = true
    light_mode = false
    
    arial_font = true
    default_font = false
    
    wav_filter = true
    mp3_filter = true
    aiff_filter = true
    png_filter = true
    mp4_filter = true
    vst2_filter = true
    vst3_filter = true
end

function get_file_names()
    if files then
        filenames = {}  -- Initialize or clear the filenames table
        for _, file in ipairs(files) do
            local fileName = file:match("^.+\\([^\\]+)$") -- extract the file name from the full path
            table.insert(filenames, fileName)
        end
        table.sort(filenames, function(a, b) return a:lower() < b:lower() end)  -- Sort filenames alphabetically in a case-insensitive manner
    end
end

function GUI()

    reaper.ImGui_PushStyleVar(ctx, reaper.ImGui_StyleVar_FrameRounding(), 1)
    reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_FrameBg(), 0x1C1C1CFF)
    reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_Button(), 0x1C1C1CFF)
    
    -- SEARCH BAR
    reaper.ImGui_SetNextItemWidth(ctx, 300)
    reaper.ImGui_InputTextWithHint(ctx, "##", "SEARCH (SAMPLES, FX, ETC..)")
    
    -- FILTER BUTTON
    reaper.ImGui_SameLine(ctx)
    if reaper.ImGui_BeginPopup(ctx, "FILTER POPUP") then
        if reaper.ImGui_Checkbox(ctx, "WAV", wav_filter) then
            wav_filter = not wav_filter
        end
        if reaper.ImGui_Checkbox(ctx, "MP3", mp3_filter) then
            mp3_filter = not mp3_filter
        end
        if reaper.ImGui_Checkbox(ctx, "AIFF", aiff_filter) then
            aiff_filter = not aiff_filter
        end
        if reaper.ImGui_Checkbox(ctx, "PNG", png_filter) then
            png_filter = not png_filter
        end
        if reaper.ImGui_Checkbox(ctx, "MP4", mp4_filter) then
            mp4_filter = not mp4_filter
        end
        if reaper.ImGui_Checkbox(ctx, "VST2", vst2_filter) then
            vst2_filter = not vst2_filter
        end
        if reaper.ImGui_Checkbox(ctx, "VST3", vst3_filter) then
            vst3_filter = not vst3_filter
        end
        reaper.ImGui_EndPopup(ctx)
    end
    if reaper.ImGui_Button(ctx, "FILTER", 102) then
        reaper.ImGui_OpenPopup(ctx, "FILTER POPUP")
    end
    
    -- PREFERENCES BUTTON
    reaper.ImGui_SameLine(ctx)
    if reaper.ImGui_BeginPopup(ctx, "HELLO") then
        reaper.ImGui_PushFont(ctx, font2)
        reaper.ImGui_Text(ctx, "THEME")
        if reaper.ImGui_RadioButton(ctx, "LIGHT MODE", light_mode) then
            light_mode = true
            dark_mode = false
        end
        reaper.ImGui_SameLine(ctx)
        if reaper.ImGui_RadioButton(ctx, "DARK MODE", dark_mode) then
            dark_mode = true
            light_mode = false
        end
        reaper.ImGui_Text(ctx, "FONT")
        if reaper.ImGui_RadioButton(ctx, "ARIAL", arial_font) then
            arial_font = true
            default_font = false
        end
        reaper.ImGui_SameLine(ctx)
        if reaper.ImGui_RadioButton(ctx, "ORIGINAL", default_font) then
            default_font = true
            arial_font = false
        end
        reaper.ImGui_PopFont(ctx)
        reaper.ImGui_EndPopup(ctx)
    end
    
    if reaper.ImGui_Button(ctx, "##Preferences Button", 20) then
        reaper.ImGui_OpenPopup(ctx, "HELLO")
    end
    
    reaper.ImGui_SetCursorPos(ctx, 429, 12)
    reaper.ImGui_Image(ctx, preferences_image, 15, 15)
    
    reaper.ImGui_SetCursorPosY(ctx, 35)
    
    -- FILE WINDOW
    if reaper.ImGui_BeginChild(ctx, "##File Windoww", 280, 650, reaper.ImGui_ChildFlags_Border()|reaper.ImGui_ChildFlags_FrameStyle()) then
        
        -- FILES

        -- IF ENTER KEY IS PRESSED 
        filenames = filenames or {}            
                
            -- Display the selectables with sorted filenames
            local itemsPerPage = 32
            local fileCount = #filenames
            idx_max = math.min(idx_min + itemsPerPage, fileCount)

            -- Display the selectables with sorted filenames within the current page
            for i = idx_min + 1, idx_max do
                if filenames[i] then -- Check if the index is valid
                    reaper.ImGui_Selectable(ctx, filenames[i]) -- display only the file name
                end
            end
        
        if reaper.ImGui_GetMouseWheel(ctx) ~= 0 then
            if idx_min >= 0 then
                idx_max = idx_max - reaper.ImGui_GetMouseWheel(ctx)
                idx_min = idx_min - reaper.ImGui_GetMouseWheel(ctx)

                if idx_min < 0 then
                    idx_min = 0
                    idx_max = 40
                --elseif idx_max > #files then
                  --  idx_min = idx_max - 10
                    --idx_max = #files
                end

            end

        end
        reaper.ImGui_EndChild(ctx)
        
        -- FILE SCROLL BAR
        reaper.ImGui_SameLine(ctx, 287)
        reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_SliderGrab(), 0x4C4C4CFF)
        --reaper.ImGui_SetCursorPos(ctx, 288, 0)
        reaper.ImGui_PushStyleVar(ctx, reaper.ImGui_StyleVar_GrabRounding(), 12)
        reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_FrameBgHovered(), 0x1C1C1CFF)
        reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_FrameBgActive(), 0x1C1C1CFF)
        reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_SliderGrabActive(), 0x4C4C4CFF)
        local touched, vslidervalue = VSliderDouble_Speed(ctx, " ", 12, 650, idx_min, - (#filenames) , 0, 0.6)
        
        if touched then
            idx_min = math.ceil(-vslidervalue)
        end
            
        reaper.ImGui_PopStyleVar(ctx, 1)
        reaper.ImGui_PopStyleColor(ctx, 4)
        
    end

    -- DIRECTORY WINDOW
    reaper.ImGui_SameLine(ctx)
    if reaper.ImGui_BeginChild(ctx, "##DIR Window", 130, 650, reaper.ImGui_ChildFlags_Border()|reaper.ImGui_ChildFlags_FrameStyle()) then
        reaper.ImGui_Button(ctx, "PLUG-INS", 122, 20)
        reaper.ImGui_Button(ctx, "MIDI TOOLS", 122, 20)
        if reaper.ImGui_Button(ctx, "BOUNCES", 122, 20) then
            
            files = {}
            files = get_files_in_directory("C:\\Users\\franc\\Desktop\\Desktop (A organizar)\\RYKORD\\Projects\\Bounces")
            filenames = {}
            get_file_names()
            -- sort them
        end
        
        if reaper.ImGui_Button(ctx, "+", 122, 20) then
            -- Add directory functions
        end
        
        reaper.ImGui_EndChild(ctx)
    end
    
    -- PLAY BUTTON
    if reaper.ImGui_ArrowButton(ctx, "##PlayButton", 1) then
        -- Preview File Function
    end
    
    -- PREVIEW VOLUME SLIDER
    reaper.ImGui_SameLine(ctx)
    reaper.ImGui_SetNextItemWidth(ctx, 270)
    reaper.ImGui_SetCursorPosX(ctx, 50)
    reaper.ImGui_PushStyleVar(ctx, reaper.ImGui_StyleVar_Alpha(), 0)
    local changed, new_value = reaper.ImGui_SliderDouble(ctx, "##Volume Slider", value, 0, 2)
    if changed then
        value = new_value
    end
    reaper.ImGui_PopStyleVar(ctx, 1)
    reaper.ImGui_SetCursorPos(ctx, 30, 692)
    reaper.ImGui_Image(ctx, fader_line_image, 295, 15)
    reaper.ImGui_SetCursorPos(ctx, 30 + new_value * 120, 682)
    reaper.ImGui_Image(ctx, fader_image, 48, 36)
    
    
    reaper.ImGui_PopStyleColor(ctx, 2)
    reaper.ImGui_PopStyleVar(ctx, 1)
end

function main()
    
    reaper.ImGui_PushStyleColor(ctx, reaper.ImGui_Col_WindowBg(), 0x2C2C2CFF)
    local is_visible, is_open = reaper.ImGui_Begin(ctx, "Media Browser", true)
    
    if is_visible then
    reaper.ImGui_PushFont(ctx, font1)
    GUI()
    reaper.ImGui_PopFont(ctx)
    
    reaper.ImGui_End(ctx)
    end
    
    reaper.ImGui_PopStyleColor(ctx) 
    
    if is_open then
        reaper.defer(main)
    end
    
end

--directory_for_test = "C:\\Users\\franc\\Desktop\\Desktop (A organizar)\\RYKORD\\Projects\\Bounces"
ctx = reaper.ImGui_CreateContext("Media Browser")

files_variables()

fonts()

images()

default_preferences()

reaper.ImGui_SetNextWindowDockID(ctx, -7)

main()

-- PROFILER END
profiler.attachToWorld() -- after all functions have been defined
profiler.run()

why doesn't the vslider work as intended? brief explanation only