--[[
			// ImageOverlay.lua
			// Creates an image overlay. Used with the game details page to see more thumbnails
]]
local CoreGui = Game:GetService("CoreGui")
local GuiRoot = CoreGui:FindFirstChild("RobloxGui")
local Modules = GuiRoot:FindFirstChild("Modules")
local GuiService = game:GetService('GuiService')
local ContextActionService = game:GetService("ContextActionService")

local GlobalSettings = require(Modules:FindFirstChild('GlobalSettings'))
local Utility = require(Modules:FindFirstChild('Utility'))
local SoundManager = require(Modules:FindFirstChild('SoundManager'))
local ThumbnailLoader = require(Modules:FindFirstChild('ThumbnailLoader'))

local createImageOverlay = function(thumbIds, selectedThumbIndex)
	local thumbCount = #thumbIds
	if selectedThumbIndex < 1 or selectedThumbIndex > thumbCount then
		print("ImageOverlay: Invalid index to selectedThumbIndex")
		return
	end

	local thumbnailImages = nil
	local prevScreenSelectedObject = nil
	local currentSelectedIndex = nil
	local baseZIndex = 3

	local shield = Utility.Create'Frame'
	{
		Name = "Shield";
		Size = UDim2.new(1, 0, 1, 0);
		BackgroundTransparency = 1;
		BackgroundColor3 = Color3.new(0, 0, 0);
		BorderSizePixel = 0;
		ZIndex = baseZIndex;
	}
	local container = Utility.Create'Frame'
	{
		Name = "ImageOverlayContainer";
		Size = UDim2.new(1, 0, 0, 668);
		Position = UDim2.new(0, 0, 0, 226);
		BorderSizePixel = 0;
		BackgroundColor3 = GlobalSettings.OverlayColor;
		ZIndex = baseZIndex;
	}
	local dummySelectionImage = Utility.Create'TextButton'
	{
		Name = "DummySelectionImage";
		Size = UDim2.new(0, 0, 0, 0);
		Visible = false;
	}
	local imageSelection = Utility.Create'Frame'
	{
		Name = "imageSelection";
		Size = UDim2.new(0, 1030, 0, 580);
		Position = UDim2.new(0.5, -1030/2, 0.5, -580/2);
		BackgroundTransparency = 1;
		Selectable = true;
		SelectionImageObject = dummySelectionImage;
		Parent = container;
		SoundManager:CreateSound('MoveSelection');
	}
	local leftArrowImage = Utility.Create'ImageButton'
	{
		Name = "LeftArrowImage";
		Size = UDim2.new(0, 26, 0, 45);
		Position = UDim2.new(0.5, imageSelection.Position.X.Offset - 18 - 75, 0.5, -45/2);
		BackgroundTransparency = 1;
		Image = 'rbxasset://textures/ui/Settings/Slider/Left.png';
		SelectionImageObject = dummySelectionImage;
		ZIndex = baseZIndex + 1;
		Parent = container;
		SoundManager:CreateSound('MoveSelection');
	}
	local rightArrowImage = leftArrowImage:Clone()
	rightArrowImage.Name = "RightArrowImage"
	rightArrowImage.Position = UDim2.new(0.5, imageSelection.Position.X.Offset + imageSelection.Size.X.Offset + 75, 0.5, -45/2)
	rightArrowImage.Image = 'rbxasset://textures/ui/Settings/Slider/Right.png';
	rightArrowImage.Parent = container

	local selectedText = Utility.Create'TextLabel'
	{
		Name = "SelectedText";
		Size = UDim2.new();
		Position = UDim2.new(0.5, 0, 1, -24);
		BackgroundTransparency = 1;
		Font = GlobalSettings.RegularFont;
		FontSize = GlobalSettings.ButtonSize;
		TextColor3 = GlobalSettings.WhiteTextColor;
		Text = "";
		ZIndex = baseZIndex;
		Parent = container;
	}

	local function createThumbImages()
		if not thumbnailImages then
			thumbnailImages = {}
			for i = 1, #thumbIds do
				local image = Utility.Create'ImageLabel'
				{
					Name = tostring(i);
					Size = imageSelection.Size;
					Position = imageSelection.Position;
					BackgroundTransparency = 1;
					ZIndex = baseZIndex;
					Parent = container;
					SoundManager:CreateSound('MoveSelection');
				}
				local loader = ThumbnailLoader:Create(image, thumbIds[i],
					ThumbnailLoader.Sizes.Large, ThumbnailLoader.AssetType.Icon, false)
				spawn(function()
					loader:LoadAsync()
				end)
				thumbnailImages[i] = image
			end
		end
	end

	local function setImagePositions(startIndex)
		for i = 1, #thumbnailImages do
			local image = thumbnailImages[i]
			local xScale = i == startIndex and 0.5 or 1.5
			image.Position = UDim2.new(xScale, -image.Size.X.Offset / 2, 0.5, image.Position.Y.Offset)
		end
	end

	local function tweenImagePositions(currentIndex, nextIndex, direction)
		if currentIndex == nextIndex then return end
		local nextStartPosition = direction * 1.5
		local currentEndPosition = -nextStartPosition
		--
		local currentImage = thumbnailImages[currentIndex]
		local nextImage = thumbnailImages[nextIndex]
		nextImage.Position = UDim2.new(nextStartPosition, -nextImage.Size.X.Offset / 2, 0.5, nextImage.Position.Y.Offset)
		--
		GuiService.SelectedCoreObject = imageSelection
		Utility.TweenPositionOrSet(currentImage, UDim2.new(currentEndPosition, -currentImage.Size.X.Offset / 2, 0.5, currentImage.Position.Y.Offset),
			Enum.EasingDirection.InOut, Enum.EasingStyle.Quad, 0.25, true)
		Utility.TweenPositionOrSet(nextImage, UDim2.new(0.5, -nextImage.Size.X.Offset / 2, 0.5, nextImage.Position.Y.Offset),
			Enum.EasingDirection.InOut, Enum.EasingStyle.Quad, 0.25, true)
		selectedText.Text = tostring(nextIndex)..'/'..tostring(#thumbnailImages)
	end

	local function onClose()
		ContextActionService:UnbindCoreAction("CloseImageOverlay")
		local shieldTweenOut = Utility.PropertyTweener(shield, "BackgroundTransparency", 0.3, 1, 0.25, Utility.EaseInOutQuad, true, function()
			shield:Destroy()
		end)
		container:Destroy()
		GuiService:RemoveSelectionGroup("ImageOverlay")
		if prevScreenSelectedObject then
			GuiService.SelectedCoreObject = prevScreenSelectedObject
		end
		SoundManager:Play('ScreenChange');
	end

	--[[ Input Events ]]--
	ContextActionService:BindCoreAction("CloseImageOverlay",
		function(actionName, inputState, inputObject)
			if inputState == Enum.UserInputState.End then
				onClose()
			end
		end,
		false, Enum.KeyCode.ButtonB)

	local function onArrowSelected(direction)
		local nextIndex = nil
		nextIndex = currentSelectedIndex + direction
		if nextIndex < 1 then
			nextIndex = thumbCount
		elseif nextIndex > thumbCount then
			nextIndex = 1
		end
		if nextIndex then
			tweenImagePositions(currentSelectedIndex, nextIndex, direction)
			currentSelectedIndex = nextIndex
		end
	end

	leftArrowImage.SelectionGained:connect(function()
		onArrowSelected(-1)
	end)
	rightArrowImage.SelectionGained:connect(function()
		onArrowSelected(1)
	end)

	--[[ Initialize Overlay ]]--
	currentSelectedIndex = selectedThumbIndex
	createThumbImages()
	setImagePositions(selectedThumbIndex)
	prevScreenSelectedObject = GuiService.SelectedCoreObject
	GuiService:AddSelectionParent("ImageOverlay", container)
	GuiService.SelectedCoreObject = imageSelection
	selectedText.Text = tostring(selectedThumbIndex)..'/'..tostring(thumbCount)
	if thumbCount == 1 then
		leftArrowImage.Visible = false
		rightArrowImage.Visible = false
	end

	shield.Parent = GuiRoot
	container.Parent = GuiRoot
	local shieldTweenIn = Utility.PropertyTweener(shield, "BackgroundTransparency", 1, 0.3, 0.25, Utility.EaseInOutQuad, nil)
end

return createImageOverlay
