
ui = {
    button = function(text, x, y, w, h, color)
        local text_x = (w-(#text*4))/2
        draw.rrectf(x,y+1,w,h,2,color[1])
        if mouse.inside(x,y,w,h) then
            if mouse.down(mouse.LEFT) then y=y+1 end
            draw.rrectf(x,y,w,h,2,color[3])
            if mouse.press(mouse.LEFT) then return true end
        else
            draw.rrectf(x,y,w,h,2,color[2])
        end
        local text_y = (h-5)/2
        draw.text(text,x+text_x+1,y+text_y,color[1])
        return false
    end,

    filedialog = {
        folder = nil,
        files = nil,
        dir = ".",
        selected = 0,
        offset = 0,
        lift_height = -1,
        lift_top = 0,
        old_update = nil,
        callback = nil,

        show = function(callback)
            if not ui.filedialog.folder then
                ui.filedialog.folder=surf.new(5,4)
                surf.target(ui.filedialog.folder)
                for i=0,4 do for j=0,3 do surf.pixel(i,j,0) end end
                for i=2,4 do surf.pixel(i,0,255) end
                surf.target(0)
            end
            ui.filedialog.loadDir(".")
            ui.filedialog.callback = callback
            ui.filedialog.old_update = update
            update = ui.filedialog.update
        end,

        loadDir = function(path)
            ui.filedialog.files = sys.dir(path)
            if #ui.filedialog.files > 0 then
                ui.filedialog.dir = ui.filedialog.files[1].name:match("(.+)/[^/]+$")
            else
                ui.filedialog.dir = path
            end
            table.insert(ui.filedialog.files, 1, { dir=true, name=".." })
            ui.filedialog.selected = 0
            ui.filedialog.offset = 0
            ui.filedialog.lift_height=-1
            ui.filedialog.lift_top=0
            if #ui.filedialog.files>23 then
                ui.filedialog.lift_height=23*161/#ui.filedialog.files
            end
        end,

        goBack = function()
            ui.filedialog.dir = ui.filedialog.dir:match("(.+)/[^/]+$")
            ui.filedialog.loadDir(ui.filedialog.dir)
        end,

        select = function()
            if ui.filedialog.files[ui.filedialog.selected].dir then
                local filename = ui.filedialog.files[ui.filedialog.selected].name:match("([^/]+)$")
                ui.filedialog.loadDir(ui.filedialog.dir.."/"..filename)
            else
                update = ui.filedialog.old_update
                ui.filedialog.callback(ui.filedialog.files[ui.filedialog.selected].name)
            end
        end,

        update = function()
            pal.trans(255)
            view.origin(20, 20)
            draw.rrectf(0,0,280,200,3,15)
            draw.rrectf(4,4,272,9,2,12)
            draw.rrectf(4,17,272,163,2,12)
            if ui.filedialog.lift_height>0 then draw.rrectf(271,18+ui.filedialog.lift_top,4,ui.filedialog.lift_height,2,14) end

            if ui.button("OK",192,184,40,11,{9,10,11}) then ui.filedialog.select() end
            if ui.button("CANCEL",236,184,40,11,{1,2,3}) then update = ui.filedialog.old_update end

            local y = 19
            draw.text(ui.filedialog.dir, 6, 6, 0)

            surf.source(ui.filedialog.folder)

            local count = 0
            for k,file in ipairs(ui.filedialog.files) do
                local filename = file.name:match("([^/]+)$")
                if count < ui.filedialog.offset then goto continue end
                --if not ui.filedialog.filter or filename:match("%."..ui.filedialog.filter.."$") then
                if mouse.inside(4,y-1,272,7) then
                --if mx>4 and mx<268 and my>=y-1 and my<y+6 then
                    draw.rectf(4,y-1,272,7,13)
                    if mouse.dblclick() then
                        ui.filedialog.select()
                    elseif mouse.press(mouse.LEFT) then
                        ui.filedialog.selected = k
                    end
                end
                
                if ui.filedialog.selected == k then
                    draw.rectf(4,y-1,272,7,10)
                end

                if file.dir then
                    draw.surf(0,0,5,4,7,y+1,5,4)
                    pal.subpal(0,3)
                    draw.surf(0,0,5,4,6,y,5,4)
                    pal.subpal(0)
                    draw.text(filename, 13, y, 0)
                else
                    draw.text(filename, 6, y, 0)
                end
                y=y+7
                ::continue::
                count=count+1
                if count-ui.filedialog.offset>=23 then break end
            end

            if key.press(key.RETURN) and ui.filedialog.selected~=-1 then
                ui.filedialog.select()
            elseif key.press(key.DOWN) and ui.filedialog.selected<#ui.filedialog.files then
                ui.filedialog.selected=ui.filedialog.selected+1
            elseif key.press(key.UP) then
                if ui.filedialog.selected>1 then
                    ui.filedialog.selected=ui.filedialog.selected-1
                else
                    ui.filedialog.selected = 1
                end
            end

            local scroll = -mouse.wheel()
            if scroll~=0 then
                ui.filedialog.offset=ui.filedialog.offset+scroll
                if ui.filedialog.offset<0 then ui.filedialog.offset=0 end
                if ui.filedialog.offset+23 > #ui.filedialog.files then ui.filedialog.offset = #ui.filedialog.files-23 end
                ui.filedialog.lift_top=ui.filedialog.offset*161/#ui.filedialog.files
            end
        end
    }
}