----------------------------------------------



local PlayerStatManager = require(script.Parent:WaitForChild("PlayerStatManager"))

local tycoonsFolder = script.Parent:WaitForChild("Tycoons")
local tycoons = tycoonsFolder:GetChildren()
local serverStorage = game:GetService("ServerStorage")
local playerMoney = serverStorage:WaitForChild("PlayerMoney")
local purchaseHandlerNew = script.PurchaseHandlerNew
local gateControlNew = script.GateControlNew
local players = game:GetService("Players")

local allObjects = {}

local Settings = require(script.Parent.Settings)

function hideButton(name,buttons)
	local allButtons = buttons:GetChildren()
	local item = nil
	for i = 1,#allButtons,1 do
		if allButtons[i].Object.Value == name then
			item = allButtons[i]
		end
	end
	if item ~= nil then
		local head = item:FindFirstChild("Head")
		if head ~= nil then
			if Settings['ButtonsFadeOut'] then
				head.CanCollide = false
				coroutine.resume(coroutine.create(function()
					for i=1,20 do
						wait(Settings['FadeOutTime']/20)
						head.Transparency = head.Transparency + 0.05
					end
				end))
			else
				head.CanCollide = false
				head.Transparency = 1
			end
		end
	end
end

for i = 1,#tycoons,1 do
	tycoons[i].PurchaseHandler:Destroy()
	tycoons[i].Entrance["Touch to claim!"].GateControl:Destroy()
	purchaseHandlerNew:Clone().Parent = tycoons[i]
	
	local gateControlNewClone = gateControlNew:Clone()
	gateControlNewClone.Parent = tycoons[i].Entrance["Touch to claim!"]
	gateControlNewClone.Disabled = false
	
	local purchasedObjects = tycoons[i]:FindFirstChild("PurchasedObjects")
	local purchases = tycoons[i].Purchases
	local owner = tycoons[i]:FindFirstChild("Owner")
	local entrance = tycoons[i].Entrance
	local touchToClaim = entrance:GetChildren()[1].Head
	
	allObjects[i] = require(tycoons[i].PurchaseHandlerNew)
	
	--Load data
	local debounce = true
	touchToClaim.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid ~= nil and debounce then
			local playerInPlayerMoney = playerMoney:FindFirstChild(humanoid.Parent.Name)
			local ownsTycoon = nil
			if playerInPlayerMoney ~= nil then
		   		 ownsTycoon = playerInPlayerMoney:FindFirstChild("OwnsTycoon")
			end
			if ownsTycoon ~= nil and ownsTycoon.Value == nil then
				touchToClaim.Transparency = 1
				debounce = false
				local player = game.Players:FindFirstChild(humanoid.Parent.Name)
				if player ~= nil then
					local data = PlayerStatManager:getPlayerData(player)
					for key,value in pairs(allObjects[i]) do	
						if data[key] == true then
							value.Parent = purchasedObjects
						end
					end	
					wait(3)
					for k,v in pairs(allObjects[i]) do
						if data[k] == true then
							local buttonName = k
							local buttons = tycoons[i].Buttons
							hideButton(buttonName,buttons)
						end
					end	
				else
					print("Player == nil")	
				end
			else
				print("ownsTycoon.Value ~= nil or ownsTycoon == nil ")	
			end
		else
			print("Humanoid == nil or debounce is false")		
		end
	end)
	
	--Save data
	if purchasedObjects ~= nil and owner ~= nil then
		purchasedObjects.ChildAdded:Connect(function(instance)
			local player = owner.Value
			if player ~= nil then
				local stat = PlayerStatManager:getStat(player,instance.Name)
				if player ~= nil and stat == false then
					PlayerStatManager:ChangeStat(player, instance.Name, true)
				end
			end
		end)
	else
		warn("SaveTrigger script won't save data because either purchasedObjects or owner is NIL")
	end
end

tycoonsFolder.ChildAdded:Connect(function(instance)
	local purchaseHandler = instance:FindFirstChild("PurchaseHandler")
	if purchaseHandler ~= nil then
		purchaseHandler:Destroy()
		purchaseHandlerNew:Clone().Parent = instance
		local purchasedObjects = instance:FindFirstChild("PurchasedObjects")
		local purchases = instance.Purchases
		local owner = instance:FindFirstChild("Owner")
		local entrance = instance.Entrance
		local touchToClaim = entrance:GetChildren()[1].Head
		
		local PurchaseHandlerNew = require(instance.PurchaseHandlerNew)
		
		--Load data
		local debounce = true
		touchToClaim.Touched:Connect(function(hit)
			local humanoid = hit.Parent:FindFirstChild("Humanoid")
			if humanoid ~= nil  and debounce then
				local playerInPlayerMoney = playerMoney:FindFirstChild(humanoid.Parent.Name)
				local ownsTycoon = nil
				if playerInPlayerMoney ~= nil then
					 ownsTycoon = playerInPlayerMoney:FindFirstChild("OwnsTycoon")
				end
				if ownsTycoon ~= nil and ownsTycoon.Value == nil then
					touchToClaim.Transparency = 1
					debounce = false
					local player = game.Players:FindFirstChild(humanoid.Parent.Name)
					if player ~= nil then
						local data = PlayerStatManager:getPlayerData(player)
						for key,value in pairs(PurchaseHandlerNew) do
							if data[key] == true then
								value.Parent = purchasedObjects
							end
						end	
						wait(3)
						for k,v in pairs(PurchaseHandlerNew) do
							if data[k] == true then
								local buttonName = k
								local buttons = instance.Buttons
								hideButton(buttonName,buttons)
							end
						end	
						
					else
						print("Player == nil")	
				 	end
				else
					print("ownsTycoon.Value ~= nil or ownsTycoon == nil ")	
				end
			else
				print("Humanoid == nil or debounce is false")		
			end
		end)
		
		--Save data
		if purchasedObjects ~= nil and owner ~= nil then
			purchasedObjects.ChildAdded:Connect(function(instance)
				local player = owner.Value
				if player ~= nil then
					local stat = PlayerStatManager:getStat(player,instance.Name)
					if player ~= nil and stat == false then
						PlayerStatManager:ChangeStat(player, instance.Name, true)
					end
				end
			end)
		else
			warn("SaveTrigger script won't save data because either purchasedObjects or owner is NIL")
		end
	end
end)

while wait(59) do
	pcall(function()
		local allPlayers = players:GetChildren()
		for i = 1,#allPlayers,1 do
			if allPlayers[i] ~= nil then
				local playersMoneyChild = playerMoney:FindFirstChild(allPlayers[i].Name)
				if playersMoneyChild then
					PlayerStatManager:saveStat(allPlayers[i], "Money", playersMoneyChild.Value)
				end
			end
		end
	end)
end