os.loadAPI("/System/API/image")
os.loadAPI("/System/API/windows")
os.loadAPI("/System/API/sysbar")
os.loadAPI("/System/API/zip")
local run = true
local xSize, ySize = term.getSize()
local xCenter = math.floor(xSize/2)
local yCenter = math.floor(ySize/2)
local obj = {}
local arg = {...}
local path = "/"
local files = {}
local pos = 0
local prevPath = {}
prevPath[0] = "/"
local history = 0
local lenthScroll = 0
local posScroll = 0
local step = 0
local lenthPage = 0
local select = 0
local selects = {}
local sel = false
local contex = false
local corpath = ""
 
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 clear = function(color)
 term.setBackgroundColor(color)
 term.clear()
 term.setCursorPos(1,1)
end

if version < '5.1.1' then
 clear(colors.white)
 windows.error('Update your OS')
 run = false
else
 os.loadAPI("/System/API/contexts")
end
 
local text = function(string,xPos,yPos)
 term.setCursorPos(xPos,yPos)
 term.write(string)
end
 
local fadeIn = function()
 for i=1,xSize do
  paintutils.drawLine(i,1,i,ySize-1,colors.black)
  sleep(0)
 end
end
 
local function fadeOut(time)
        clear(colors.lightGray)
        sleep(time)
        clear(colors.gray)
        sleep(time)
        clear(colors.black)
        sleep(time)
        term.setCursorPos(1,1)
        term.setTextColor(colors.black)
end
 
