os.loadAPI("/System/API/image")
os.loadAPI("/System/API/windows")
os.loadAPI("/rom/apis/gps")
os.loadAPI("/System/API/sysbar")

local pastebin = function(paste,filename)
 local file = http.get("https://pastebin.com/raw/"..paste)
 if file then
  file = file.readAll()
  h=fs.open(filename,"w")
  h.write(file)
  h.close()
 else
  windows.error("Unable to connect to the Internet")
 end
end

local find = false
local file = fs.list("/System/accounts")
for I=1,#file do
 if file[I] == l_login then find = true end
end
if find == false and l_login ~= '...' then
  local conf = fs.open("/System/accounts/"..l_login,"w")
  conf.writeLine('autoPassEnter = 0 autoLogin = 0')
  conf.close()
elseif l_login ~= '...' and log == true then
  pastebin("AH3k8DMa","/ver")
  shell.run("/ver")
  shell.run("/System/accounts/"..l_login)
end
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 = 3 
local check = -4
find = true
local p_login = ""
local p_password = ""
local pword = ""
local ico = false
local back = false
local pull = false
shell.run("mkdir /System/accounts")
file = fs.list("System/accounts")
local autopass = autoPassEnter
local autolog = autoLogin

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(" < Security",mainTextCol,mainColor)
		sysbar.draw(colors.white,colors.black,noty,true,false)
		sleep(1)
	end
end

