local AceGUI = LibStub("AceGUI-3.0")

--------------------------
-- Slider  	            --
--------------------------
do
	local widgetType = "List_Slider"
	local widgetVersion = 1
	
	local function OnAcquire(self)
		self.keys = {}
		self.reversekeys = {}
		self:SetWidth(200)
		self:SetHeight(44)
		self:SetDisabled(false)
		self:SetSliderValues(0,100,1)
		self:SetValue(0)
	end
	
	local function OnRelease(self)
		self.frame:ClearAllPoints()
		self.frame:Hide()
		self:SetDisabled(false)
		self.keys = nil
		self.reversekeys = nil
		self.list = nil
	end

	local function UpdateText(self)
		local value = self.value or 0
		self.editbox:SetText(self.list and self.list[self.keys[value]] or '')
	end
	
	local function SetValue(self, value)
		self.slider.setup = true
		self.slider:SetValue(self.reversekeys[value] or 0)
		self.value = self.reversekeys[value]
		UpdateText(self)
		self.slider.setup = nil
	end
	
	local function SetList(self, list)
		if not next(list) then return end
		self.list = list
		for k in pairs(self.list) do
			self.keys[#self.keys+1] = k
		end
		table.sort(self.keys)
		for i, k in ipairs(self.keys) do
			self.reversekeys[k] = i
		end
		self:SetSliderValues(1,#self.keys,1)
	end
		
	local function Slider_OnValueChanged(this)
		local self = this.obj
		if not this.setup then
			local newvalue
			newvalue = this:GetValue()
			if newvalue ~= self.value and not self.disabled then
				self.value = newvalue
				if not self.MouseDown then
					self:Fire("OnValueChanged", self.keys[newvalue])
				end
			end
			if self.value then
				local value = self.value
				UpdateText(self)
			end
		end
	end
	
	local function Slider_OnMouseUp(this)
		local self = this.obj
		self.MouseDown = false
		self:Fire("OnValueChanged",self.keys[this:GetValue()])
	end
	
	local function Slider_OnMouseDown(this)
		local self = this.obj
		self.MouseDown = true
	end

	local function UpdateLabels(self)
		local min, max = (self.min or 0), (self.max or 100)
		if self.ispercent then
			self.lowtext:SetFormattedText("%s%%",(min * 100))
			self.hightext:SetFormattedText("%s%%",(max * 100))
		else
			self.lowtext:SetText(self.list and self.list[self.keys[min]] or '')
			self.hightext:SetText(self.list and self.list[self.keys[max]] or '')
		end
	end

	local function SetSliderValues(self, min, max, step)
		local frame = self.slider
		frame.setup = true
		self.min = min
		self.max = max
		self.step = step
		frame:SetMinMaxValues(min or 0,max or 100)
		UpdateLabels(self)
		frame:SetValueStep(step or 1)
		frame.setup = nil
	end

	local function Constructor()
		local self = AceGUI:Create("Slider")
		self.type = widgetType

		self.OnRelease = OnRelease
		self.OnAcquire = OnAcquire
		
		self.UpdateText = UpdateText
		self.SetList = SetList
		self.SetValue = SetValue
		self.SetSliderValues = SetSliderValues
		
		self.editbox:EnableMouse(false)
		self.editbox:SetScript('OnEditFocusGained', function(self) self:ClearFocus() end)
		self.slider:SetScript("OnValueChanged",Slider_OnValueChanged)
		self.slider:SetScript("OnMouseUp", Slider_OnMouseUp)
		self.slider:SetScript("OnMouseDown", Slider_OnMouseDown)
		self.slider:EnableMouseWheel(true)
		
		return self
	end
	
	AceGUI:RegisterWidgetType(widgetType,Constructor,widgetVersion)
end