local sysBar = function()
 sysbar.header("File Manager",mainTextCol,mainColor)
 if #path>xSize then
  text(path,xSize-#path+1,2)
  text("...",2,2)
 else
  text(path,1,2)
 end
 if #path > 1 then
  text("^",1,2)
 end
 paintutils.drawLine(1,ySize,xSize,ySize,colors.white)
 term.setTextColor(colors.black)
 if select==0 then
  newObj("()",0,0,0,0)
  newObj("<<",0,0,0,0)
  sysbar.draw(colors.white,colors.black,noty,true,true)
 else
  if select==1 then
   text(" Copy",1,ySize)
   text(" Cut",6,ySize)
   text(" Delete",10,ySize)
   if #selects~=#files then
    text(" All ",xSize-5,ySize)
   else
    text(" One ",xSize-5,ySize)
   end
   newObj("()",xSize-6,ySize,xSize-1,ySize)
   newObj("<<",xSize,ySize,xSize,ySize)
  elseif select>1 then
   text(" Paste",1,ySize)
   text(" Cancel ",7,ySize)
  end
  term.setTextColor(colors.red)
  text("x",xSize,ySize)
 end
end

local delik = function(y,color)
 for i=2,xSize-2 do
  term.setBackgroundColor(colors.wite)
  term.setTextColor(color)
  text("-",i,y)
 end
end
 
local redraw = function()
 if version >= '5.1.1' then
 clear(colors.white)
 files = fs.list(path)
 files = windows.reorganizeFilesAndFolders(path,files,true,true)
 for I=1,#files do
  yPos = pos+4*I
  if fs.isDir(path.."/"..files[I]) and windows.getFileFormat(files[I]) ~= ".apk" then
   image.draw(2,yPos,"/System/folder.png")
  elseif (windows.getFileFormat(files[I])==".apk" or windows.getFileFormat(files[I])==".app") then
    image.draw(2,yPos,"/System/app.png")
  elseif fs.isDir(path.."/"..files[I]) == false then
   if (windows.getFileFormat(files[I]) == nil or windows.getFileFormat(files[I])=='.lua' or windows.getFileFormat(files[I])=='.exe')then
    image.draw(2,yPos,"/System/prog.png")
   elseif (windows.getFileFormat(files[I])==".png" or windows.getFileFormat(files[I])==".pic" or windows.getFileFormat(files[I])==".jpg") then
    image.draw(2,yPos,"/System/picture.png")
   elseif (windows.getFileFormat(files[I])==".txt" or windows.getFileFormat(files[I])==".cfg" or windows.getFileFormat(files[I])==".ini" or windows.getFileFormat(files[I])==".md") then
    image.draw(2,yPos,"/System/text.png")
   elseif (windows.getFileFormat(files[I])==".zip" or windows.getFileFormat(files[I])==".rar" or windows.getFileFormat(files[I])==".7z" or windows.getFileFormat(files[I])==".tar" or windows.getFileFormat(files[I])==".gz") then
    image.draw(2,yPos,"/System/archive.png")
   elseif (windows.getFileFormat(files[I])==".rom" or windows.getFileFormat(files[I])==".img" or windows.getFileFormat(files[I])==".iso" or windows.getFileFormat(files[I])==".back") then
    image.draw(2,yPos,"/System/rom.png")
   else
    image.draw(2,yPos,"/System/unknown.png")
   end
   term.setBackgroundColor(colors.white)
   term.setTextColor(colors.lightGray)
   if fs.getSize(path..'/'..files[I]) >= 1024 then
    text(math.floor(fs.getSize(path..'/'..files[I])/1024).." KB",7,yPos+2)
   else
    text(fs.getSize(path..'/'..files[I]).." B",7,yPos+2)
   end
  end
  term.setBackgroundColor(colors.white)
  term.setTextColor(colors.black)
  text(files[I],7,yPos+1)
  newObj(files[I],2,yPos,xSize,yPos+2)
  if select == 1 then
   term.setTextColor(colors.lightGray)
   text("[ ]",xSize-4,yPos+1)
   for ii=1,#selects do
    if selects[ii]==files[I] then
     term.setTextColor(mainSwitchCol)
     text("V",xSize-3,yPos+1)
    end
   end
  end
 end
 step = sysbar.scrollBar(#files,4,posScroll,2)
 sysBar()
end
end 

local waitForAny = function()
	local wait = true
	while wait do
		eve = os.pullEvent()
		if eve == 'mouse_click' or eve == 'key' then
			wait = false
		end
	end
end

local running = function()
 while run == true and home == false do
  event, side, x, y = os.pullEvent()
  if event == "mouse_click" then
   if x==1 and y==2 then
    pos = 0
    posScroll = 0
    history=history-1
    if history<0 then history=0 end
    path = prevPath[history]
    redraw()
   elseif x==xSize and y==ySize and select==0 then
     term.setBackgroundColor(colors.gray)
     term.setTextColor(colors.lightGray)
     text("|",xSize,ySize)
	 local budka = contexts.main(xSize,ySize-1,'Select','New Folder','New File')
     if budka == 'Select' then
	  select = 1
      selects = {}
      contex = false
	 elseif budka == 'New Folder' then
	  redraw()
      newDir = windows.input("auto","auto","New Folder",14,{"Folder name",""})
      shell.run("mkdir "..path.."/"..newDir[1])
      contex = false
	 elseif budka == 'New File' then
	  redraw()
      newFile = windows.input("auto","auto","New File",14,{"File name",""})
      shell.run("edit "..path.."/"..newFile[1])
      contex = false
	 end
	 redraw()
   elseif select == 0 and sysbar.back(x,y) then
    pos = 0
    posScroll = 0
    history=history-1
    if history<0 then history=0 end
    path = prevPath[history]
    redraw()
   elseif select == 0 and sysbar.home(x,y) then
    term.clear()
    term.setCursorPos(1,1)
    run = false
   elseif x>=obj["<<"]["x1"] and x<=obj["<<"]["x2"] and y>=obj["<<"]["y1"] and y<=obj["<<"]["y2"] and select == 1 then
    pos = 0
    posScroll = 0
    select=0
    selects={}
    redraw()
   elseif x>=obj["()"]["x1"] and x<=obj["()"]["x2"] and y>=obj["()"]["y1"] and y<=obj["()"]["y2"] and select == 1 then
    if #selects~=#files then
     for I=1,#files do
      selects[I]=files[I]
     end
     redraw()
    else
     selects={}
     redraw()
    end
   elseif x>9 and x<=17 and y==ySize and select==1 then
    select=0
	local but = windows.select({"Dellete files"},{"Are you sure?"},{"Yes",colors.red,colors.white},{"No",colors.gray,colors.white})
     if but == "Yes" then
      for I=1,#selects do
       shell.run("rm "..path.."/"..selects[I])
      end
	 end
    selects={}
    redraw()
   elseif x>7 and x<=15 and y==ySize and select>1 then
    select=0
    selects={}
    redraw()
   elseif x>1 and x<=7 and y==ySize and select>1 then
    local f = fs.list(path)
    for I=1,#selects do
	 local find = false 
	 for i=1,#f do
		if f[i]==selects[I] then find = true end
	 end
	 if find then
	  local check = windows.select({selects[I]},{'already exist'},{'Skip',colors.lightBlue,colors.white},{'Overwrite',colors.lightBlue,colors.white},{'Rename',colors.lightBlue,colors.white})
	  if check == 'Rename' then
	   shell.run("copy "..corpath.."/"..selects[I].." "..path.."/copy-"..selects[I])
	   if select==3 then
        shell.run("rm "..corpath.."/"..selects[I])
       end
	  elseif check == 'Overwrite' then
	   shell.run("rm "..path.."/"..selects[I])
	   shell.run("copy "..corpath.."/"..selects[I].." "..path.."/"..selects[I])
	  end
	 else
	  shell.run("copy "..corpath.."/"..selects[I].." "..path.."/"..selects[I])
	  if select==3 then
       shell.run("rm "..corpath.."/"..selects[I])
      end
	 end
    end
    selects={}
    select=0
    redraw()
   elseif x>0 and x<=5 and y==ySize and select>0 and #selects>0 then
    select=2
    corpath = path
    redraw()
   elseif x>5 and x<=10 and y==ySize and select>0 and #selects>0 then
    select=3
    corpath = path
    redraw()
   end
   if side == 1 then
    files = fs.list(path)
    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"] and y<ySize then
      if select > 1 or select==0 then
       if fs.isDir(path.."/"..files[I]) then
        history = history + 1
        path = path.."/"..files[I]
        prevPath[history] = path
        pos = 0
        posScroll = 0
       elseif fs.isDir(path.."/"..files[I])==false and (windows.getFileFormat(files[I])==nil or windows.getFileFormat(files[I])=='.lua' or windows.getFileFormat(files[I])=='.exe') then
        clear(colors.white)
        sysbar.header(files[I],mainTextCol,mainColor)
	 	term.setBackgroundColor(colors.white)
	 	term.setTextColor(colors.black)
        term.setCursorPos(1,3)
        shell.run(path.."/"..files[I])
		sysbar.header(files[I],mainTextCol,mainColor)
		paintutils.drawLine(1,ySize,xSize,ySize,colors.white)
		term.setTextColor(colors.lightGray)
		text('Press any key...',1,ySize)
        waitForAny()
       elseif fs.isDir(path.."/"..files[I])==false and windows.getFileFormat(files[I])==".sec" then
        shell.run("/ver")
        shell.run(path.."/"..files[I])
       elseif fs.isDir(path.."/"..files[I])==false and (windows.getFileFormat(files[I])==".png" or windows.getFileFormat(files[I])==".pic" or windows.getFileFormat(files[I])==".jpg") then
        shell.run("/.ds/"..l_login.."/ImageViewer.apk/main "..path.." "..files[I])
	   elseif fs.isDir(path.."/"..files[I])==false and (windows.getFileFormat(files[I])==".txt" or windows.getFileFormat(files[I])==".cfg" or windows.getFileFormat(files[I])==".ini" or windows.getFileFormat(files[I])==".md") then
	    shell.run("/.ds/"..l_login.."/FileBrowser.apk/Resources/reader "..path.."/"..files[I])
       end
      else
       for ii=1,#selects do
        if files[I]==selects[ii] then
         sel=false
         for iii=ii,#selects do
          selects[iii]=selects[iii+1]
         end
        end
       end
       if sel==true then
        selects[#selects+1]=files[I]
       elseif sel==false then
        sel=true
       end
      end
      redraw()
      sel=true
      break
     end
	end
   elseif side == 2 then
	if select == 0 then
     local files = fs.list(path)
     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"] and y<ySize then
	   local budka = ""
	   if fs.isDir(path.."/"..files[I])==false and windows.getFileFormat(path.."/"..files[I])==".zip" then
	    budka = contexts.main(x,y,'Unarchive','Rename','Open as','Edit','Copy','Cut','Dellete')
	   elseif fs.isDir(path.."/"..files[I])==false then
	    budka = contexts.main(x,y,'Open as','Rename','Edit','Copy','Cut','Dellete')
	   else
	    budka = contexts.main(x,y,'Archive','Rename','Copy','Cut','Dellete')
	   end
        if budka == 'Open as' then
		 redraw()
		 local open = windows.select({"Open "..files[I]},{"Open as"},{"x",colors.white,colors.red},{"Text",colors.lightBlue,colors.white},{"Program",colors.lightBlue,colors.white})
		 if open == 'Text' then
		  shell.run("/.ds/"..l_login.."/FileBrowser.apk/Resources/reader "..path.."/"..files[I])
		 elseif open == 'Program' then
		  clear(colors.white)
          sysbar.header(files[I],mainTextCol,mainColor)
	 	  term.setBackgroundColor(colors.white)
	 	  term.setTextColor(colors.black)
          term.setCursorPos(1,3)
          shell.run(path.."/"..files[I])
		  sysbar.header(files[I],mainTextCol,mainColor)
		  paintutils.drawLine(1,ySize,xSize,ySize,colors.white)
		  term.setTextColor(colors.lightGray)
		  text('Press any key...',1,ySize)
          waitForAny()
		 elseif open == 'Image' then
		  shell.run("/.ds/"..l_login.."/ImageViewer.apk/main "..path.." "..files[I])
		 end
		elseif budka == 'Unarchive' then
		 redraw()
		 local unzipPath = windows.input('auto','auto','Unarchive',20,{'Path', ''})
		 if unzipPath[1] ~= '' then
		  shell.run('mkdir '..unzipPath[1])
		  zip.unarchive(path..'/'..files[I],unzipPath[1])
		 elseif unzipPath[1] == '' then
		  zip.unarchive(path..'/'..files[I],path)
		 end
		 redraw()
		elseif budka == 'Archive' then
		 redraw()
		 local zipName = windows.input('auto','auto','Archive',20,{'Name', ''})
		 if zipName[1] ~= '' then
		  zip.archive(path..'/'..files[I],path..'/'..zipName[1])
		 elseif zipName[1] == '' then
		  zip.archive(path..'/'..files[I],path..'/'..files[I])
		 end
		 redraw()
		elseif budka == 'Rename' then
		 redraw()
		 local newName = windows.input('auto','auto','Rename',20,{'New name', files[I]})
		 if newName[1] ~= '' then
		  shell.run('rename '..path..'/'..files[I]..' '..path..'/'..newName[1])
		 end
		 redraw()
		elseif budka == 'Edit' then
		 shell.run("edit "..path.."/"..files[I])
		elseif budka == 'Copy' then
		 select=2
		 selects[1] = files[I]
         corpath = path
         redraw()
		elseif budka == 'Cut' then
		 select=3
		 selects[1] = files[I]
         corpath = path
         redraw()
		elseif budka == 'Dellete' then
		 redraw()
		 local but2 = windows.select({"Delete files"},{"Are you sure?"},{"Yes",colors.red,colors.white},{"No",colors.gray,colors.white})
         if but2 == "Yes" then
		  shell.run("rm "..path.."/"..files[I])
		 end
		end
       redraw()
       break
      end
	 end
	end
   end
  elseif event == "mouse_scroll" and contex==false then
   if #files>4 then
    if side == 1 then
     if pos>-#files*4+15 then
      posScroll = posScroll-step
      pos = pos-2
     else
      pos = -#files*4+15
     end 
    elseif side == -1 then
     if pos<0 then 
      pos = pos+2
      posScroll = posScroll+step
     else
      pos = 0
     end
    end
    redraw()
   end
  elseif event == "key" then
   if side == 208 then
    if pos>-#files*4+16 then
     posScroll = posScroll-step
     pos = pos-2
    else
     pos = -#files*4+16
    end 
   elseif side == 200 then
    if pos<0 then 
     pos = pos+2
     posScroll = posScroll+step
    else
     pos = 0
    end
   end
   redraw()
  end
 end
end

redraw()
running()
fadeOut(0)