boucleConnect = true
boucle = true
boucleWait = true
mode = "net"
slc = 0

os.loadAPI("GPnet/net")

function Updater()
fs.delete("GPB/fond")
shell.run("pastebin","get","C0rGzPP3","GPB/fond")
end

function Barre()
barreIMG = paintutils.loadImage("GPB/fond")
paintutils.drawImage(barreIMG,1,1)
term.setCursorPos(1,1)
term.setBackgroundColor(colors.blue)
term.setTextColor(colors.white)
write("                  G-Power Browser               ")
term.setBackgroundColor(colors.red)
print(" X ")
term.setCursorPos(1,2)
term.setBackgroundColor(colors.lightGray)
write(" Opt ")
term.setBackgroundColor(colors.blue)
print(" AR:")
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
term.setCursorPos(11,2)
print(inputCible)
term.setTextColor(colors.white)
term.setBackgroundColor(colors.black)
end

function Fond()
fd1 = paintutils.loadImage("GPnet/fond")
paintutils.drawImage(fd1,1,4)
end

function NoConnect()
term.setCursorPos(10,8)
term.setBackgroundColor(colors.blue)
print("        Pas de connexion         ")
term.setCursorPos(10,9)
term.setBackgroundColor(colors.lightGray)
print("                                 ")
term.setCursorPos(10,10)
print(" Veuillez connecter l'ordinateur ")
term.setCursorPos(10,11)
print(" au reseau via un modem filaire. ")
term.setCursorPos(10,12)
print("                                 ")
end

function Connexion()
while boucleConnect do
net.connectModem(1)
file = fs.open("GPnet/CacheConnect","r")
connect = file.readAll()
file.close()
if connect == "true" then
  boucleConnect = false
else NoConnect()
  sleep(1)
end
end
end

function limitRead(nLength, cReplaceChar)
term.setCursorBlink(true)
nLegth = nLength or -1
sReturnString = ""
xPos, yPos = term.getCursorPos()

while true do
event, char = os.pullEvent()
if nLength ~= -1 and string.len(sReturnString) >= nLength then term.setCursorBlink(false); return sReturnString end
if event == "char" then sReturnString = sReturnString .. char
elseif event == "key" and char == 28 then term.setCursorBlink(false); return sReturnString
elseif event == "key" and char == 14 then
  term.setCursorPos(xPos,yPos)
  term.write(string.rep(" ", string.len(sReturnString)))
  sReturnString = string.sub(sReturnString, 1, string.len(sReturnString)-1)
  term.setCursorPos(xPos,yPos)
  if not cReplaceChar then term.write(sReturnString)
  else term.write(string.rep(cReplaceChar, string.len(sReturnString))) end
end
term.setCursorPos(xPos,yPos)
term.write(string.rep(" ", string.len(sReturnString)))
term.setCursorPos(xPos,yPos)
if not cReplaceChar then term.write(sReturnString)
else term.write(string.rep(cReplaceChar, string.len(sReturnString))) end
end
end

function MenuOpt()
term.setCursorPos(1,3)
term.setBackgroundColor(colors.lightGray)
term.setTextColor(colors.white)
print("     ")
Y = 4
for i = 1, 16 do
term.setCursorPos(1,Y)
term.clearLine()
Y = Y + 1
end
term.setCursorPos(22,6)
print("Options")
if mode == "net" then
  term.setCursorPos(11,11)
  print("Activer le mode communication")
elseif mode == "tel" then
  term.setCursorPos(14,11)
  print("Activer le mode internet")
end
term.setCursorPos(50,5)
print("X")
term.setBackgroundColor(colors.black)
end

function TimeOut()
Fond()
term.setCursorPos(5,7)
term.setBackgroundColor(colors.blue)
term.setTextColor(colors.white)
print("                Erreur 01                 ")
term.setCursorPos(5,8)
term.setBackgroundColor(colors.lightGray)
print("                                          ")
term.setCursorPos(5,9)
print(" Il se peut que :                         ")
term.setCursorPos(5,10)
print(" - vous ne soyez pas connecte a Internet, ")
term.setCursorPos(5,11)
print(" - l'adresse demandee soit incorrecte,    ")
term.setCursorPos(5,12)
print(" - le serveur recherche ne reponde pas.   ")
term.setCursorPos(5,13)
print("                                          ")
term.setCursorPos(5,14)
write("                   ")
term.setBackgroundColor(colors.blue)
write(" OK ")
term.setBackgroundColor(colors.lightGray)
term.setTextColour(colors.black)
write("                   ")
end

Updater()
Barre()
Fond()
Connexion()

function main()
while boucle do
local event, button, X, Y = os.pullEventRaw()
if event == "mouse_click" then
  if button == 1 then
    if slc == 0 then
      if X >= 1 and X <= 5 and Y == 2 then
        MenuOpt()
        slc = 1
      elseif X >= 9 and X <= 46 and Y == 2 then
        term.setCursorPos(11,2)
        term.setBackgroundColor(colors.white)
        term.setTextColor(colors.black)
        print("                                    ")
        term.setCursorPos(11,2)
        inputCible = limitRead(35)
        term.setBackgroundColor(colors.black)
        term.setTextColor(colors.white)
        if inputCible ~= "" then
          net.transmitData(inputCible,"initConnect",1)
          reponse = net.waitResponding(3,1)
          boucleExe = true
          
          while boucleExe do
          boucleExe = false
                    
          if reponse == "time out" then
            slc = 2
            TimeOut()
          else
            execPage = loadstring(reponse)
            parallel.waitForAny(main, function()
                                      out = execPage()
                                      end)
            
            if string.find(out,"askPage") then
              boucleExe = true
              tableau = {}
              
              for sub_string in string.gmatch(out,'([^->]+)') do
              table.insert(tableau,sub_string)
              end
              
              reponse = net.askPage(tableau[2],tableau[3],tonumber(tableau[4]))
            elseif out == 0 then
              boucleExe = false
            end
          end
          end
        end
      elseif X >= 49 and X <= 51 and Y == 1 then
        term.setBackgroundColor(colors.black)
        term.setTextColor(colors.white)
        term.clear()
        term.setCursorPos(1,1)
        boucle = false
        inputCible = nil
        func = nil
        net.disconnect()
        error()
      end
    elseif slc == 1 then
      if mode == "net" then
        if X >= 11 and X <= 39 and Y == 11 then
          mode = "tel"
          MenuOpt()
        elseif X == 50 and Y == 5 then
          Barre()
          Fond()
          slc = 0
        elseif (X >= 6 and Y == 2 or Y == 3) or (X >= 1 and Y == 1) then
          Barre()
          Fond()
          slc = 0
        end
      elseif mode == "tel" then
        if X >= 14 and X <= 37 and Y == 11 then
          mode = "net"
          MenuOpt()
        elseif X == 50 and Y == 5 then
          Barre()
          Fond()
          slc = 0
        elseif (X >= 6 and Y == 2 or Y == 3) or (X >= 1 and Y == 1) then
          Barre()
          Fond()
          slc = 0
        end
      end
    elseif slc == 2 then
      if X >= 24 and X <= 27 and Y == 14 then
        Barre()
        Fond()
        slc = 0
      end
    end
  end
end
end
end

main()
