local B = LibStub("AceAddon-3.0"):GetAddon("BFGadgets")
if not B then return end
local V = B:NewModule("Volume")
local S
local miniMapButton

local function CreateSlider()
	S = CreateFrame("Slider", "BFVolumeSlider", nil, "VolumeSliderTemplate")
	
	S:SetPoint("TOP",VolumeMiniButton,"BOTTOM")
	S:SetValueStep(0.1)
	S:SetMinMaxValues(0,1)
	
	S:EnableMouseWheel(true)
	S.time=0
	S.text = _G[S:GetName() .. 'Text']
	S.text:SetPoint("BOTTOM",S,"BOTTOM",25,10)
	S.low = _G[S:GetName() .. 'Low']
	S.high = _G[S:GetName() .. 'High']
	S.low:Hide()
	S.high:Hide()
	
end

local function SetSliderSCript()
	S:SetScript("OnShow",function(self)
		local __volume = GetCVar("Sound_MasterVolume")
		self:SetValue(1-__volume)
		self.text:SetText(tostring(ceil(__volume * 100)).."%")		
	end)
	S:SetScript("OnValueChanged",function(self, value)
		S:SetValue(value)
		BlizzardOptionsPanel_SetCVarSafe("Sound_MasterVolume", 1-value);
		S.text:SetText(tostring(ceil((1-value) * 100)).."%");
		S.time=0
	end)
	
	S:SetScript("OnMouseWheel",function(self,delta) 
		if delta==1 then
			if S:GetValue() == 1 then
				S:SetValue(0.8)
			else
				S:SetValue(max(S:GetValue()-0.1,0))
			end
		elseif delta==-1 then
			S:SetValue(min(S:GetValue()+0.1,1))
		end
	end)
	
	S:SetScript("OnUpdate",function(self, elapsed)
		S.time=S.time+elapsed;
		if S.time>=2 then
			S:Hide()
			S.time=0
		end
	end)
	
end

local function InitVolume()
	CreateSlider()
	SetSliderSCript()
	miniMapButton = _G.VolumeMiniButton
	S:Hide()
end

function Volume_MiniFrame_OnClick()
	if S:IsShown() then
		S:Hide()
		S.time=0
	else
		S:Show()
	end
end


function V:OnEnable()
	InitVolume()
	miniMapButton:Show()
end

function V:OnDisable()
	miniMapButton:Hide()
end

--interface for current bigfoot switches
function BigFoot_EnableVolume(flag)
	if flag then
		V:Enable()
	else
		V:Disable()
	end
end