--SimSoft Yellow -  Copyright by Simlor (http://www.computercraft.info/forums2/index.php?/user/55655-simlor/)

--AppStore

AppLink = nil
AppName = nil

seite = 1

AppStoreEXIT = false

AppStoreAnzeigeC = 2
MaximaleAppSeitenC = 3
IsPastebin = false


function IsPastebinANZEIGE()

	if IsPastebin == true then
		
		CP(13,10)
		
		BC(SystemColor)
		write("  ")
		
		BC(128)
		print(" ")
		
	elseif IsPastebin == false then
				
		CP(13,10)
				
		BC(SystemColor)
		write(" ")
				
		BC(128)
		print("  ")
			
	end
end

function AppListe_ANZEIGE()

	BC(1)
	Clear()
	
	file = fs.open("SimSoft/Data/SystemColor","r")
	local fileData = {}
	local line = file.readLine()
	repeat
	table.insert(fileData,line)
	line = file.readLine()
	until line == nil
	file.close()

	SystemColor = fileData[3]

	SystemColor = ((SystemColor-1)+1)

	w, h = term.getSize()

	wC = 0
	hC = 1
	
	while true do

		wC = (wC+1)
	
		CP(wC, hC)
		BC(SystemColor)
		write(" ")
	
		if wC == w then
			if hC == 3 then
				break
			else
				hC = (hC+1)
				wC = 0
			end
		end
	
	end

	anzeige = "App Store - App List"

	TC(1)
	CP(((w/2)-((#anzeige)/2)),2)

	print(anzeige)
	
	
	CP(2,2)
	BC(SystemColor)
	TC(1)
	write("<")
	
	CP((w-1),2)
	BC(SystemColor)
	TC(1)
	write(">")
	
	CP(1,1)
	
	BC(SystemColor)
	TC(1)
	
	CP((w-11), (h-3))
	print("           ")
	CP((w-11), (h-2))
	print("  Desktop  ")
	CP((w-11), (h-1))
	print("           ")
	
	AppListLoader()
	
	AppListANZEIGE(seite)
	
end

function SelfMadeApp_ANZEIGE()

	BC(1)
	Clear()
	
	file = fs.open("SimSoft/Data/SystemColor","r")
	local fileData = {}
	local line = file.readLine()
	repeat
	table.insert(fileData,line)
	line = file.readLine()
	until line == nil
	file.close()

	SystemColor = fileData[3]

	SystemColor = ((SystemColor-1)+1)

	w, h = term.getSize()

	wC = 0
	hC = 1
	
	while true do

		wC = (wC+1)
	
		CP(wC, hC)
		BC(SystemColor)
		write(" ")
	
		if wC == w then
			if hC == 3 then
				break
			else
				hC = (hC+1)
				wC = 0
			end
		end
	
	end

	anzeige = "App Store - Create Your Own App"

	TC(1)
	CP(((w/2)-((#anzeige)/2)),2)

	print(anzeige)
	
	
	CP(2,2)
	BC(SystemColor)
	TC(1)
	write("<")
	
	CP((w-1),2)
	BC(SystemColor)
	TC(1)
	write(">")
	
	CP(1,1)
	
	BC(SystemColor)
	TC(1)
	
	CP((w-11), (h-3))
	print("           ")
	CP((w-11), (h-2))
	print("  Desktop  ")
	CP((w-11), (h-1))
	print("           ")
	
	BC(1)
	
	if (AppName == nil) then
		TC(SystemColor)
		CP(4,6)
		write("Program Name ")
		TC(128)
		print("(9 Character)")
	else
		
		TC(SystemColor)
		CP(4,6)
		print("                                               ")
		CP(4,6)
		print(AppName)
		
	end
	
	if (AppLink == nil) then
		TC(SystemColor)
		CP(4,8)
		write("Program Path Or Pastebin-Code")
	else
		TC(SystemColor)
		CP(4,8)
		print("                                               ")
		CP(4,8)
		print(AppLink)
	end
		
		
	if not (AppName == nil) and not (AppLink == nil) then
		TC(32)
		CP(4,(h-2))
		write("Create")
	else
		TC(128)
		CP(4,(h-2))
		write("Create")
	end
	
	
	TC(128)
	CP(4,10)
	write("Pastebin")
	
	TC(256)
	CP(4,12)
	write("Activate Pastebin First")
	CP(4,13)
	write("If You Want To Use It.")
	TC(128)
	
	IsPastebinANZEIGE()
	
end

function AnzeigeLoader()
	if AppStoreAnzeigeC == 1 and AppStoreEXIT == false then
		
		AppListe_ANZEIGE()
		
		
	elseif AppStoreAnzeigeC == 2 and AppStoreEXIT == false then
		
		SelfMadeApp_ANZEIGE()
		
	elseif AppStoreAnzeigeC == 3 and AppStoreEXIT == false then
		
		STD_GUI_ANZEIGE()
		
	end
end

function STD_GUI_ANZEIGE()
	
	BC(1)
	Clear()
	
	file = fs.open("SimSoft/Data/SystemColor","r")
	local fileData = {}
	local line = file.readLine()
	repeat
	table.insert(fileData,line)
	line = file.readLine()
	until line == nil
	file.close()

	SystemColor = fileData[3]

	SystemColor = ((SystemColor-1)+1)

	w, h = term.getSize()

	wC = 0
	hC = 1
	
	while true do

		wC = (wC+1)
	
		CP(wC, hC)
		BC(SystemColor)
		write(" ")
	
		if wC == w then
			if hC == 3 then
				break
			else
				hC = (hC+1)
				wC = 0
			end
		end
	
	end

	anzeige = "App Store - STD-GUI"

	TC(1)
	CP(((w/2)-((#anzeige)/2)),2)

	print(anzeige)
	
	CP(2,2)
	BC(SystemColor)
	TC(1)
	write("<")
	
	CP((w-1),2)
	BC(SystemColor)
	TC(1)
	write(">")
	
	CP(1,1)
	
	BC(SystemColor)
	TC(1)
	
	CP((w-11), (h-3))
	print("           ")
	CP((w-11), (h-2))
	print("  Desktop  ")
	CP((w-11), (h-1))
	print("           ")
	
	
	BC(SystemColor)
	TC(1)
	
	anzeige = "    STD-GUI    "
	
	CP(((w/2)-((#anzeige)/2)),7)
	print("               ")
	CP(((w/2)-((#anzeige)/2)),8)
	print(anzeige)
	CP(((w/2)-((#anzeige)/2)),9)
	print("               ")
	
	BC(1)
	
	TC(128)
	
	anzeige = "STD-GUI Is An App Store Optimized For SimSoft."
	
	CP((((w/2)-((#anzeige)/2))+2),12)
	print(anzeige)
	
	anzeige = "Store by LDDestroier"
	
	TC(128)
	CP((((w/2)-((#anzeige)/2))+1),14)
	write("Store By ")
	TC(SystemColor)
	print("LDDestroier")
	
	
	
end


function AppListLoader()
	
	fs.delete("SimSoft/Data/StoreTemp")
	local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
	local datei = http.get("https://pastebin.com/raw/VGVnVK4j".. "?cb=" .. cacheBuster)
	datei = datei.readAll()
 
	local file = fs.open("SimSoft/Data/StoreTemp", "w")
	file.write(datei)
	file.close()
	
	file = fs.open("SimSoft/Data/StoreTemp","r")
	local fileData = {}
	local line = file.readLine()
	repeat
	table.insert(fileData,line)
	line = file.readLine()
	until line == nil
	file.close()
	
	C = 0
	StoreTempData = nil
	StoreTempData = {}
	
	while true do
		
		C = (C+1)
		
		if fileData[C] == nil or fileData[C] == "" then
			
			MaxStoreApp = ((C-1)/3)
			break
			
		else
		
			table.insert(StoreTempData, (fileData[C]))
			
		end
		
	end
	
end

function AppListANZEIGE(seite)
	
	w, h = term.getSize()
	
	BC(1)
	
	CP(4,(h-2))
	TC(SystemColor)
	print("<  >")
	
	CP(6,6)
	print("                                 ")
	
	TC(SystemColor)
	CP(4,6)
	print((StoreTempData[((seite*3)-2)]))
	
	CP(6,9)
	print("                                 ")
	
	TC(128)
	CP(6,9)
	write("App by ")
	TC(SystemColor)
	print((StoreTempData[((seite*3)-1)]))
	
	CP(6,11)
	print("                                 ")
	
	TC(128)
	CP(6,11)
	write("Copy ")
	TC(SystemColor)
	print("("..(StoreTempData[((seite*3)-0)])..")")
	
	TC(256)
	CP(6,13)
	print("Click On 'Copy' To Copy The Pastebin-Code")
	CP(6,14)
	print("Into App Creator.")
	
	
	
end



w, h = term.getSize()

AnzeigeLoader()

while true do

	event, side, x, y = os.pullEvent()
	if event == "mouse_click" then
		
		--Anzeige 1 
		
		if x >= (w-11) and x <= (w-1) and y >= (h-3) and y <= (h-1) and AppStoreEXIT == false then
			
			AppStoreEXIT = true
			
		end
		
		if AppStoreEXIT == false and AppStoreAnzeigeC == 1 and x >= 1 and x <= w and y >= 3 and y <= h and not (x >= (w-11) and x <= (w-1) and y >= (h-3) and y <= (h-1)) then
			
			if x == 4 and y == (h-2) then
			
				if seite == 1 then
					seite = MaxStoreApp
				else
					seite = (seite-1)
				end
				
				AppListANZEIGE(seite)
				
			end
			
			if x == 7 and y == (h-2) then
			
				if seite == MaxStoreApp then
					seite = 1
				else
					seite = (seite+1)
				end
				
				AppListANZEIGE(seite)
				
			end
			
			if x >= 6 and x <= 20 and y == 11 then
				
				IsPastebin = true
				AppLink = (StoreTempData[((seite*3)-0)])
				
				AppName = nil
				
				TC(256)
				CP(6,11)
				write("Copy ")
				TC(256)
				print("("..(StoreTempData[((seite*3)-0)])..")")
				sleep(0.2)
				
				
				TC(128)
				CP(6,11)
				write("Copy ")
				TC(SystemColor)
				print("("..(StoreTempData[((seite*3)-0)])..")")
				
			end
			
		end
		
		--Anzeige 2
		
		if AppStoreEXIT == false and AppStoreAnzeigeC == 2 and x >= 1 and x <= w and y >= 3 and y <= h and not (x >= (w-11) and x <= (w-1) and y >= (h-3) and y <= (h-1)) then
			
			
			if x >= 4 and x <= (w-1) and y == 6 then
				
				BC(1)
				CP(4,6)
				print("                                               ")
				CP(4,6)
				TC(SystemColor)
				AppName = read()
				
				if AppName == "" then
					CP(4,6)
					print("                                               ")
					CP(4,6)
					TC(SystemColor)
					print("Please Enter A Name.")
					AppName = nil
					
					sleep(1)
					
					SelfMadeApp_ANZEIGE()
					
				end
				
			end
			
			if x >= 4 and x <= (w-1) and y == 8 then
				
				BC(1)
				CP(4,8)
				print("                                               ")
				CP(4,8)
				TC(SystemColor)
				AppLink = read()
				
				AppLinkA = fs.exists(AppLink)
				
				if IsPastebin == false and (AppLink == "" or AppLinkA == false) then
					CP(4,8)
					print("                                               ")
					CP(4,8)
					TC(SystemColor)
					print("The Path Was Not Found.")
					AppLink = nil
					
					sleep(1)
					
					SelfMadeApp_ANZEIGE()
					
				end
				
				if IsPastebin == true and AppLink == "" then
					
					CP(4,8)
					print("                                               ")
					CP(4,8)
					TC(SystemColor)
					print("Please Enter Something.")
					AppLink = nil
					
					sleep(1)
					
					SelfMadeApp_ANZEIGE()
					
				end
				
			end
			
			if x >= 13 and x <= 15 and y == 10 then
				
				if IsPastebin == true then
					IsPastebin = false
				elseif IsPastebin == false then
					IsPastebin = true
				end
				
				IsPastebinANZEIGE()
				
			end
			
			if not (AppName == nil) and not (AppLink == nil) then
				
				TC(32)
				CP(4,(h-2))
				BC(1)
				write("Create")
				
				
				
				
			end
			
			if x >= 4 and x <= 9 and y == (h-2) and not (AppName == nil) and not (AppLink == nil) and not (AppName == "") and not (AppLink == "") then
				
				fs.makeDir("SimSoft/SappS/"..AppName)
				
				if IsPastebin == true then
					local file = fs.open("SimSoft/SappS/"..AppName.."/SappS", "w")
					file.writeLine("SimSoft App System (SappS)")
					file.writeLine("SimSoft/SappS/"..AppName.."/"..AppLink)
					file.writeLine(AppName)
					file.close()
					
					local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
					local datei = http.get("https://pastebin.com/raw/" .. textutils.urlEncode(AppLink) .. "?cb=" .. cacheBuster)
					datei = datei.readAll()
					
					local file = fs.open("SimSoft/SappS/"..AppName.."/"..AppLink, "w")
					file.writeLine(datei)
					file.close()
				else
					local file = fs.open("SimSoft/SappS/"..AppName.."/SappS", "w")
					file.writeLine("SimSoft App System (SappS)")
					file.writeLine(AppLink)
					file.writeLine(AppName)
					file.close()
				end
				
				AppStoreEXIT = true
				
			end
			
		end
		
		--Anzeige 3
		
		if AppStoreEXIT == false and AppStoreAnzeigeC == 3 and x >= 1 and x <= w and y >= 3 and y <= h and not (x >= (w-11) and x <= (w-1) and y >= (h-3) and y <= (h-1)) then
			
			anzeige = "    STD-GUI    "
			
			if x >= ((w/2)-((#anzeige)/2)) and x <= (((w/2)-((#anzeige)/2))+((#anzeige)-1)) and y >= 7 and y <= 9 then
				
				fs.delete("SimSoft/Data/STDGUI_Temp")
				
				local cacheBuster = ("%x"):format(math.random(0, 2 ^ 30))
				local datei = http.get("https://pastebin.com/raw/P9dDhQ2m".. "?cb=" .. cacheBuster)
				datei = datei.readAll()
				
				local file = fs.open("SimSoft/Data/STDGUI_Temp", "w")
				file.write(datei)
				file.close()
				
				shell.run("SimSoft/Data/STDGUI_Temp")
				
				STD_GUI_ANZEIGE()
				
			end
			
			
			
		end
		
		if x == 2 and y == 2 then
			
			if AppStoreAnzeigeC == 1 then
				AppStoreAnzeigeC = MaximaleAppSeitenC
			else
				AppStoreAnzeigeC = (AppStoreAnzeigeC-1)
			end
			
			AnzeigeLoader()
			
		end
		
		if x == (w-1) and y == 2 then
			
			if AppStoreAnzeigeC == MaximaleAppSeitenC then
				AppStoreAnzeigeC = 1
			else
				AppStoreAnzeigeC = (AppStoreAnzeigeC+1)
			end
			
			AnzeigeLoader()
			
		end
		
		if AppStoreEXIT == true then
			break
		end
		
		
	end
end








--end