local scroll = 0
local procs = lOS.processes
local sel = {}
local pSel = {}
local expanded = {}
local cEnd = false
local function drawProcs(x1,y1,x2,y2,e)
    cY = y1-scroll
    local pr = {}
    local bg = {}

    y2 = y2-2

    for p=1,#procs do
        if procs[p].path then
            if procs[p].win then
                if not pr[procs[p].path] then
                    if expanded[procs[p].path] == nil then
                        expanded[procs[p].path] = false
                    end
                    pr[#pr+1] = {expanded=expanded[procs[p].path],path=procs[p].path,pr={procs[p]}}
                    pr[procs[p].path] = pr[#pr]
                else
                    for t=1,#pr do
                        if pr[t].path == procs[p].path then
                            pr[t].pr[#pr[t].pr+1] = procs[p]
                        end
                    end
                end
            else
                bg[#bg+1] = procs[p]
            end
        end
    end
    local clickmenu = {}
    -- TEMP

    if type(pSel) == "table" then
        sel = pSel
    elseif type(pSel) == "number" then
        sel = pr[pSel]
    else
        sel = {}
    end
    --if pr[2] then
        --pr[2].expanded = true
    --end
    if cY+1 >= y1 and cY+1 <= y2 then
        term.setTextColor(colors.blue)
        term.setBackgroundColor(colors.white)
        term.setCursorPos(x1,cY+1)
        term.write("Programs ("..#pr..")")
        cY = cY+2
    end
    for p=1,#pr do
        if cY >= y1 and cY <= y2 then
            term.setCursorPos(x1,cY)
            if p == 1 or sel ~= pr[p-1] then
                term.setTextColor(colors.white)
            else
                term.setTextColor(colors.lightBlue)
            end
            if sel ~= pr[p] then
                term.setBackgroundColor(colors.white)
            else
                term.setBackgroundColor(colors.lightBlue)
            end
            term.write(string.rep("\131",x2-(x1-1)))
        end
        cY = cY+1
        if cY >= y1 and cY <= y2 then
            --term.setTextColor(colors.black)
            clickmenu[cY] = p
            term.setCursorPos(x1,cY)
            term.write(string.rep(" ",x2-(x1-1)))
            term.setCursorPos(x1+1,cY)
            if sel ~= pr[p] then
                term.setBackgroundColor(colors.white)
            else
                term.setBackgroundColor(colors.lightBlue)
            end
            if not pr[p].expanded then
                term.setTextColor(colors.lightGray)
                term.write("\16 ")
            else
                term.setTextColor(colors.gray)
                term.write("\31 ")
            end
            term.setTextColor(colors.black)
            local t = lUtils.getFileName(pr[p].path)
            t = t:sub(1,1):upper()..t:sub(2)
            term.write(t.." ("..#pr[p].pr..")")
        end
        cY = cY+1
        if pr[p].expanded then
            for t=1,#pr[p].pr do
                if cY >= y1 and cY <= y2 then
                    clickmenu[cY] = pr[p].pr[t]
                    if sel == pr[p].pr[t] or sel == pr[p] then
                        term.setBackgroundColor(colors.lightBlue)
                    else
                        term.setBackgroundColor(colors.white)
                    end
                    term.setTextColor(colors.black)
                    if not sel == pr[p] then
                        term.setCursorPos(x1+3,cY)
                        term.write(string.rep(" ",(x2)-(x1+2)))
                    else
                        term.setCursorPos(x1,cY)
                        term.write(string.rep(" ",x2-(x1-1)))
                    end
                    term.setCursorPos(x1+4,cY)
                    term.write(pr[p].pr[t].title)
                end
                cY = cY+1
            end
        end
    end
    -- draw bg processes (no extended)
    if cY+1 >= y1 and cY+1 <= y2 then
        term.setTextColor(colors.blue)
        term.setBackgroundColor(colors.white)
        term.setCursorPos(x1,cY+1)
        term.write("Background Processes ("..#bg..")")
        cY = cY+2
    end
    for p=1,#bg do
        if cY >= y1 and cY <= y2 then
            term.setCursorPos(x1,cY)
            if p == 1 or sel ~= bg[p-1] then
                term.setTextColor(colors.white)
            else
                term.setTextColor(colors.lightBlue)
            end
            if sel ~= bg[p] then
                term.setBackgroundColor(colors.white)
            else
                term.setBackgroundColor(colors.lightBlue)
            end
            term.write(string.rep("\131",x2-(x1-1)))
        end
        cY = cY+1
        if cY >= y1 and cY <= y2 then
            --term.setTextColor(colors.black)
            clickmenu[cY] = bg[p]
            term.setCursorPos(x1,cY)
            term.write(string.rep(" ",x2-(x1-1)))
            term.setCursorPos(x1+1,cY)
            if sel ~= bg[p] then
                term.setBackgroundColor(colors.white)
            else
                term.setBackgroundColor(colors.lightBlue)
            end
            term.write("  ")
            term.setTextColor(colors.black)
            local t = ""
            if bg[p].title then
                t = bg[p].title
            else
                t = bg[p].path
            end
            term.write(t)
        end
        cY = cY+1
    end


    term.setCursorPos(x1,y2+1)
    term.setBackgroundColor(colors.white)
    term.setTextColor(colors.gray)
    term.write(string.rep("\131",x2-(x1-1)))
    --term.setCursorPos(x1,y2+2)
    local txt = "End Task"
    term.setCursorPos(x2-(string.len(txt)),y2+2)
    if sel.path then
        if not cEnd then
            term.setTextColor(colors.blue)
        else
            term.setTextColor(colors.lightBlue)
        end
    else
        term.setTextColor(colors.lightGray)
    end
    term.write(txt)

    if e[1] == "mouse_click" then
        if e[3] >= x1 and e[3] <= x2 and e[4] >= y1 and e[4] <= y2 then
            sel = {}
            pSel = {}
        end
        if sel.path and e[4] == y2+2 and e[3] >= x2-string.len(txt) and e[3] <= x2-1 then
            term.setCursorPos(x2-(string.len(txt)),y2+2)
            term.setTextColor(colors.lightBlue)
            term.write(txt)
            cEnd = true
        end
        if clickmenu[e[4]] then
            pSel = clickmenu[e[4]]
            if type(pSel) == "number" and e[3] == x1+1 then
                if expanded[pr[pSel].path] == false then
                    expanded[pr[pSel].path] = true
                else
                    expanded[pr[pSel].path] = false
                end
            end
        end
    elseif e[1] == "mouse_up" then
        cEnd = false
        if sel.path and e[4] == y2+2 and e[3] >= x2-string.len(txt) and e[3] <= x2-1 then
            local t = 1
            while true do
                if lOS.processes[t] == nil then break end
                if type(sel[1]) == "thread" then
                    if lOS.processes[t] == sel then
                        for i=1,#lOS.wins do
                            if lOS.wins[i] == lOS.processes[t] then
                                table.remove(lOS.wins,i)
                            end
                        end
                        table.remove(lOS.processes,t)
                        break
                    else
                        t = t+1
                    end
                else
                    if lOS.processes[t].path == sel.path then
                        for i=1,#lOS.wins do
                            if lOS.wins[i] == lOS.processes[t] then
                                table.remove(lOS.wins,i)
                            end
                        end
                        table.remove(lOS.processes,t)
                    else
                        t = t+1
                    end
                end
            end
            sel = {}
            pSel = {}
        end
    elseif e[1] == "mouse_scroll" then
        scroll = scroll+e[2]
    end
end
-- sel wont work since pr gets regenerated every time
-- it will work for subprocesses tho
local w,h = term.getSize()
term.setBackgroundColor(colors.white)
term.clear()
while true do
    e = {os.pullEvent()}
    term.setBackgroundColor(colors.white)
    term.clear()
    local w,h = term.getSize()
    drawProcs(2,1,w-1,h,e)
    if e[1] == "key_up" and e[2] == keys.t and lUtils.isHolding(keys.leftShift) then
        term.setBackgroundColor(colors.black)
        term.clear()
        term.setCursorPos(1,1)
        term.setTextColor(colors.red)
        print("Terminated")
        return
    end
end