-- This is the first time setup so the computer will
-- Download all the required scripts.
local args = { ... }
running = true
elements = {}
-- Checking System Support
if not http then
  error "You Must Enable HTTP In Computer-craft Con-figs To Use The System!"
end

if not term.isColor() then
  error "This Computer Does Not Appear To Be Advanced, If It Is Advanced, Then Please Report This"
end

function create(text, width, height, x, y, tc, bg, otc, obg, func, toggle, secBG, secTC, secText, parent) --Return the new button so it can be called later to be drawn
	local new = {} --New Table To Store Details
	setmetatable( new, {__index = self} ) --New Metatable To Set Index To Self
	new.text = text or 'Nil Text' --Button Text
	new.width = width or #text + 2	--Button width
	if secText then new.secWidth = #secText + 2 else new.secWidth = #text + 2 end
	new.height = height or 1 --Button Height
	new.pad = pad or 2 --Padding Variable
	new.x = x or 1 --Button X Positioning
	new.y = y or 1 --Button Y Positioning
	new.tc = tc or colors.white --Button Text Color
	new.bg = bg or 128 --Button Background Color
	new.otc = otc or colors.white --Original Background Color
	new.obg = obg or 256 --Orignial Text Color
	new.func = func or nil --Function on click
	new.toggle = toggle or false --Whether the button should change state on click
	new.secondaryBackground = secBG	or bg --Toggle Background Color
	new.secondaryTextColor = secTC or tc --Toggle Text Color
	new.toggleState = 1 --Set toggle state to default
	new.secondaryText = secText or text
	new.visible = true
	new.parent = parent or 'Nil'
	return new
end

function tryClick(elementTable, x, y) --On Click Check Co-Ords, If match run the function assigned to the button
	if elementTable then
		for i, v in ipairs(elementTable) do
			if x >= v.x and x < v.x + v.width then
				if y == v.y then
					if v.visible then 
						if v.toggle then element.toggle(v) end
						if v.parent then
							for i, p in ipairs(elementTable) do
								if p.parent == v.parent then
									if p ~= v then p.toggleState = 1 else p.toggleState = 2 end
									draw(p)
								end
							end
						end
						if v.func then return(v.func) end
					end
				end
			end
		end
	end
end

function eraseCurrent(elem)
	if elem then
		if elem.toggleState == 2 then textSpec = elem.secondaryText widthSpec = elem.secWidth else textSpec = elem.text widthSpec = elem.width end 
		term.setCursorPos(elem.x, elem.y)
		term.setBackgroundColor(elem.obg)
		if elem.width and elem.secWidth then
			if elem.toggleState == 1 then
				write(string.rep(' ', elem.secWidth ))
			elseif elem.toggleState == 2 then
				write(string.rep(' ', elem.width ))
			end	
		end
	end
end

