-- TOGGLEABLE METHODS


function Icon:setLabel(text, toggleState)
	text = text or ""
	self:set("iconText", text, toggleState)
	return self
end

function Icon:setCornerRadius(scale, offset, toggleState)
	local oldCornerRadius = self.instances.iconCorner.CornerRadius
	local newCornerRadius = UDim.new(scale or oldCornerRadius.Scale, offset or oldCornerRadius.Offset)
	self:set("iconCornerRadius", newCornerRadius, toggleState)
	return self
end

function Icon:setImage(imageId, toggleState)
	local textureId = (tonumber(imageId) and "http://www.roblox.com/asset/?id="..imageId) or imageId or ""
	return self:set("iconImage", textureId, toggleState)
end

function Icon:setOrder(order, toggleState)
	local newOrder = tonumber(order) or 1
	return self:set("order", newOrder, toggleState)
end

function Icon:setLeft(toggleState)
	return self:set("alignment", "left", toggleState)
end

function Icon:setMid(toggleState)
	return self:set("alignment", "mid", toggleState)
end

function Icon:setRight(toggleState)
	return self:set("alignment", "right", toggleState)
end

function Icon:setImageYScale(YScale, toggleState)
	local newYScale = tonumber(YScale) or 0.63
	return self:set("iconImageYScale", newYScale, toggleState)
end

function Icon:setImageRatio(ratio, toggleState)
	local newRatio = tonumber(ratio) or 1
	return self:set("iconImageRatio", newRatio, toggleState)
end

function Icon:setLabelYScale(YScale, toggleState)
	local newYScale = tonumber(YScale) or 0.45
	return self:set("iconLabelYScale", newYScale, toggleState)
end
	
function Icon:setBaseZIndex(ZIndex, toggleState)
	local newBaseZIndex = tonumber(ZIndex) or 1
	return self:set("baseZIndex", newBaseZIndex, toggleState)
end

function Icon:_updateBaseZIndex(baseValue)
	local container = self.instances.iconContainer
	local newBaseValue = tonumber(baseValue) or container.ZIndex
	local difference = newBaseValue - container.ZIndex
	if difference == 0 then return "The baseValue is the same" end
	for _, object in pairs(self.instances) do
		object.ZIndex = object.ZIndex + difference
	end
	return true
end

function Icon:setSize(XOffset, YOffset, toggleState)
	local newXOffset = tonumber(XOffset) or 32
	local newYOffset = tonumber(YOffset) or newXOffset
	return self:set("iconSize", UDim2.new(0, newXOffset, 0, newYOffset), toggleState)
end

