--Alle Rechte an SimSoft 3 sind "SimlorLP" (oder in Minecraft auch nur "Simlor") vorbehalten.

local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
local datei = http.get("https://pastebin.com/raw/LZch2Xnt".. "?cb=" .. cacheBuster)
if not datei then
 BC(128)
 Clear()
 TC(16384)
 print("Please, check you internet connection!")
 print("Or try it later again.")
 sleep(3)
else

fs.delete("SimSoft/Daten/StoreTemp/Temp")
Download("MD0dmx9s", "SimSoft/Daten/StoreTemp/Temp")

BC(128)
CP(15,18)
print("Loading...")

function LiveSappSDar(nr)
 
 StoreCleaner()
 
 file = fs.open("SimSoft/Daten/StoreTemp/Temp","r")
 local fileData = {}
 local line = file.readLine()
 repeat
 table.insert(fileData,line)
 line = file.readLine()
 until line == nil
 file.close()
 
 Sname = fileData[nr]
 nr = (nr+1)
 Sautor = fileData[nr]
 nr = (nr+1)
 Ssterne = fileData[nr]
 nr = (nr+1)
 PBCodeS = fileData[nr]
 nr = (nr-3)
 
 CP(17,6) --17,6
 TC(1)
 print(Sname)
 
 CP(18,8)
 TC(256)
 print("Creator = "..Sautor)
 
 CP(18,10)
 TC(256)
 print("Stars = "..Ssterne)
 
 CP(18,13)
 TC(1)
 write("Download ")
 TC(256)
 print("("..PBCodeS..")")
 
 CP(14,18)
 TC(256)
 print("<")
 
 CP(50,18)
 TC(256)
 print(">")
 
 CP(27,18)
 TC(256)
 print(ggz.."/"..anzLS)
end
 
auswahl = 0

BC(128)
Clear()

SimSoftBalken("SimSoft 3 - Store", 17)

w, h = term.getSize()

CP(1,4)
BC(256)
TC(1)
print("            ")
write(" Store ")
TC(128)
print("BETA ")
print("            ")
TC(256)
BC(128)
print(" SappS-Code ")
TC(128)
BC(256)
print(" Live-Store ")
TC(256)
BC(128)
print(" Add a pro. ")
BC(256)

z = 9

while true do
 z = (z+1)
 CP(1,z)
 write("            ")
 if z == h then
  
  TC(128)
  z = (z-1)
  CP(2,z)
  print("Back")
  break
 end
end

--StoreCleaner

function StoreCleaner()
 BC(128)
 ySC = 3
 
 while true do
 
  ySC = (ySC+1)
  CP(13,ySC)
  write("                                       ")
  
  if ySC == 19 then
   break
  end
 end
end
 

--TouchMenu

function TouchMenu()

 while true do
  event,side,ex,ry = os.pullEvent()
  if event == "mouse_click" then
  
   if ex >= 1 and ex <= 12 and ry == 7 then
    auswahl = 1
   end
  
   if ex >= 1 and ex <= 12 and ry == 8 then
    auswahl = 2
   end
  
   if ex >= 1 and ex <= 12 and ry == 9 then
    auswahl = 3
   end
   
   if ex >= 2 and ex <= 5 and ry == z then
    auswahl = 4
   end
  end
 end
end

--Anzeige Functionen

 --Kontrolle0
function AnzeigeKontrolle0()
 while true do
  if auswahl == 0 then
   sleep(0.1)
  else
   break
  end
 end
end

 --Kontrolle1
function AnzeigeKontrolle1()
 while true do
  if auswahl == 1 then
   sleep(0.1)
  else
   break
  end
 end
end

 --Kontrolle2
function AnzeigeKontrolle2()
 while true do
  if auswahl == 2 then
   sleep(0.1)
  else
   break
  end
 end
end

 --Kontrolle3
function AnzeigeKontrolle3()
 while true do
  if auswahl == 3 then
   sleep(0.1)
  else
   break
  end
 end
