os.loadAPI("/System/API/image")
os.loadAPI("/System/API/windows")
os.loadAPI("/rom/apis/gps")
os.loadAPI("/System/apis/sysbar")
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 screen = window.create(term.current(),1,3,xSize,ySize-3)
local draw = true

function findModem()
	local PeriList = peripheral.getNames()
	for i=1,#PeriList do
		if peripheral.getType(PeriList[i]) == "modem" then
			return PeriList[i]
		end
	end
end

local computerModem = findModem()

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
 
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()
	while true do
		if draw then
			sysbar.header("Settings",mainTextCol,mainColor)
			sysbar.draw(colors.white,colors.black,noty,true,false)
		end
		sleep(1)
	end
end
 
local menu = function(...)
 term.setBackgroundColor(colors.white)
 term.setTextColor(colors.black)
 local cursor = 4
 for i=1,#arg do
  text(arg[i],2,cursor-2)
  newObj(arg[i],2,cursor,xSize,cursor)
  cursor=cursor+2
 end
end
 
local redraw = function()
 term.redirect(screen)
 clear(colors.white)
 menu("RedNet","Applications","Customization","Security","Storage","Update","About")
 paintutils.drawLine(xSize-7,2,xSize-2,2,colors.gray)
 term.setTextColor(colors.white)
 if wifi == "off" then
  paintutils.drawLine(xSize-7,2,xSize-5,2,colors.lightGray)
 elseif wifi == "on" then
  paintutils.drawLine(xSize-4,2,xSize-2,2,mainSwitchCol)
 end
 term.redirect(term.native())
end
 

local running = function()
 while run == true and home == false do
  event, side, x, y = os.pullEvent()
  if event == "mouse_click" then
   draw = false
   if sysbar.back(x,y) == true then
    term.clear()
    term.setCursorPos(1,1)
    run = false
   elseif sysbar.home(x,y) == true then
    term.clear()
    term.setCursorPos(1,1)
    run = false
   elseif x>=1 and x<=xSize and y==4 and wifi=="off" then
	if computerModem ~= nil then
	 rednet.open(computerModem)
	 wifi = "on"
	end
    redraw()
   elseif x>=1 and x<=xSize and y==4 and wifi=="on" then
	if computerModem ~= nil then
	 rednet.close(computerModem)
	 wifi = "off"
	end
    redraw()
   elseif x>=obj["About"]["x1"] and x<=obj["About"]["x2"] and y>=obj["About"]["y1"] and y<=obj["About"]["y2"] then
    paintutils.drawLine(1,obj["About"]["y1"],xSize,obj["About"]["y2"],colors.lightGray)
	term.setTextColor(colors.white)
	text('About',2,obj["About"]["y1"])
    shell.run("/System/prog/about")
    redraw()
   elseif x>=obj["Applications"]["x1"] and x<=obj["Applications"]["x2"] and y>=obj["Applications"]["y1"] and y<=obj["Applications"]["y2"] then
    paintutils.drawLine(1,obj["Applications"]["y1"],xSize,obj["Applications"]["y2"],colors.lightGray)
	term.setTextColor(colors.white)
	text('Applications',2,obj["Applications"]["y1"])
    shell.run("/System/prog/applications")
    redraw()
   elseif x>=obj["Customization"]["x1"] and x<=obj["Customization"]["x2"] and y>=obj["Customization"]["y1"] and y<=obj["Customization"]["y2"] then
    paintutils.drawLine(1,obj["Customization"]["y1"],xSize,obj["Customization"]["y2"],colors.lightGray)
	term.setTextColor(colors.white)
	text('Customization',2,obj["Customization"]["y1"])
    shell.run("/System/prog/style")
    redraw()
   elseif x>=obj["Storage"]["x1"] and x<=obj["Storage"]["x2"] and y>=obj["Storage"]["y1"] and y<=obj["Storage"]["y2"] then
    paintutils.drawLine(1,obj["Storage"]["y1"],xSize,obj["Storage"]["y2"],colors.lightGray)
	term.setTextColor(colors.white)
	text('Storage',2,obj["Storage"]["y1"])
    shell.run("/System/prog/storage")
    redraw()
   elseif x>=obj["Update"]["x1"] and x<=obj["Update"]["x2"] and y>=obj["Update"]["y1"] and y<=obj["Update"]["y2"] then
    paintutils.drawLine(1,obj["Update"]["y1"],xSize,obj["Update"]["y2"],colors.lightGray)
	term.setTextColor(colors.white)
	text('Update',2,obj["Update"]["y1"])
    shell.run("/System/prog/update")
    redraw()
   elseif x>=obj["Security"]["x1"] and x<=obj["Security"]["x2"] and y>=obj["Security"]["y1"] and y<=obj["Security"]["y2"] then
    paintutils.drawLine(1,obj["Security"]["y1"],xSize,obj["Security"]["y2"],colors.lightGray)
	term.setTextColor(colors.white)
	text('Security',2,obj["Security"]["y1"])
    shell.run("/System/prog/security")
    redraw()
   end
   sysbar.header("Settings",mainTextCol,mainColor)
   sysbar.draw(colors.white,colors.black,noty,true,false)
   draw = true
  end
 end
end

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