function Icon:_updateIconSize(_, toggleState)
	-- This is responsible for handling the appearance and size of the icons label and image, in additon to its own size
	local X_MARGIN = 12
	local X_GAP = 8

	local values = {
		iconImage = self:get("iconImage", toggleState) or "_NIL",
		iconText = self:get("iconText", toggleState) or "_NIL",
		iconSize = self:get("iconSize", toggleState) or "_NIL",
		iconImageYScale = self:get("iconImageYScale", toggleState) or "_NIL",
		iconImageRatio = self:get("iconImageRatio", toggleState) or "_NIL",
		iconLabelYScale = self:get("iconLabelYScale", toggleState) or "_NIL",
	}
	for k,v in pairs(values) do
		if v == "_NIL" then
			return
		end
	end

	local iconContainer = self.instances.iconContainer
	local iconLabel = self.instances.iconLabel
	local iconImage = self.instances.iconImage
	local noticeFrame = self.instances.noticeFrame

	-- We calculate the cells dimensions as apposed to reading because there's a possibility the cells dimensions were changed at the exact time and have not yet updated
	-- this essentially saves us from waiting a heartbeat which causes additonal complications
	local cellSizeXOffset = values.iconSize.X.Offset
	local cellSizeXScale = values.iconSize.X.Scale
	local cellWidth = cellSizeXOffset + (cellSizeXScale * iconContainer.Parent.AbsoluteSize.X)
	local minCellWidth = cellWidth
	local maxCellWidth = (cellSizeXScale > 0 and cellWidth) or 9999
	local cellSizeYOffset = values.iconSize.Y.Offset
	local cellSizeYScale = values.iconSize.Y.Scale
	local cellHeight = cellSizeYOffset + (cellSizeYScale * iconContainer.Parent.AbsoluteSize.Y)
	local labelHeight = cellHeight * values.iconLabelYScale
	local labelWidth = textService:GetTextSize(values.iconText, labelHeight, iconLabel.Font, Vector2.new(10000, labelHeight)).X
	local imageWidth = cellHeight * values.iconImageYScale * values.iconImageRatio
	
	local usingImage = values.iconImage ~= ""
	local usingText = values.iconText ~= ""
	local notifPosYScale = 0.5
	local desiredCellWidth
	
	if usingImage and not usingText then
		notifPosYScale = 0.45
		self:set("iconImageVisible", true, toggleState)
		self:set("iconImageAnchorPoint", Vector2.new(0.5, 0.5), toggleState)
		self:set("iconImagePosition", UDim2.new(0.5, 0, 0.5, 0), toggleState)
		self:set("iconImageSize", UDim2.new(values.iconImageYScale*values.iconImageRatio, 0, values.iconImageYScale, 0), toggleState)
		self:set("iconLabelVisible", false, toggleState)

	elseif not usingImage and usingText then
		desiredCellWidth = labelWidth+(X_MARGIN*2)
		self:set("iconLabelVisible", true, toggleState)
		self:set("iconLabelAnchorPoint", Vector2.new(0, 0.5), toggleState)
		self:set("iconLabelPosition", UDim2.new(0, X_MARGIN, 0.5, 0), toggleState)
		self:set("iconLabelSize", UDim2.new(1, -X_MARGIN*2, values.iconLabelYScale, 0), toggleState)
		self:set("iconLabelTextXAlignment", Enum.TextXAlignment.Center, toggleState)
		self:set("iconImageVisible", false, toggleState)

	elseif usingImage and usingText then
		local labelGap = X_MARGIN + imageWidth + X_GAP
		desiredCellWidth = labelGap + labelWidth + X_MARGIN
		self:set("iconImageVisible", true, toggleState)
		self:set("iconImageAnchorPoint", Vector2.new(0, 0.5), toggleState)
		self:set("iconImagePosition", UDim2.new(0, X_MARGIN, 0.5, 0), toggleState)
		self:set("iconImageSize", UDim2.new(0, imageWidth, values.iconImageYScale, 0), toggleState)
		----
		self:set("iconLabelVisible", true, toggleState)
		self:set("iconLabelAnchorPoint", Vector2.new(0, 0.5), toggleState)
		self:set("iconLabelPosition", UDim2.new(0, labelGap, 0.5, 0), toggleState)
		self:set("iconLabelSize", UDim2.new(1, -labelGap-X_MARGIN, values.iconLabelYScale, 0), toggleState)
		self:set("iconLabelTextXAlignment", Enum.TextXAlignment.Left, toggleState)
	end
	if desiredCellWidth then
		if not self._updatingIconSize then
			self._updatingIconSize = true
			local widthScale = (cellSizeXScale > 0 and cellSizeXScale) or 0
			local widthOffset = (cellSizeXScale > 0 and 0) or math.clamp(desiredCellWidth, minCellWidth, maxCellWidth)
			self:set("iconSize", UDim2.new(widthScale, widthOffset, values.iconSize.Y.Scale, values.iconSize.Y.Offset), toggleState, "_ignorePrevious")
			self._updatingIconSize = false
		end
	end
	self:set("iconLabelTextSize", labelHeight, toggleState)
	self:set("noticeFramePosition", UDim2.new(notifPosYScale, 0, 0, -2), toggleState)

	-- Caption
	if self.captionText then
		local CAPTION_X_MARGIN = 6
		local CAPTION_CONTAINER_Y_SIZE_SCALE = 0.8
		local CAPTION_LABEL_Y_SCALE = 0.58
		local captionContainer = self.instances.captionContainer
		local captionLabel = self.instances.captionLabel
		local captionContainerHeight = cellHeight * CAPTION_CONTAINER_Y_SIZE_SCALE
		local captionLabelHeight = captionContainerHeight * CAPTION_LABEL_Y_SCALE
		local labelFont = self:get("captionFont")
		local textWidth = textService:GetTextSize(self.captionText, captionLabelHeight, labelFont, Vector2.new(10000, captionLabelHeight)).X
		captionLabel.TextSize = captionLabelHeight
		captionLabel.Size = UDim2.new(0, textWidth, CAPTION_LABEL_Y_SCALE, 0)
		captionContainer.Size = UDim2.new(0, textWidth + CAPTION_X_MARGIN*2, 0, cellHeight*CAPTION_CONTAINER_Y_SIZE_SCALE)
	end

	self._updatingIconSize = false
end