end
   
   
--1 - SappS-Code
function AnzeigeStore1()
 
 while true do
  
  StoreCleaner()
  
  CP(17,6)
  BC(128)
  TC(1)
  print("Enter a SappS-Code")
  
  TC(256)
  
  CP(18,8)
  print("SappS-Code")
  
  CP(15,18)
  print("SappS-Code? simsoft-en.jimdo.com")
  
  while true do
  
   event,side,x,y = os.pullEvent()
   if event == "mouse_click" then
    
	if x >= 18 and x <= 27 and y == 8 then
	 CP(18,8)
	 write("                                       ")
	 CP(18,8)
	 TC(256)
	 sappscode = read()
	 
	 CP(18,10)
	 shell.run("pastebin run "..sappscode)
	 CP(1,11)
	 BC(256)
	 print("            ")
	 auswahl = 4
	 break	 
	end
   end
  end
 end  
end


--2 - Live-Store
function AnzeigeStore2()
 
 while true do
  StoreCleaner()
  
  BC(128)
  CP(15,18)
  print("Loading...")

  anzLS = 0
  
  file = fs.open("SimSoft/Daten/StoreTemp/Temp","r")
  local fileData = {}
  local line = file.readLine()
  repeat
  table.insert(fileData,line)
  line = file.readLine()
  until line == nil
  file.close()
  
  anzLS = fileData[1]
  
  anzLS = (anzLS+1)
  anzLS = (anzLS-1)
  
  sonderzahl = (anzLS*5-2)
  
  nr = 3
  ggz = 1
  
  if anzLS == 0 then
   CP(15,18)
   print("          ")
   CP(15,18)
   TC(16384)
   print("ERROR - Try later")
   
  elseif anzLS >= 1 then
   nr = 3
   ggz = 1
   LiveSappSDar(nr)
  end
  
  while true do
   event,side,tx,ty = os.pullEvent()
   if event == "mouse_click" then
    
	if tx == 14 and ty == 18 then
	 if nr >= 8 then
	  nr = (nr-5)
	  ggz = (ggz-1)
	  LiveSappSDar(nr)
	 elseif nr == 3 then
	  nr = sonderzahl
	  ggz = anzLS
	  LiveSappSDar(nr)
	 end
	end
	
	if tx == 50 and ty == 18 then
	 if nr < sonderzahl then
	  nr = (nr+5)
	  ggz = (ggz+1)
	  LiveSappSDar(nr)
	 elseif nr == sonderzahl then
	  nr = 3
	  ggz = 1
	  LiveSappSDar(nr)
	 end
	end
	
	if tx >= 18 and tx <= 25 and ty == 13 then
	 BC(128)
	 TC(32)
	 CP(15,14)
	 shell.run("pastebin run "..PBCodeS)
	 auswahl = 4
	 break
	end
   end	
  end 
 end
end


