os.loadAPI("/System/API/image")
os.loadAPI("/System/API/windows")
os.loadAPI("/rom/apis/gps")
os.loadAPI("/System/API/sysbar")
local run = true
local xSize, ySize = term.getSize()
local xCenter = math.floor(xSize/2)
local yCenter = math.floor(ySize/2)
local obj = {}
local screen = 1
local maxScreen = 1
local xp = 3
local yp = 3
local pos = -1
local files = {}
local lenthScroll = 0
local posScroll = 0
local step = 0
local lenthPage = 0
local screen = window.create(term.current(),1,3,xSize-1,ySize-3)

local function refreshFiles()
	files = fs.list('/.ds/'..l_login)
	return files
end
 
local newObj = function(name,x1,y1,x2,y2)
 obj[name] = {}
 obj[name]["x1"] = x1
 obj[name]["y1"] = y1
 obj[name]["x2"] = x2
 obj[name]["y2"] = y2
end
 
local delApp = function()
 files = refreshFiles()
 for i=1,#files do
  newObj(files[i],-1,-1,-1,-1)
 end
end

local clear = function(color)
 term.setBackgroundColor(color)
 term.clear()
 term.setCursorPos(1,1)
end
 
local text = function(string,xPos,yPos)
 term.setCursorPos(xPos,yPos)
 term.write(string)
end
 
local function fadeOut(time)
 clear(colors.white)
        term.setCursorPos(1,1)
        term.setTextColor(colors.white)
end

local sysBar = function()
	while true do
		files = refreshFiles()
		sysbar.header(" < Applications",mainTextCol,mainColor)
		sysbar.draw(colors.white,colors.black,noty,true,false)
		sleep(1)
	end
end

local app = function(name,ico,x,y)
 image.draw(x,y,ico)
 term.setBackgroundColor(colors.white)
 term.setTextColor(colors.black)
 text(name,x+7,y+1)
 text("    ",x+#name+3,y+1)
 newObj(name,xSize-10,y+5,xSize-2,y+5)
 local size = fs.getSize("/.ds/"..l_login..'/'..name.."/main")
 size = math.floor(size/1024)
 term.setTextColor(colors.gray)
 text("Size: "..size.." KB",x+7,y+2)
 term.setTextColor(colors.red)
 text("Delete",xSize-7,y+3)
end

local redraw = function()
 term.redirect(screen)
 clear(colors.white)
 files = refreshFiles()
 maxScreen = math.floor(#files/3+1)
 for i=1,#files do
  app(files[i],"/.ds/"..l_login..'/'..files[i].."/Resources/icon.png",1,i*5+pos-2)
 end
 term.redirect(term.native())
 step = sysbar.scrollBar(#files,5,posScroll,2)
end

local running = function()
 while run and l_login ~= '$test' do
  event, side, x, y = os.pullEvent()
  if event == "mouse_scroll" then
   if #files>3 then
    if side == 1 then
     if pos>-#files*5+15 then
      posScroll = posScroll-step
      pos = pos-2
     else
      pos = -#files*5+15
     end 
    elseif side == -1 then
     if pos<-1 then 
      pos = pos+2
      posScroll = posScroll+step
     else
      pos = -1
     end
    end
    redraw()
   end
  elseif event == "key" then
   if side == 208 then
    if pos>-#files*5+15 then
     posScroll = posScroll-step
     pos = pos-2
    else
     pos = -#files*5+15
    end 
   elseif side == 200 then
    if pos<-1 then 
     pos = pos+2
     posScroll = posScroll+step
    else
     pos = -1
    end
   end
   redraw()
  elseif event == "mouse_click" then
   if sysbar.back(x,y) or y==1 then
    term.clear()
    term.setCursorPos(1,1)
    run = false
   elseif sysbar.home(x,y) then
    home = true
    term.clear()
    term.setCursorPos(1,1)
    run = false
   else
    files = refreshFiles()
    for i=1,#files do
     if x>=obj[files[i]]["x1"] and x<=obj[files[i]]["x2"] and y>=obj[files[i]]["y1"] and y<=obj[files[i]]["y2"] then
      local but = windows.select({"Delete"},{"Are you sure?"},{"Yes",colors.red,colors.white},{"No",colors.gray,colors.white})
      if but == "Yes" then
       shell.run("rm /.ds/"..l_login..'/'..files[i])
       redraw()
       break
      elseif but == "No" then redraw() end
     end
    end
   end
  end
 end
end

clear(colors.white)
redraw()
parallel.waitForAny(sysBar,running)
fadeOut(0)