function draw(elem) --Visually Draw The Button Provided
	if elem then eraseCurrent(elem) end
	if elem.visible then
		if elem.toggleState == 2 then term.setTextColor(elem.secondaryTextColor) else term.setTextColor(elem.tc) end
		if elem.toggleState == 2 then term.setBackgroundColor(elem.secondaryBackground) else term.setBackgroundColor(elem.bg) end
		term.setCursorPos(elem.x, elem.y)
		write(string.rep(' ', elem.width )) --Create The Background Panel
		if elem.toggleState == 2 then textSpec = elem.secondaryText widthSpec = elem.secWidth else textSpec = elem.text widthSpec = elem.width end 
		term.setCursorPos(elem.x + (widthSpec - #textSpec)/2, elem.y) --Set The Cursor Pos In The Correct Padding Pos So The Text Appears In The Correct Location
		write(textSpec)--Print The Button Text On The Button
	else
		term.setTextColor(elem.otc)
		term.setBackgroundColor(elem.obg)
		term.setCursorPos(elem.x, elem.y)
		write(string.rep(' ', elem.width )) --Create The Background Panel
		term.setCursorPos(elem.x, elem.y) --Set The Cursor Pos In The Correct Padding Pos So The Text Appears In The Correct Location
	end
	term.setTextColor(elem.otc)
	term.setBackgroundColor(elem.obg)
end

function toggle(elem)
	if elem.toggle then
		if elem.toggleState == 1 then elem.toggleState = 2
		elseif elem.toggleState == 2 then elem.toggleState = 1 end
		draw(elem)
	end
end

function opacity(elem, state) --Set or toggle visibility of a button, if invisible the BG color will be what you assigned when creating the button
	if elem then
		if state ~= 't' then --Manual Adjust
			elem.visible = state
		else --Toggle Auto
			if elem.visible == true then elem.visible = false else elem.visible = true end
		end
		draw(elem)
	end
end

function btnInit(btnText, btnWidth, btnHeight, btnX, btnY, btnTC, btnBG, oTC, oBG, onClick, toggle, secBG, secTC, secText) --Function to create button
	local btn = create(btnText, btnWidth, btnHeight, btnX, btnY, btnTC, btnBG, oTC, oBG, onClick, toggle, secBG, secTC, secText) --Calls API to generate button
	table.insert(elements, btn) --Inserts into table so it can be scanned later
	opacity(btn, true) --Sets visibility to true
	return btn
end

function doClick(event, btn, x, y)
	functionToRun = tryClick(elements, x, y)
	if functionToRun then --Check click location
		functionToRun()
	end
end

function PrintCentered(text, y)
    term.setTextColor(1)
	local w, h = term.getSize()
    x = math.ceil(math.ceil((w / 2) - (#text / 2)), 0)+1
    term.setCursorPos(x, y)
	term.clearLine()
    write(text)
end

function initBackground(color) --Draw The Background In The Specified Color
  term.setBackgroundColor(color)
  term.clear()
end

function drawTitleBar()
  term.setBackgroundColor(128)
  term.setCursorPos(1,1)
  term.clearLine()
  term.setTextColor(colors.cyan)
  write "HbombOS Security Solutions"
  term.setCursorPos(1,2)
  term.clearLine()
  term.setTextColor(256)
  write "Installer"
  term.setTextColor(1)
end
initBackground(256)
drawTitleBar()

local function checkSite()
  local response = http.get("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/version")
  if response then
    return true
  else
    return false
  end
end

termX, termY = term.getSize()

function start()
  if args[1] == 'auto' then
	  return true
  else
	  term.setBackgroundColor(256)
	  PrintCentered('Welcome To The', 7)
	  PrintCentered('Hbomb Security Suite', 8)
	  PrintCentered('Install Wizzard', 9)
	  PrintCentered('BEWARE',13)
	  PrintCentered('All Files Already In Destination Will Be',14)
	  PrintCentered('Overwritten',15)	
	  yes = btnInit('Install', nil, nil, termX-#'Install'-2, termY-1, 1, colors.green, 1, 256, function() os.queueEvent('continue') end, false, nil, nil, nil, nil)
	  no = btnInit('Dont Install', nil, nil, 2, termY-1, 1, colors.red, 1, 256, function() os.reboot() end, false, nil, nil, nil, nil)
	  while true do
		event, btn, x, y = os.pullEvent()
		if event == "mouse_click" then
			doClick(event, btn, x, y)
		elseif event == "continue" then
			break
		end
	  end
	  return true
  end
end

function open()
start()
opacity(yes ,false)
opacity(no ,false)
term.clear()
drawTitleBar()
term.setBackgroundColor(256)
term.setTextColor(1)
  PrintCentered("Starting Download", 6)
  PrintCentered("Establishing Connection With Github", 8)
  PrintCentered("Reading GitHub Repository", 18)
  if not checkSite() then
    PrintCentered("The GitHub Version File Does Not", 18)
    PrintCentered("Appear To Be At The Destination URL", 19)
	term.setTextColor(colors.red)
	PrintCentered("The Installer Has Encountered An Issue!", 2)
	return false
  end
  PrintCentered("Connection With Github Established", 8)
  PrintCentered("GitHub Repository Located", 18)
  sleep(1)
  PrintCentered("GitHub Repository Connection Established", 18)
  sleep(0)
  PrintCentered("Downloading And Installing Files", 8)
  PrintCentered("Starting Download Sequence", 18)
  sleep(0.1)
  return true
end

function downloadFiles(getUrl, toPath)
term.setTextColor(1)
term.setBackgroundColor(256)
-- Download the files and scripts from github
  for i = 1, 3 do
    local response = http.get(getUrl)
	if response then
	  data = response.readAll()
	      if fs.exists(toPath) then
		  fs.delete(toPath)
		  PrintCentered ("Delete: "..toPath, 19)
		end
		if toPath then
		  if fs.exists(toPath) then fs.delete(toPath) end
		  local file = fs.open(toPath, "w")
		  file.write(data)
		  file.close()
		  PrintCentered ("Download: "..toPath, 19)
		  return true
		else
		  print "We Believe The Path Specified Is Invalid, Report If Not Your Fault"
		  sleep(3)
		  os.shutdown()
		end
	else
	  print ("The File Or Files At: "..getUrl.." Do Not Appear To Exist! Please Report This Issue On Forums Or Through GitHub")
	  sleep(3)
	  os.shutdown()
	end
  end
  print ("Failed To Download The File From URL: "..getUrl.." Please try again later, If this is the 2nd time you've seen this, then report it on the fourms")
  sleep(3)
  os.shutdown()
end

function createDirectory() --Used to create directories for version 1.5 and below
  fs.makeDir('/systemFiles/')
  fs.makeDir('/systemFiles/Images/BootLogos/')
  fs.makeDir('/systemFiles/Images/Update/')
  fs.makeDir('/systemFiles/Images/progressBar/')
  fs.makeDir('/systemFiles/Install/')
  fs.makeDir('/api/')
  fs.makeDir('/Documentation/')
  fs.makeDir('/systemFiles/Programs/')
  fs.makeDir('/systemFiles/Security/')
end

function thanks()
  term.clear()
  drawTitleBar()
  term.setBackgroundColor(256)
  term.setTextColor(1)
  PrintCentered ("Thank You For Downloading And Installing The", 6)
  PrintCentered ("Latest Version Of My Security Suite", 7)
  PrintCentered ("Report Any Issues You Come Across", 8)
  local f = fs.open('version', 'r')
  currVer = f.readLine()
  f.close()
  PrintCentered ("You Are Now Running The Latest Version: "..currVer, 10)
  PrintCentered ("Hope You Enjoy, Click Anywhere To Get Started", 19)
  if args[1] ~= 'auto' then os.pullEvent('mouse_click') else return end
  os.reboot()
end

function download() --Download Files From GitHub And Install Them On The Local Computer
term.setBackgroundColor(256)
PrintCentered ("Creating Directories... Please Wait", 19)
sleep(0)
createDirectory() --Creates The Directories To Stop The Program From Crashing
PrintCentered ("Downloading Scripts... Please Wait", 19)
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/startup", "startup")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/version", "version")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Install/installer", "systemFiles/Install/installer")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Install/updater", "systemFiles/Install/updater")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Install/setup", "systemFiles/Install/setup")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/BootLogos/boot0.nfp", "systemFiles/Images/BootLogos/boot0.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/BootLogos/boot1.nfp", "systemFiles/Images/BootLogos/boot1.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/BootLogos/boot2.nfp", "systemFiles/Images/BootLogos/boot2.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/BootLogos/boot3.nfp", "systemFiles/Images/BootLogos/boot3.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/BootLogos/boot4.nfp", "systemFiles/Images/BootLogos/boot4.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/BootLogos/boot5.nfp", "systemFiles/Images/BootLogos/boot5.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/BootLogos/boot6.nfp", "systemFiles/Images/BootLogos/boot6.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/BootLogos/boot7.nfp", "systemFiles/Images/BootLogos/boot7.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/Update/update1.nfp", "systemFiles/Images/Update/update1.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/Update/update2.nfp", "systemFiles/Images/Update/update2.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/Update/update3.nfp", "systemFiles/Images/Update/update3.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/progressBar/load0.nfp", "systemFiles/Images/progressBar/load0.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/progressBar/load1.nfp", "systemFiles/Images/progressBar/load1.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/progressBar/load2.nfp", "systemFiles/Images/progressBar/load2.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/progressBar/load3.nfp", "systemFiles/Images/progressBar/load3.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/progressBar/load4.nfp", "systemFiles/Images/progressBar/load4.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/progressBar/load5.nfp", "systemFiles/Images/progressBar/load5.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/progressBar/load6.nfp", "systemFiles/Images/progressBar/load6.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/progressBar/load7.nfp", "systemFiles/Images/progressBar/load7.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Images/progressBar/load8.nfp", "systemFiles/Images/progressBar/load8.nfp")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/api/download", "/api/download")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/api/systemCheck", "/api/systemCheck")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/api/update", "/api/update")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/api/printer", "/api/printer")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/api/titleBar", "/api/titleBar")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/api/LogFile", "/api/LogFile")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/api/element", "/api/element")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/api/uInput", "/api/uInput")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/api/errora", "/api/errora")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Programs/dualKey", "/systemFiles/Programs/dualKey")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Programs/keycard", "/systemFiles/Programs/keycard")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Programs/keycardDual", "/systemFiles/Programs/keycardDual")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Programs/pin", "/systemFiles/Programs/pin")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Programs/rangeLock", "/systemFiles/Programs/rangeLock")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Programs/reactor", "/systemFiles/Programs/reactor")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Programs/OpenPDetect", "/systemFiles/Programs/OpenPDetect")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/systemFiles/Programs/remoteControl.lua", "/systemFiles/Programs/remoteControl.lua")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/README", "/Documentation/README")
downloadFiles("https://raw.githubusercontent.com/hbomb79/securitySystemPro/master/LICENSE", "/Documentation/LICENSE")
PrintCentered("Complete", 19)
thanks()
return
end

if open() then 
  download()
  return
else
  sleep(3)
  os.reboot()
end