--3 - Add a program
function AnzeigeStore3()

 StoreCleaner()
 
 programname = ""
 
 programpfad = ""
 
 n = ""
 
 p = 13.0
 
 f = 1.0
 
 paste = false
 
 while true do
  
  CP(17,6)
  BC(128)
  TC(1)
  print("Add your own program    ")
  
  TC(256)
  
  CP(18,8)
  print("Program Name (6 characters)  ")
  
  CP(18,10)
  print("Program path or Pastebin-Code")
  
  
  CP(18,13)
  TC(256)
  print("<")
  CP(23,13)
  print(">")
  
  CP(26,13)
  TC(1)
  print("Pastebin")
  CP(35,13)
  BC(32768)
  write(" ")
  BC(256)
  print("  ")
  
  BC(128)
  CP(18,16)
  TC(32768)
  print("Create   ")
  
  AppG(n,p,f)
  
  while true do
  
   event,side,x,y = os.pullEvent()
   if event == "mouse_click" then
    
	if x >= 18 and x <= 44 and y == 8 then
	 CP(18,8)
	 write("                                       ")
	 CP(18,8)
	 TC(256)
	 programname = read()
	end
	
	if x >= 35 and x <= 37 and y == 13 then
	 if paste == false then
	  CP(35,13)
      BC(2048)
      write("  ")
      BC(32768)
      print(" ")
	  paste = true
	 elseif paste == true then
	  CP(35,13)
      BC(32768)
      write(" ")
      BC(256)
      print("  ")
	  paste = false
	 end
	 BC(128)
	end
	
	if x >= 18 and x <= 46 and y == 10 then
	 CP(18,10)
	 write("                                       ")
	 CP(18,10) 
	 TC(256)
	 programpfad = read()
	end
    
	if x == 18 and y == 13 then
	 if f >= 2.0 then
	  f = (f-1)
	  AppG(n,p,f)
	 else
	  f = 20.0
	  AppG(n,p,f)
	 end
	end
	
	if x == 23 and y == 13 then
	 if f <= 19.0 then
	  f = (f+1)
	  AppG(n,p,f)
	 else
	  f = 1.0
	  AppG(n,p,f)
	 end
	end
	
	if x >= 18 and x <= 23 and y == 16 then
	 if programname == "" then
	  CP(18,16)
	  TC(16384)
	  print("You forget somthing!  ")
	  sleep(1)
	  CP(18,16)
	  TC(32768)
	  print("Create                ")
	  
	 elseif programpfad == "" then 
	  CP(18,16)
	  TC(16384)
	  print("You forget somthing!  ")
	  sleep(1)
	  CP(18,16)
	  TC(32768)
	  print("Create                ")
	  
	 else
	  CP(18,16)
	  TC(32)
	  print("please wait...        ")
	  
	  term.setBackgroundColor(128)
	  local file = fs.open("SimSoft/Daten/SappS/A","r")
	  local fileData = {}
	  local line = file.readLine()
	  repeat
  	  table.insert(fileData,line)
  	  line = file.readLine()
	  until line == nil
	  file.close()
	  anz = fileData[1]
	  
	  anz = (anz+1)
	  anz = (anz-1)
	  
	  if anz >= 15.0 then
	   CP(18,16)
	   TC(16384)
	   print("Too many Apps.        ")
	   sleep(1)
	   CP(18,16)
	   TC(32768)
	   print("Create             ")
      else
       anz = (anz+1)
       local file = fs.open("SimSoft/Daten/SappS/A","w")
       file.write(anz)
       file.close()
       local file = fs.open("SimSoft/Daten/SappS/"..anz,"w")
       file.writeLine(programname)
       file.writeLine(anz)
       file.writeLine(f)
	   file.writeLine("")
       file.writeLine("sv")
	   file.writeLine("-")
	   if paste == false then
	    file.writeLine(programpfad)
	   elseif paste == true then
	    file.writeLine("SimSoft/SappS/Downloads/"..programpfad)
		Download(programpfad, "SimSoft/SappS/Downloads/"..programpfad)
	   end
       file.close()
	  
	   CP(18,16)
	   TC(32)
	   print("Finish!           ")
	   sleep(1)
	   auswahl = 4
	  end
     end	  
	end
   end
  end
 end
end
 
--Store Start


function StoreStart()

 while true do
  if auswahl == 0 then
   parallel.waitForAny(AnzeigeKontrolle0)
  end
  
  if auswahl == 1 then
   parallel.waitForAny(AnzeigeKontrolle1, AnzeigeStore1)
  end
  
  if auswahl == 2 then
   parallel.waitForAny(AnzeigeKontrolle2, AnzeigeStore2)
  end
  
  if auswahl == 3 then
   parallel.waitForAny(AnzeigeKontrolle3, AnzeigeStore3)
  end
  
  if auswahl == 4 then
   break
  end
 end
end

--Start

parallel.waitForAny(TouchMenu, StoreStart)

end
--end