-- Functions

local Clear = function()
	for _,v in pairs(Content:GetChildren()) do
		if v ~= UIGrid then
			v:Destroy()
		end
	end
end

local Add = function(char)
	local charPrev = char:GetChildren()[1]
	assert(charPrev,"[Error] Character doesn't exist in module " .. char.Name)
	
	-- base frame
	local ViewportFrame = Instance.new("ViewportFrame")
	ViewportFrame.Name = char.Name
	ViewportFrame.Parent = Content
	ViewportFrame.BorderSizePixel = 0
	ViewportFrame.BackgroundColor3 = Color3.fromRGB(25,25,25)
	local Uicorner = game.ServerStorage.Extra:WaitForChild("UICorner"):Clone()
	Uicorner.Parent = ViewportFrame
	
	-- character preview
	local clone = charPrev:Clone()
	clone.Parent = ViewportFrame
	
	-- camera
	local camera = Instance.new("Camera")
	camera.Parent = ViewportFrame
	camera.CFrame = CFrame.new(
		(clone:WaitForChild("Head").CFrame * CFrame.new(0,0,-8)).Position
		+ Vector3.new(3,-2,0), 
		clone.Head.Position
	)
	camera.FieldOfView = 30
	
	ViewportFrame.CurrentCamera = camera
	
	-- button
	local button = Instance.new("TextButton")
	button.Parent = ViewportFrame
	button.Size = UDim2.new(1,0,1,0)
	button.BackgroundTransparency = 1
	button.Text = ""
	
	
	button.MouseButton1Click:Connect(function()
		if Debounce then return end
		Debounce = true;
		SelectedCharacter.Value = char
		wait(1)
		Debounce = false
	end)
end

local Refresh = function()
	Clear();
	local cellsize = GetCellSize:InvokeClient(Player)
	UIGrid.CellSize = UDim2.new(0,cellsize,0,cellsize)
	for _,v in pairs(Characters:GetChildren()) do
		Add(v);
	end
end

Refresh();
