local x=script.Parent.Parent local C=require(x:WaitForChild("\076\097\122\121\076\111\097\100\101\114"))local I=game:GetService("\085\115\101\114\073\110\112\117\116\083\101\114\118\105\099\101")local N={[Enum.UserInputType.MouseButton1]=true,[Enum.UserInputType.Touch]=true}local X="\114\098\120\097\115\115\101\116\105\100\058\047\047\053\049\053\052\048\055\056\057\050\053"local a="\114\098\120\097\115\115\101\116\105\100\058\047\047\053\049\052\051\049\054\053\053\052\057"local O={}O.__index=O O.__type="\068\114\111\112\100\111\119\110"function O.__tostring(x)return O.__type end function O.new(x,I)local N=setmetatable({},O)N._Maid=C.Utilities.Maid.new()N._ChangedBind=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")N._Options={}N._Selected=nil N.Button=x N.ListFrame=I N.Changed=N._ChangedBind.Event init(N)N:Set(N._Options[1])return N end function init(x)local C=x.Button local X=x.ListFrame for C,I in next,X.ScrollFrame:GetChildren()do if not I:IsA("\071\117\105\066\117\116\116\111\110")then continue end x._Options[C]=I I.Activated:Connect(function()x:Set(I)end)end x._Maid:Mark(C.Activated:Connect(function()x:Show(not X.Visible)end))x._Maid:Mark(I.InputBegan:Connect(function(I)if N[I.UserInputType]then local N=I.Position local a=Vector2.new(N.x,N.y)if X.Visible and not(isInFrame(X,a)or isInFrame(C,a))then x:Show(false)end end end))end function isInFrame(x,C)local I=x.AbsolutePosition local N=x.AbsoluteSize local X=C-I return X.x>=0 and(X.x<=N.x and(X.y>=0 and X.y<=N.y))end function O.Set(C,x)if C._Selected~=x then C._Selected=x C._ChangedBind:Fire(x)C.Button.Option.Text=x.Text end C:Show(false)end function O.Get(x)return x._Selected end function O.Show(C,x)C.Button.Arrow.Image=x and X or a C.ListFrame.Visible=x end function O.Destroy(x)x._Maid:Sweep()x._ChangedBind:Destroy()x._Changed=nil x._Options=nil x._Selected=nil end return O
--[[
Classes.Dropdown

This class creates a dropdown that the user can select a list of options from.

Constructors:
	new(frame [instance], listFrame [instance])
		>
	Create(list[] [string], max [integer])
		> Creates a dropdown from the list with a max scrolling number.

Properties:
	Frame [instance]
		> The container frame for the dropdown. Can be used for positioning and resizing.
	ListFrame [instance]
 		> The contaienr frame for the dropdown list. Parented underneath the main container frame.

Methods:
	:Set(option [instance]) [void]
		> option is a child of the ListFrame and you can set it as the selected option of the dropdown with this method
	:Get() [instance]
		> Returns the selected option frame (which is again, a child of the ListFrame)
	:Show(bool [boolean])
		> Set whether the dropdown list is visible or not.
	:Destroy() [void]
		> Destroys the RadioButtonGroup and all the events, etc that were running it.

Events:
	.Changed:Connect(function(option [instance])
		> Fired when the user selects a new option from the dropdown list.
--]]


-- CONSTANTS

local GuiLib = script.Parent.Parent
local Lazy = require(GuiLib:WaitForChild("LazyLoader"))
-- local Defaults = GuiLib:WaitForChild("Defaults")

local UIS = game:GetService("UserInputService")

local VALID_PRESS = {
	[Enum.UserInputType.MouseButton1] = true;
	[Enum.UserInputType.Touch] = true;
}

local ARROW_UP = "rbxassetid://5154078925"
local ARROW_DOWN = "rbxassetid://5143165549"

-- local DROP_BUTTON = Defaults:WaitForChild("DropdownButton")

-- Class

local DropdownClass = {}
DropdownClass.__index = DropdownClass
DropdownClass.__type = "Dropdown"

function DropdownClass:__tostring()
	return DropdownClass.__type
end

-- Public Constructors

function DropdownClass.new(button, listFrame)
	local self = setmetatable({}, DropdownClass)
	
	self._Maid = Lazy.Utilities.Maid.new()
	self._ChangedBind = Instance.new("BindableEvent")
	self._Options = {}
	self._Selected = nil
	
	self.Button = button
	self.ListFrame = listFrame
	self.Changed = self._ChangedBind.Event
	
	init(self)
	self:Set(self._Options[1])
	
	return self
end

-- function DropdownClass.Create(list, max)
-- 	max = max or #list
	
-- 	local button = DROP_BUTTON:Clone()
-- 	local listFrame = Lazy.Constructors.List.Create(list, max)
	
-- 	listFrame.Position = UDim2.new(0, 0, 1, 0)
-- 	listFrame.Size = UDim2.new(1, 0, max, 0)
-- 	listFrame.Visible = false
-- 	listFrame.Parent = button
	
-- 	return DropdownClass.new(button, listFrame)
-- end

-- Private Methods

function init(self)
	local button = self.Button
	local listFrame = self.ListFrame
	
	--local function contentSizeUpdate()
	--	local absSize = button.AbsoluteSize
	--	local ratio = absSize.y / absSize.x
		
	--	button.Arrow.Size = UDim2.new(ratio, 0, 1, 0)
	--	button.Option.Size = UDim2.new(1 - ratio, -12, 1, 0)
	--end
	
	--contentSizeUpdate()
	--self._Maid:Mark(button:GetPropertyChangedSignal("AbsoluteSize"):Connect(contentSizeUpdate))
	
	for i, optionButton in next, listFrame.ScrollFrame:GetChildren() do
		if not optionButton:IsA("GuiButton") then
			continue
		end
		
		self._Options[i] = optionButton
		optionButton.Activated:Connect(function()
			self:Set(optionButton)
		end)
	end

	self._Maid:Mark(button.Activated:Connect(function()
		self:Show(not listFrame.Visible)
	end))
	
	self._Maid:Mark(UIS.InputBegan:Connect(function(input)
		if (VALID_PRESS[input.UserInputType]) then
			local p = input.Position
			local p2 = Vector2.new(p.x, p.y)
			
			if (listFrame.Visible and not (isInFrame(listFrame, p2) or isInFrame(button, p2))) then
				self:Show(false)
			end
		end
	end))
end

function isInFrame(frame, pos)
	local fPos = frame.AbsolutePosition
	local fSize = frame.AbsoluteSize
	local d = pos - fPos
	return (d.x >= 0 and d.x <= fSize.x and d.y >= 0 and d.y <= fSize.y)
end

-- Public Methods

function DropdownClass:Set(option)
	if (self._Selected ~= option) then
		self._Selected = option
		self._ChangedBind:Fire(option)
		self.Button.Option.Text = option.Text
	end
	self:Show(false)
end

function DropdownClass:Get()
	return self._Selected
end

function DropdownClass:Show(bool)
	self.Button.Arrow.Image = bool and ARROW_UP or ARROW_DOWN
	self.ListFrame.Visible = bool
end

function DropdownClass:Destroy()
	self._Maid:Sweep()
	self._ChangedBind:Destroy()
	self._Changed = nil
	self._Options = nil
	self._Selected = nil
end

--

return DropdownClass