local C=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local g=C:WaitForChild("\077\111\100\117\108\101\115")local q=require(g.src.utils.Logger)local h=require(g.src.objects.InventoryObjects)local x=h.RoomObjects local t=game:GetService("\080\104\121\115\105\099\115\083\101\114\118\105\099\101")local e=require(g.src.CollisionGroup)local P=require(g.M)local H=game.Workspace:WaitForChild("\080\108\097\099\101")local n=H:findFirstChild("\082\111\111\109\115")local l={}function l.initCollisionGroups(C)q.d("\073\110\105\116\105\097\108\105\122\105\110\103 \082\111\111\109\115 \099\111\108\108\105\115\105\111\110 \103\114\111\117\112")function initRoomGroups(C)local g=l:getRoomCollisionGroup(C)e:getOrCreateGroupId(g)t:CollisionGroupSetCollidable("\068\101\102\097\117\108\116",g,true)local q=l:findRoomPart(C)if q then local C=q.placeholders:FindFirstChild("\076\111\099\107\080\108\097\099\101\104\111\108\100\101\114")if C then local q=C:FindFirstChild("\076\111\099\107")e:setCollisionGroupRecursive(q,g)end end end P.each(x,initRoomGroups)end function l.initPlayerCollisionGroup(g,C)q:d("\073\110\105\116 \112\108\097\121\101\114 \099\111\108\108\105\115\105\103\111\110 \103\114\111\117\112 "..C)e:getOrCreateGroupId(C)end function l.removePlayerCollisionGroup(g,C)q:d("\082\101\109\111\118\101 \112\108\097\121\101\114 \099\111\108\108\105\115\105\103\111\110 \103\114\111\117\112 "..C)t:RemoveCollisionGroup(C)end function l.findRoomPart(g,C)local h=C.modelName if not n then q:w("\082\111\111\109\115 \070\111\108\100\101\114 \100\111\101\115 \110\111\116 \101\120\105\115\116\115\033")return end local x=n:findFirstChild(h)if not x then q:w("\082\111\111\109 \111\098\106\101\099\116 \102\111\114 "..(h.." \100\111\101\115 \110\111\116 \101\120\105\115\116\115\033"))return end return x end function l.getRoomCollisionGroup(g,C)return"\114\111\111\109\095"..tostring(C.id)end function l.addToCharacter(H,C,g,h)if not C then q:d("\078\111 \067\104\097\114\097\099\116\101\114 \102\111\117\110\100 \102\111\114 \112\108\097\121\101\114\058"..tostring(h))return end e:setCollisionGroupRecursive(C,h)function applyRoomLocks(C)local x=g[C.id]local e=l:getRoomCollisionGroup(C,h)if x then q:d("\082\101\109\111\118\101 \114\111\111\109 "..(C.name.." \108\111\099\107 \102\111\114 \112\108\097\121\101\114\046"))t:CollisionGroupSetCollidable(h,e,false)end end P.each(x,applyRoomLocks)end return l
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Modules = ReplicatedStorage:WaitForChild('Modules')
local logger = require(Modules.src.utils.Logger)
local InventoryObjects = require(Modules.src.objects.InventoryObjects)
local RoomObjects = InventoryObjects.RoomObjects
local PhysicsService = game:GetService('PhysicsService')
local CollisionGroup = require(Modules.src.CollisionGroup)

local M = require(Modules.M)

local Place = game.Workspace:WaitForChild('Place')
local RoomsFolder = Place:findFirstChild('Rooms')

local RoomManager = {}

function RoomManager:initCollisionGroups()
	logger.d('Initializing Rooms collision group')

	function initRoomGroups(room)
		local groupName = RoomManager:getRoomCollisionGroup(room)

		CollisionGroup:getOrCreateGroupId(groupName)
		PhysicsService:CollisionGroupSetCollidable('Default', groupName, true)

		local roomPart = RoomManager:findRoomPart(room)

		if roomPart then
			local lockPlaceholder = roomPart.placeholders:FindFirstChild('LockPlaceholder')

			if lockPlaceholder then
				local lockPart = lockPlaceholder:FindFirstChild('Lock')

				CollisionGroup:setCollisionGroupRecursive(lockPart, groupName)
			end
		end
	end

	M.each(RoomObjects, initRoomGroups)
end

function RoomManager:initPlayerCollisionGroup(playerId)
	logger:d('Init player collisigon group ' .. playerId)
	CollisionGroup:getOrCreateGroupId(playerId)
end

function RoomManager:removePlayerCollisionGroup(playerId)
	logger:d('Remove player collisigon group ' .. playerId)
	PhysicsService:RemoveCollisionGroup(playerId)
end

-- TODO use this everywehere else also
function RoomManager:findRoomPart(room)
	local modelName = room.modelName
	if not RoomsFolder then
		logger:w('Rooms Folder does not exists!')
		return
	end

	local roomObj = RoomsFolder:findFirstChild(modelName)
	if not roomObj then
		logger:w('Room object for ' .. modelName .. ' does not exists!')
		return
	end

	return roomObj
end
function RoomManager:getRoomCollisionGroup(room)
	return 'room_' .. tostring(room.id)
end

function RoomManager:addToCharacter(character, inventory, playerId)
	if not character then
		logger:d('No Character found for player:' .. tostring(playerId))
		return
	end

	CollisionGroup:setCollisionGroupRecursive(character, playerId)

	function applyRoomLocks(room)
		local hasRoom = inventory[room.id]

		local groupName = RoomManager:getRoomCollisionGroup(room, playerId)

		if hasRoom then
			logger:d('Remove room ' .. room.name .. ' lock for player.')
			PhysicsService:CollisionGroupSetCollidable(playerId, groupName, false)
		end
	end
	M.each(RoomObjects, applyRoomLocks)
end

return RoomManager