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 arg = {...}
local cursor = 2 
local check = -4
local step = 0
local posScroll = 0
local screen = window.create(term.current(),1,3,xSize-1,ySize-3)

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 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
		sysbar.header(" < Customisation",mainTextCol,mainColor)
		sysbar.draw(colors.white,colors.black,noty,true,false)
		sleep(1)
	end
end
 
local setStyle = function(color)
 style = color
end

local redraw = function()
 term.redirect(screen)
 clear(colors.white)
 term.setBackgroundColor(colors.white)
 term.setTextColor(colors.gray)
 text("Main color",3,cursor)
 term.setTextColor(colors.black)
 text("Red",6,cursor+2)
 text("Orange",6,cursor+4)
 text("Yellow",6,cursor+6)
 text("Lime",6,cursor+8)
 text("Green",6,cursor+10)
 text("Cyan",6,cursor+12)
 text("Blue",6,cursor+14)
 text("Light Blue",6,cursor+16)
 text("Light Gray",6,cursor+18)
 text("Purple",6,cursor+20)
 text("Colored Headers",3,cursor+22)
 term.setTextColor(colors.lightGray)
 text("( )",xSize-4,cursor+2)
 text("( )",xSize-4,cursor+4)
 text("( )",xSize-4,cursor+6)
 text("( )",xSize-4,cursor+8)
 text("( )",xSize-4,cursor+10)
 text("( )",xSize-4,cursor+12)
 text("( )",xSize-4,cursor+14)
 text("( )",xSize-4,cursor+16)
 text("( )",xSize-4,cursor+18)
 text("( )",xSize-4,cursor+20)
 term.setTextColor(mainColor)
 if style == colors.red then check = 2
 elseif style == colors.orange then check = 4
 elseif style == colors.yellow then check = 6
 elseif style == colors.lime then check = 8
 elseif style == colors.green then check = 10
 elseif style == colors.cyan then check = 12
 elseif style == colors.blue then check = 14
 elseif style == colors.lightBlue then check = 16
 elseif style == colors.lightGray then check = 18
 elseif style == colors.purple then check = 20
 end
 text(string.char(7),xSize-3,cursor+check)
 paintutils.drawLine(3,cursor+2,4,cursor+2,colors.red)
 paintutils.drawLine(3,cursor+4,4,cursor+4,colors.orange)
 paintutils.drawLine(3,cursor+6,4,cursor+6,colors.yellow)
 paintutils.drawLine(3,cursor+8,4,cursor+8,colors.lime)
 paintutils.drawLine(3,cursor+10,4,cursor+10,colors.green)
 paintutils.drawLine(3,cursor+12,4,cursor+12,colors.cyan)
 paintutils.drawLine(3,cursor+14,4,cursor+14,colors.blue)
 paintutils.drawLine(3,cursor+16,4,cursor+16,colors.lightBlue)
 paintutils.drawLine(3,cursor+18,4,cursor+18,colors.lightGray)
 paintutils.drawLine(3,cursor+20,4,cursor+20,colors.purple)
 sysbar.switch(cursor+22,colored,mainSwitchCol)
 term.setBackgroundColor(colors.white)
 if wallp then
  term.setTextColor(colors.red)
 else
  term.setTextColor(colors.lightGray)
 end
 text("Remove Wallpapers",3,cursor+24)
 term.redirect(term.native())
 step = sysbar.scrollBar(13,2.7,posScroll,2)
end
 

local running = function()
 while run and l_login ~= '$test' do
  event, side, x, y = os.pullEvent()
  if 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
   elseif y==cursor+4 then
    setStyle(colors.red)
   elseif y==cursor+6 then
    setStyle(colors.orange)
   elseif y==cursor+8 then
    setStyle(colors.yellow)
   elseif y==cursor+10 then
    setStyle(colors.lime)
   elseif y==cursor+12 then
    setStyle(colors.green)
   elseif y==cursor+14 then
    setStyle(colors.cyan)
   elseif y==cursor+16 then
    setStyle(colors.blue)
   elseif y==cursor+18 then
    setStyle(colors.lightBlue)
   elseif y==cursor+20 then
    setStyle(colors.lightGray)
   elseif y==cursor+22 then
    setStyle(colors.purple)
   elseif y==cursor+24 and (colored == 0 or colored == nil) then
    colored = 1
	redraw()
   elseif y==cursor+24 and colored == 1 then
    colored = 0
	redraw()
   elseif y==cursor+26 and wallp then
    local but = windows.select({"Delete"},{"Are you sure?"},{"Yes",colors.red,colors.white},{"No",colors.gray,colors.white})
    if but == "Yes" then
     local f=fs.list("/system")
     for i=0,#f do
      if f[i] == "wallp.png" then
       shell.run("rm /System/wallp.png")
	   wallp = false
	   break
      end
     end
     redraw()
    elseif but == "No" then redraw()
    end
   end
   redraw()
  elseif event == "mouse_scroll" then
   if side == 1 then
    if cursor>(-9) then
     cursor = cursor-1
	 posScroll = posScroll - step
    end
   elseif side == -1 then
    if cursor<2 then
     cursor = cursor+1
	 posScroll = posScroll + step
    end
   end
   redraw()
  end
 end
end

clear(colors.white)
redraw()
parallel.waitForAny(sysBar,running)
  local conf = fs.open("/System/accounts/"..l_login,"w")
  conf.writeLine('autoPassEnter = '..autoPassEnter..' autoLogin = '..autoLogin..' password = "'..password..'"')
  conf.close()
  local conf = fs.open("/System/accounts/"..l_login,"a")
  conf.writeLine('style = '..style)
  conf.writeLine('colored = '..colored)
  conf.close()