--[[
	Button for opening the EmoteBar on gamepad enabled devices.
]]


local ContextActionService = game:GetService("ContextActionService")

local Roact = require(script.Parent.Parent.Packages.Roact)
local t = require(script.Parent.Parent.Packages.t)
local ConfigurationContext = require(script.Parent.Parent.Packages.Configuration).ConfigurationContext

local GamepadToggleButton = Roact.Component:extend("GamepadToggleButton")

local MAX_SIZE = 66

GamepadToggleButton.defaultProps = {
	actionName = "GamepadToggleButton",
	onActivated = function() end,
}

GamepadToggleButton.validateProps = t.interface({
	onActivated = t.callback,
	transparency = t.optional(t.union(t.table, t.number)), -- Binding or number
})

function GamepadToggleButton:init()
	self.onActivated = function(_actionName, inputState)
		if inputState ~= Enum.UserInputState.Begin then
			return
		end

		self.props.onActivated()
	end
end

function GamepadToggleButton:render()
	return Roact.createElement("ImageButton", {
		Size = UDim2.fromOffset(MAX_SIZE, MAX_SIZE),
		Image = self.props.configuration.gamepadButtonIcon,
		ImageTransparency = self.props.transparency,
		BackgroundTransparency = 1,

		[Roact.Event.Activated] = self.props.onActivated,
	}, {
		AspectRatio = Roact.createElement("UIAspectRatioConstraint", {
			AspectRatio = 1,
		}),

		GamepadButton = Roact.createElement("ImageLabel", {
			Image = self.props.configuration.toggleButtonImage,
			ImageTransparency = self.props.transparency,
			Size = UDim2.fromScale(0.5, 0.5),
			Position = UDim2.fromScale(1, 1),
			AnchorPoint = Vector2.new(0.85, 0.85),
			BackgroundTransparency = 1,
		}),
	})
end

function GamepadToggleButton:didMount()
	ContextActionService:BindAction(
		self.props.actionName,
		self.onActivated,
		false,
		self.props.configuration.toggleButton
	)
end

function GamepadToggleButton:willUnmount()
	ContextActionService:UnbindAction(self.props.actionName)
end

return ConfigurationContext.withConfiguration(GamepadToggleButton)
