InGameAvatarEditor/src/ServerScriptService/AvatarEditorInGameSetup/AvatarEditorInGame/Modules/Packages/UIBlox/App/InputButton/RadioButtonList.lua
local Packages = script.Parent.Parent.Parent.Parent

local Roact = require(Packages.Roact)
local t = require(Packages.t)
local UIBloxConfig = require(Packages.UIBlox.UIBloxConfig)
local RadioButton = require(script.Parent.RadioButton)

local ID_PROP_NAME = UIBloxConfig.renameKeyProp and "id" or "key"

local RadioButtonList = Roact.PureComponent:extend("RadioButtonList")

local validateButton = t.strictInterface({
	label = t.string,
	isDisabled = t.optional(t.boolean),
})

local validateProps = t.strictInterface({
	radioButtons = t.array(t.union(t.string, validateButton)),
	onActivated = t.callback,
	elementSize = t.UDim2,
	selectedValue = t.optional(t.number),
	layoutOrder = t.optional(t.number)
})

function RadioButtonList:init()
	self.state = {
		currentValue = self.props.selectedValue or 0,
	}

	local disabledIndices = {}
	for i, v in ipairs(self.props.radioButtons) do
		disabledIndices[i] = type(v) == "table" and v.isDisabled or false
	end
	self.state.disabledIndices = disabledIndices

	self.doLogic = function(key)
		if self.state.disabledIndices[key] then return end
		self:setState({
			currentValue = key,
		})
		self.props.onActivated(key)
	end
end

function RadioButtonList:render()
	assert(validateProps(self.props))

	local radioButtons = {}
	radioButtons.layout = Roact.createElement("UIListLayout", {
		SortOrder = Enum.SortOrder.LayoutOrder
	})

	for i, value in ipairs(self.props.radioButtons) do
		radioButtons["RadioButton"..i] = Roact.createElement(RadioButton, {
			text = type(value) == "table" and value.label or value,
			isSelected = i == self.state.currentValue,
			isDisabled = self.state.disabledIndices[i],
			onActivated = self.doLogic,
			size = self.props.elementSize,
			layoutOrder = i,
			[ID_PROP_NAME] = i,
		})
	end

	return Roact.createElement("Frame", {
		Size = UDim2.new(1, 0, 1, 0),
		BackgroundTransparency = 1,
		LayoutOrder = self.props.layoutOrder,
	}, radioButtons)
end

return RadioButtonList