local redraw = function()
 clear(colors.white)
 term.setTextColor(colors.black)
 if #file == 0 then 
  find = false 
 elseif #file > 0 then
  find = true
 end
 if find == false and log == false then
  clear(colors.white)
  term.setTextColor(colors.gray)
  text("New account",3,4)
  term.setTextColor(colors.black)
  text("Username:",6,6)
  text("Password:",6,8)
  paintutils.drawLine(15,6,xSize-5,6,colors.lightGray)
  paintutils.drawLine(15,8,xSize-5,8,colors.lightGray)
  term.setTextColor(colors.white)
  text(p_login,6+(xCenter-#p_login/2),6)
  text(pword,6+(xCenter-#p_password/2),8)
  if p_login~="" and p_password~="" then
   term.setBackgroundColor(colors.lime)
   term.setTextColor(colors.white)
   text(" Create ",xCenter-3,10)
  end
 elseif find == true and log == false then
  clear(colors.white)
  term.setTextColor(colors.gray)
  text("Enter account",3,4)
  term.setTextColor(colors.black)
  text("Username:",6,6)
  text("Password:",6,8)
  paintutils.drawLine(15,6,xSize-5,6,colors.lightGray)
  paintutils.drawLine(15,8,xSize-5,8,colors.lightGray)
  term.setTextColor(colors.white)
  if l_login~=nil then
   text(l_login,xCenter-#l_login/2+6,6)
  end
  text(pword,xCenter-#p_password/2+6,8)
  text("v",xSize-5,6)
  if l_login~="" and p_password~="" then
   term.setBackgroundColor(colors.lime)
   term.setTextColor(colors.white)
   text(" Log In ",xCenter-3,10)
  end
 elseif log == true then
  clear(colors.white)
  term.setTextColor(colors.gray)
  text("Account settings",3,4)
  local icon = fs.list("/System")
  for i=1,#icon do
   if icon[i]==l_login..".png" then
    image.draw(3,6,"/System/"..l_login..".png")
    ico = true
   end
  end
  if ico == false then
   image.draw(3,6,"/System/acc.png")
  end
  ico = false
  term.setTextColor(colors.black)
  term.setBackgroundColor(colors.white)
  text("Auto enter",3,12)
  text("Auto login",3,14)
  term.setTextColor(colors.gray)
  text("Exit to CraftOS",3,16)
  term.setBackgroundColor(colors.red)
  term.setTextColor(colors.white)
  text(" Delete ",12,8)
  term.setBackgroundColor(colors.lightGray)
  text(" Logout ",12,6)
  sysbar.switch(12,autoPassEnter,mainSwitchCol)
  sysbar.switch(14,autoLogin,mainSwitchCol)
 end
end

local pullEvent = function()
	local event, side, x, y = os.pullEventRaw()
	if event == "terminate" then
		windows.error('You can`t use "Ctrl+T"')
		redraw()
	else
		return event, side, x, y
	end
end 

local running = function()
 while run do
  event, side, x, y = pullEvent()
  if event == "mouse_click" then
   if log == false then
    if x==xSize-5 and y==6 then
     if pull == true then
      pull = false
      redraw()
     elseif pull == false then
      file = fs.list("/System/accounts")
      for i=1,#file do
       paintutils.drawLine(15,6,xSize-5,6,colors.lightGray)
       term.setTextColor(colors.white)
       text("^",xSize-5,6)
       text("...",xCenter-3/2+6,6)
       paintutils.drawLine(15,6+i,xSize-5,6+i,colors.lightGray)
       paintutils.drawLine(15,7+i,xSize-5,7+i,colors.lightGray)
       text(file[i],xCenter-#file[i]/2+6,6+i)
       term.setTextColor(colors.gray)
       text("New",xCenter-3/2+6,7+i)
      end
      pull = true
     end
    elseif y>6 and y-6<=#file and pull==true then
     l_login = file[y-6]
     shell.run("/System/accounts/"..file[y-6])
     pull = false
     redraw()
    elseif y-6==#file+1 and pull==true then
     l_login = "$test"
     file = {}
     find = false
     pull = false
     redraw()
    elseif y==6 and find==false then
     paintutils.drawLine(6,6,xSize-5,6,colors.lightGray)
	 term.setTextColor(colors.white)
     term.setCursorPos(6,6)
     p_login = read()
     redraw()
    elseif y==8 and pull==false then
     paintutils.drawLine(6,8,xSize-5,8,colors.lightGray)
     term.setCursorPos(6,8)
	 term.setTextColor(colors.white)
     p_password = read("o")
     pword=""
     for i=1, #p_password do
      pword = pword.."o"
     end
     redraw()
    elseif y==10 and ((p_login~="" or l_login~="") and l_login ~= '$test') and p_password~="" then
     if find==false then
      local f = fs.open("/System/accounts/"..p_login,"w")
      local conf = fs.open("/System/accounts/"..p_login,"w")
       conf.writeLine('autoPassEnter = 0 autoLogin = 0 password = "'..p_password..'"')
       conf.close()
      local conf = fs.open("/System/accounts/"..p_login,"a")
       conf.writeLine('style = colors.lightBlue')
       conf.writeLine('colored = 0')
       conf.close()
      p_password = ""
      pword = ""
	  fs.makeDir('/.ds/'..p_login)
      file = fs.list("System/accounts")
     else
      if p_password == password then
       log = true
      else
       paintutils.drawLine(6,7,xSize-5,7,colors.red)
       term.setTextColor(colors.white)
       text("Wrong password",xCenter-6,7)
       sleep(1)
      end
     end
     redraw()
    elseif 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
    end
   elseif log == true then
    if y==6 and x>=12 and x<=21 then
     log = false
     p_password = ""
     pword = ""
	 password = ""
     l_login = "$test"
	 style = colors.lightBlue
	 colored = 0
	 mainColor = colors.lightGray
	 mainTextCol = colors.gray
	 mainSwitchCol = colors.lightBlue
     redraw()
    elseif y==8 and x>=12 and x<=21 then
	 local but = windows.select({"Delete account"},{"Are you sure?"},{"Yes",colors.red,colors.white},{"No",colors.gray,colors.white})
     if but == "Yes" then
      shell.run("rm /System/accounts/"..l_login)
	  shell.run("rm /System/desks/"..l_login)
	  shell.run('rm /.ds/'..l_login)
      log = false
      p_password = ""
      pword = ""
	  password = ""
      l_login = "$test"
	  style = colors.lightBlue
	  colored = 0
	  mainColor = colors.lightGray
	  mainTextCol = colors.gray
	  mainSwitchCol = colors.lightBlue
	 end
     redraw()
    elseif sysbar.back(x,y) or y==1 then
     clear(colors.black)
     term.setCursorPos(1,1)
     run = false
    elseif sysbar.home(x,y) then
     term.clear()
     term.setCursorPos(1,1)
	 home = true
     run = false
    elseif x>=1 and x<=xSize and y==12 and autoPassEnter == 0 then
     autoPassEnter = 1
     redraw()
    elseif x>=1 and x<=xSize and y==12 and autoPassEnter == 1 then
     autoPassEnter = 0 
     redraw()
    elseif x>=1 and x<=xSize and y==14 and autoLogin == 0 then
     autoLogin = 1
     redraw()
    elseif x>=1 and x<=xSize and y==14 and autoLogin == 1 then
     autoLogin = 0
     redraw()
	elseif y==16 then
     local but = windows.select({"Exit to CraftOS"},{"Are you sure?"},{"Yes",colors.red,colors.white},{"No",colors.gray,colors.white})
     if but == "Yes" then
	  osexit = true
      clear(colors.black)
      term.setCursorPos(1,1)
	  home = true
      run = false
	  break
     elseif but == "No" then
	  redraw()
	 end
    end
   end
  end
 end
end

redraw()
parallel.waitForAny(sysBar,running)
if l_login ~= '...' and l_login ~= '$test' and log == true then
  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()
 elseif log == false then
  l_login = "$test"
  style = colors.lightBlue
  colored = 0
  mainColor = colors.lightGray
  mainTextCol = colors.gray
  mainSwitchCol = colors.lightBlue
  shell.run('mkdir /System/desks')
  local file = fs.open('/System/desks/'..l_login,"w")
  file.write('{}')
  file.close()
end