-- Compiled with roblox-ts v2.3.0
local TS = _G[script]
local TweenService = TS.import(script, TS.getModule(script, "@rbxts", "services")).TweenService
--* The Timer Instance for the TimerType.Bar 
--* The Timer Instance for the TimerType.Digit 
--[[
	*
	 * @enum
	 * This enum represents the types of Time display that this Timer will use.
	 
]]
local TimerType
do
	local _inverse = {}
	TimerType = setmetatable({}, {
		__index = _inverse,
	})
	TimerType.Bar = 0
	_inverse[0] = "Bar"
	TimerType.Digit = 1
	_inverse[1] = "Digit"
end
--[[
	*
	 * @enum
	 * The preset Position's of the Timer within the Prompt.
	 
]]
local TimerPosition
do
	local _inverse = {}
	TimerPosition = setmetatable({}, {
		__index = _inverse,
	})
	TimerPosition.TopLeft = 0
	_inverse[0] = "TopLeft"
	TimerPosition.TopRight = 1
	_inverse[1] = "TopRight"
	TimerPosition.Top = 2
	_inverse[2] = "Top"
	TimerPosition.BottomLeft = 3
	_inverse[3] = "BottomLeft"
	TimerPosition.BottomRight = 4
	_inverse[4] = "BottomRight"
	TimerPosition.Bottom = 5
	_inverse[5] = "Bottom"
