--[[

	Scorpio's AddOns

	By Scorpio of <Sisu> http://sisuguild.fi

]]


-- Local variables
local SAD_Saved_BuffButton_Update = nil;
local SAD_Saved_BuffButton_OnUpdate = nil;
local BuffButtonsMoved = 0;

-- OnLoad
function SAD_BuffDuration_OnLoad()

	SlashCmdList["SAD_BUFF_DURATION"] = SAD_BuffDuration_SlashCommandHandler;
	SLASH_SAD_BUFF_DURATION1 = "/sadbuffdur";
	this:RegisterEvent("ADDON_LOADED");

end

-- OnEvent
function SAD_BuffDuration_OnEvent(event)

	if((event == "ADDON_LOADED") and (arg1 == "SAD_BuffDuration")) then
		SAD_BuffDuration_Enable(SAD_BUFF_DURATION);
	end

end

-- Slash command handler
function SAD_BuffDuration_SlashCommandHandler(msg)

	-- make all lowercase to ease checking
	msg = string.lower(msg);

	if((string.find(msg, 'enable')) or (string.find(msg, 'true'))) then
	
		SAD_BuffDuration_Enable(1);
		
	elseif((string.find(msg, 'disable')) or (string.find(msg, 'false'))) then
	
		SAD_BuffDuration_Enable(0);
	
	else

		local helptext = "/sadbuffdur enable|true|disable|false";
		
		SAD_Output(helptext);
	
	end

end

-- Get BuffButton Duration Field to be Changed
local function GetBuffButtonDurationField(buttonname)

	local ignored1, ignored2, num = string.find(buttonname, "BuffButton(%d+)");
	return getglobal("BuffDuration"..num.."Time");
	
end

-- Enable Buff Duration Timer
function SAD_BuffDuration_Enable(toggle)

	if(toggle == 1) then
		
		if(SAD_Saved_BuffButton_Update == nil) then
			SAD_Saved_BuffButton_Update = BuffButton_Update;
			BuffButton_Update = SAD_BuffDuration_BuffButton_Update;
		end
			
		if(SAD_Saved_BuffButton_OnUpdate == nil) then
			SAD_Saved_BuffButton_OnUpdate = BuffButton_OnUpdate;
			BuffButton_OnUpdate = SAD_BuffDuration_BuffButton_OnUpdate;
		end
			
		if(BuffButtonsMoved == 0) then
			SAD_BuffDuration_MoveBuffButtons(-5, -11);
			BuffButtonsMoved = 1;
		end
			
		SAD_Trace("BuffDuration Enabled");
			
	else
		
		SAD_BuffDuration_CleanUp();
		
		if(SAD_Saved_BuffButton_Update ~= nil) then
			BuffButton_Update = SAD_Saved_BuffButton_Update;
			SAD_Saved_BuffButton_Update = nil;
		end
			
		if(SAD_Saved_BuffButton_OnUpdate ~= nil) then
			BuffButton_OnUpdate = SAD_Saved_BuffButton_OnUpdate;
			SAD_Saved_BuffButton_OnUpdate = nil;
		end
			
		if(BuffButtonsMoved ~= 0) then
			SAD_BuffDuration_MoveBuffButtons(-5, 0);
			BuffButtonsMoved = 0;
		end
			
		SAD_Trace("BuffDuration Disabled");
		
	end
	
	SAD_BUFF_DURATION = toggle;

end

-- Clean Up
function SAD_BuffDuration_CleanUp()

	local ButtonNum;
	
	for ButtonNum = 0, 23 do
	
		local ButtonVar = getglobal("BuffButton"..ButtonNum);
		
		if(ButtonVar ~= nil) then
		
			local buttonname = ButtonVar:GetName();
			GetBuffButtonDurationField(buttonname):SetText("");
		
		end
		
	end
	
end

-- Relocate BuffButtons so the Texts Will Fit Better
function SAD_BuffDuration_MoveBuffButtons(xos, yos)

	local TempButton;
	
	getglobal("BuffButton8"):SetPoint("TOP", "BuffButton0", "BOTTOM", 0, yos);
	
	for TempButton = 9, 15 do
	
		getglobal("BuffButton"..TempButton):SetPoint("RIGHT", "BuffButton"..(TempButton - 1), "LEFT", xos, 0);
		
	end
	
	getglobal("BuffButton16"):SetPoint("TOP", "BuffButton8", "BOTTOM", 0, yos);
	
	for TempButton = 17, 23 do
	
		getglobal("BuffButton"..TempButton):SetPoint("RIGHT", "BuffButton"..(TempButton - 1), "LEFT", xos, 0);
		
	end

end

-- Update BuffButton
function SAD_BuffDuration_BuffButton_Update()

	if(SAD_Saved_BuffButton_Update ~= nil) then
	
		SAD_Saved_BuffButton_Update();
		
	end
	
	if( this.untilCancelled ) then
	
		local buttonname = this:GetName();
		GetBuffButtonDurationField(buttonname):SetText("");
		
	end
	
end

-- BuffButton Update Event Handler
function SAD_BuffDuration_BuffButton_OnUpdate()

	if(SAD_Saved_BuffButton_OnUpdate ~= nil) then
	
		SAD_Saved_BuffButton_OnUpdate();
		
	end
	
	if( BuffFrameUpdateTime <= 0 ) then
	
		local buttonname = this:GetName();
		local duration = GetPlayerBuffTimeLeft(this.buffIndex);
		
		GetBuffButtonDurationField(buttonname):SetText(SAD_FormatTimeText(duration), false);
		
	end
	
end