os.loadAPI("/System/API/image")
os.loadAPI("/System/API/windows")
os.loadAPI("/rom/apis/gps")
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
local p_login = ""
local p_password = ""
local pword = ""

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 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 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()
 clear(colors.lightGray)
 term.setTextColor(colors.gray)
 text(" Skip ",2,ySize-1)
 term.setTextColor(colors.white)
 text("Hello!",xCenter-2,3)
 text("Username:",4,13)
 text("Password:",4,15) 
 if p_login~="" and p_password~="" then 
  term.setBackgroundColor(colors.lime)
  text(" Create ",xSize-8,ySize-1)
 else
  term.setBackgroundColor(colors.gray)
  text(" Create ",xSize-8,ySize-1)
 end
 for i=1,4 do
  paintutils.drawLine(1,6+i,xSize,6+i,colors.white)
 end
 term.setTextColor(colors.black)
 text("Please, create",xCenter-6,8)
 text("your new account",xCenter-7,9)
 paintutils.drawLine(13,13,xSize-3,13,colors.white)
 paintutils.drawLine(13,15,xSize-3,15,colors.white)
 term.setTextColor(colors.black)
 text(p_login,6+(xCenter-#p_login/2),13)
 text(pword,6+(xCenter-#p_password/2),15)
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 x>=2 and x<=7 and y==ySize-1 then
    term.clear()
    clear(colors.white)
    term.setCursorPos(1,1)
    l_login = "..."
    run = false
   elseif y==13 then
    paintutils.drawLine(3,13,xSize-3,13,colors.white)
    term.setCursorPos(3,13)
    p_login = read()
    l_login = p_login
    redraw()
   elseif y==15 then
    paintutils.drawLine(3,15,xSize-3,15,colors.white)
    term.setCursorPos(3,15)
    p_password = read(string.char(7))
    pword=""
    for i=1, #p_password do
     pword = pword..string.char(7)
    end
    redraw()
   elseif x>=xSize-8 and x<=xSize-1 and y==ySize-1 and p_login~="" and p_password~="" 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()
    fs.makeDir('/.ds/'..p_login)
    fs.makeDir('/System/desks')
    local desk = {}
    desk[1] = {}
    desk[1][1] = {}
    desk[1][1]['name']='sys/Store@.apk'
    desk[1][1]['x']=3
    desk[1][1]['y']=ySize-6
    local file = fs.open('/System/desks/'..p_login,"w")
    file.write(textutils.serialize(desk))
    file.close()
    run=false
    os.reboot()
   end
  end
 end
end
redraw()
running()
clear(colors.white)
term.setCursorPos(1,1)