end
--[[
	*
	 * **Timer**
	 * 
	 * 
	 * The Timer class is designed to create a timer in seconds that
	 * can be displayed on a Bar or with a digital TextLabel.
	 
]]
local Timer
do
	Timer = setmetatable({}, {
		__tostring = function()
			return "Timer"
		end,
	})
	Timer.__index = Timer
	function Timer.new(...)
		local self = setmetatable({}, Timer)
		return self:constructor(...) or self
	end
	function Timer:constructor(_type, start)
		self.ClassName = "Timer"
		self.ParentBorderSize = 0
		self._activeTween = nil
		self._type = _type
		local _condition = start
		if not (_condition ~= 0 and _condition == _condition and _condition) then
			_condition = 0
		end
		self._time = _condition
		self._lastStartTime = self._time
		if _type == TimerType.Bar then
			local back = Instance.new("Frame")
			back.Name = "TimeUI"
			back.Size = UDim2.new(1, 0, 0.05, 0)
			back.BackgroundColor3 = Color3.fromRGB(50, 50, 50)
			local bar = Instance.new("Frame")
			bar.Name = "Bar"
			bar.Size = UDim2.new(1, 0, 1, 0)
			bar.BackgroundColor3 = Color3.fromRGB(80, 80, 80)
			bar.Parent = back
			self._timeUI = back
			self:SetPosition(TimerPosition.Bottom)
		elseif _type == TimerType.Digit then
			local back = Instance.new("Frame")
			back.Name = "TimeUI"
			back.SizeConstraint = Enum.SizeConstraint.RelativeYY
			back.Size = UDim2.new(0.1, 0, 0.1, 0)
			back.BackgroundColor3 = Color3.fromRGB(68, 68, 68)
			local tl = Instance.new("TextLabel")
			tl.Name = "Digit"
			tl.BackgroundTransparency = 1
			tl.Size = UDim2.new(0.97, 0, 0.97, 0)
			tl.AnchorPoint = Vector2.new(0.5, 0.5)
			tl.Position = UDim2.new(0.5, 0, 0.5, 0)
			tl.TextColor3 = Color3.fromRGB(255, 255, 255)
			tl.Font = Enum.Font.Code
			tl.TextScaled = true
			tl.Parent = back
			self._timeUI = back
			self:SetPosition(TimerPosition.BottomLeft)
		else
			error("Failed to create Timer with invalid _type of TimerType: " .. tostring(_type))
		end
	end
	function Timer:Increment(n)
		local _condition = n
		if not (_condition ~= 0 and _condition == _condition and _condition) then
			_condition = 1
		end
		self._time += _condition
		self:updateUI()
	end
	function Timer:Decrement(n)
		if not (n ~= 0 and n == n and n) then
			n = 1
		end
		if (self._time - n) < 0 then
			self._time = 0
		else
			self._time -= n
		end
		self:updateUI()
	end
	function Timer:Reset()
		self._time = self._lastStartTime
		if self._type == TimerType.Bar then
			-- Reset the Timer bar
			local _result = self._activeTween
			if _result ~= nil then
				_result:Cancel()
			end
			self._activeTween = nil
			self._timeUI.Size = UDim2.new(1, 0, 1, 0)
		end
		self:updateUI()
	end
	function Timer:Set(_time)
		self._lastStartTime = _time
		self._time = _time
		if self._type == TimerType.Bar then
			-- Reset the Timer bar
			local _result = self._activeTween
			if _result ~= nil then
				_result:Cancel()
			end
			self._activeTween = nil
			(self._timeUI).Bar.Size = UDim2.new(1, 0, 1, 0)
		end
		self:updateUI()
	end
	function Timer:SetPosition(pos)
		if pos == TimerPosition.TopLeft then
			-- TopLeft should only work with digital timers.
			if self._type == TimerType.Bar then
				warn("Invalid position of [TimerType.Bar] to TopLeft.")
				return nil
			end
			self._timeUI.AnchorPoint = Vector2.new(0, 0)
			self._timeUI.Position = UDim2.new(0.03, 0, 0.03, 0)
		elseif pos == TimerPosition.TopRight then
			-- TopRight should only work with digital timers.
			if self._type == TimerType.Bar then
				warn("Invalid position of [TimerType.Bar] to TopRight.")
				return nil
			end
			self._timeUI.AnchorPoint = Vector2.new(1, 0)
			self._timeUI.Position = UDim2.new(0.97, 0, 0.03, 0)
		elseif pos == TimerPosition.Top then
			-- Top should only work with bar timers.
			if self._type == TimerType.Digit then
				warn("Invalid position of [TimerType.Digit] to Top.")
				return nil
			end
			self._timeUI.AnchorPoint = Vector2.new(0, 0)
			self._timeUI.Position = UDim2.new(0, 0, -self._timeUI.Size.Y.Scale, -(self.ParentBorderSize + 2))
		elseif pos == TimerPosition.BottomLeft then
			-- BottomLeft should only work with digital timers.
			if self._type == TimerType.Bar then
				warn("Invalid position of [TimerType.Bar] to BottomLeft.")
				return nil
			end
			self._timeUI.AnchorPoint = Vector2.new(0, 1)
			self._timeUI.Position = UDim2.new(0.03, 0, 0.97, 0)
		elseif pos == TimerPosition.BottomRight then
			-- BottomRight should only work with digital timers.
			if self._type == TimerType.Bar then
				warn("Invalid position of [TimerType.Bar] to BottomRight.")
				return nil
			end
			self._timeUI.AnchorPoint = Vector2.new(1, 1)
			self._timeUI.Position = UDim2.new(0.97, 0, 0.97, 0)
		elseif pos == TimerPosition.Bottom then
			-- Bottom should only work with bar timers.
			if self._type == TimerType.Digit then
				warn("Invalid position of [TimerType.Digit] to Bottom.")
				return nil
			end
			self._timeUI.AnchorPoint = Vector2.new(0, 0)
			self._timeUI.Position = UDim2.new(0, 0, 1, self.ParentBorderSize)
		end
	end
	function Timer:SetZIndex(zIndex)
		if not self._timeUI then
			return nil
		end
		if self._type == TimerType.Bar then
			local timerBar = self._timeUI
			timerBar.ZIndex = zIndex
			timerBar.Bar.ZIndex = zIndex + 1
		elseif self._type == TimerType.Digit then
			local timerDigit = self._timeUI
			timerDigit.ZIndex = zIndex
			timerDigit.Digit.ZIndex = zIndex + 1
		end
	end
	function Timer:Destroy()
		if self._timeUI then
			self._timeUI:Destroy()
			self._timeUI = nil
		end
		if self._activeTween then
			self._activeTween:Cancel()
			self._activeTween = nil
		end
	end
	function Timer:updateUI()
		if self._type == TimerType.Bar then
			local timeBar = self._timeUI
			-- If the time is 0 do not create a new tween
			if self._time <= 0 then
				return nil
			end
			if self._activeTween and self._activeTween.PlaybackState == Enum.PlaybackState.Playing then
				return nil
			end
			self._activeTween = TweenService:Create(timeBar.Bar, TweenInfo.new(self._time, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {
				Size = UDim2.new(0, 0, timeBar.Bar.Size.Y.Scale, 0),
			})
			self._activeTween:Play()
		elseif self._type == TimerType.Digit then
			local timerDigit = self._timeUI
			timerDigit.Digit.Text = tostring(self._time)
		end
	end
end
return {
	TimerType = TimerType,
	TimerPosition = TimerPosition,
	Timer = Timer,
}
