--[[

        SetMultiBars:  Added more options to Blizzard's extra action bars.
	
	Based on ButtonLabels mod and Bar Options.
	
	** Features
	1. You can show or hide bind key labels on blizzard's MultiBars.
	2. You can show only binding keys' suffix label.
	3. You can change button colors to 'red' when spell or skill range out of target.
	4. Support to instant action bar change bind keys.
	5. You can change MultiBarLeft and MultiBarRight to horizon bar.
]]

SetMultiBars_HideKey = 0;
SetMultiBars_HideKeyMod = 0;
SetMultiBars_RangedColor = 1;

Instant_ActionBar_Return = -1;

SetMultiBarsVars = {
	["Action"] = 1,
	["Action_Scale"] = 36,
	["Shapeshift"] = 1,
	["Shapeshift_Scale"] = 30,
	["MultiBarBottomLeft"] = 1,
	["MultiBarBottomLeft_Scale"] = 36,
	["MultiBarBottomRight"] = 1,
	["MultiBarBottomRight_Scale"] = 36,
	["MultiBarRight"] = 0,
	["MultiBarRight_Scale"] = 36,
	["MultiBarLeft"] = 0,
	["MultiBarLeft_Scale"] = 36
};

-- Based on code from Iriel's RedRange
function SB_ActionButton_OnUpdate(elapsed)
	local self = this
	local t = self.rangeTimer
	if (not t) then
		return;
	end
	if ((t == TOOLTIP_UPDATE_TIME) or (t <= 0)) then
		local newRange = nil
		local id = self.action
		if ( ActionHasRange(id) and (IsActionInRange(id) == 0) ) then
			newRange = true
		end
		if ( self.abcRangeFlag ~= newRange ) then
			self.abcRangeFlag = newRange
			ActionButton_UpdateUsable()
		end
	end
end

-- Based on code from Iriel's RedRange
function SB_ActionButton_UpdateUsable()
	local self = this
	local id = self.action
	if (not id) then
		return
	end
	local isUsable, notEnoughMana = IsUsableAction(id)
	if ( isUsable and SetMultiBars_RangedColor == 1 ) then
		if (ActionHasRange(id) and IsActionInRange(id) == 0) then
			local name = self:GetName()
			local icon = getglobal(name.."Icon")
			local normalTexture = getglobal(name.."NormalTexture")
			icon:SetVertexColor(1.0, 0.5, 0.5);
			normalTexture:SetVertexColor(1.0, 0.5, 0.5);
			return
		end
	elseif ( notEnoughMana ) then
		local name = self:GetName()
		local icon = getglobal(name.."Icon")
		local normalTexture = getglobal(name.."NormalTexture")
		icon:SetVertexColor(0.5, 0.5, 1.0);
		normalTexture:SetVertexColor(0.5, 0.5, 1.0);
	end
end

hooksecurefunc("ActionButton_OnUpdate", SB_ActionButton_OnUpdate);
hooksecurefunc("ActionButton_UpdateUsable", SB_ActionButton_UpdateUsable);

function SetMultiBars_InstantPage(page)
	if ( GetActionBarPage() ~= page ) then
		if ( Instant_ActionBar_Return == -1 ) then
			Instant_ActionBar_Return = GetActionBarPage();
		end
		ChangeActionBarPage(page);
	end
end

function SetMultiBars_OnLoad()
	if ( Cosmos_RegisterConfiguration ) then
		Cosmos_RegisterConfiguration(
			"COS_SETMULTI",
			"SECTION",
			SETMULTI_SEP,
			SETMULTI_SEP_INFO
			);
		Cosmos_RegisterConfiguration(
			"COS_SETMULTIHEADER",
			"SEPARATOR",
			SETMULTI_SEP,
			SETMULTI_SEP_INFO
			);
		local barNames = { "Action", "Shapeshift", "MultiBarBottomLeft", "MultiBarBottomRight", "MultiBarLeft", "MultiBarRight" };
		for index, barName in pairs(barNames) do
			local cosvar = "COS_SETMULTI_"..strupper(barName);
			local text = getglobal("SETMULTI_"..strupper(barName).."_HORIZON");
			local subtext = getglobal("SETMULTI_"..strupper(barName).."_HORIZON_INFO");
			if ( barName ) then
				Cosmos_RegisterConfiguration(
					cosvar,
					"BOTH",													--Things to use
					text,
					subtext,
					function(checked, value)
						SetMultiBarsVars[barName] = checked;
						SetMultiBarsVars[barName.."_Scale"] = value;
						SetMultiBars_SetupBars(barName, checked, value);
					end,					--Callback
					SetMultiBarsVars[barName],
					SetMultiBarsVars[barName.."_Scale"],
					20,
					52,
					SETMULTI_SLIDER_HEADER,
					1,
					1,
					"",
					1
				);
			end

		end
		Cosmos_RegisterConfiguration(
			"COS_SETMULTIKEY",
			"CHECKBOX",
			SETMULTI_HIDEKEY, 
			SETMULTI_HIDEKEY_INFO,
			SetMultiBarsHideKey_Toggle,
			SetMultiBars_HideKey
			);
		Cosmos_RegisterConfiguration(
			"COS_SETMULTIKEYMOD",
			"CHECKBOX",
			SETMULTI_HIDEKEYMOD, 
			SETMULTI_HIDEKEYMOD_INFO,
			SetMultiBarsHideKeyMod_Toggle,
			SetMultiBars_HideKeyMod
			);
		Cosmos_RegisterConfiguration(
			"COS_SETMULTIRANGE",
			"CHECKBOX",
			SETMULTI_RANGEDCOLOR, 
			SETMULTI_RANGEDCOLOR_INFO,
			SetMultiBarsRangedColor_Toggle,
			SetMultiBars_RangedColor
			);
	end
	this:RegisterEvent("UPDATE_BINDINGS");

	--hooksecurefunc("ActionButtonDown", SB_ActionButtonDown);
	--hooksecurefunc("ActionButtonUp", SB_ActionButtonUp);
end

function SetMultiBars_OnEvent(event)
	if ( event == "UPDATE_BINDINGS" ) then
		SetMultiBars_Update();
	end
end

function SetMultiBarsHideKey_Toggle(toggle)
	if ( toggle == 1 ) then
		SetMultiBars_HideKey = 1;
		SetMultiBars_Update();
	else
		SetMultiBars_HideKey = 0;
		SetMultiBars_Update();
	end
end

function SetMultiBarsHideKeyMod_Toggle(toggle)
	if ( toggle == 1 ) then
		SetMultiBars_HideKeyMod = 1;
		SetMultiBars_Update();
	else
		SetMultiBars_HideKeyMod = 0;
		SetMultiBars_Update();
	end
end

function SetMultiBarsRangedColor_Toggle(toggle)
	if ( toggle == 1 ) then
		SetMultiBars_RangedColor = 1;
	else
		SetMultiBars_RangedColor = 0;
	end
end

function SetMultiBars_UpdateKeybind(bar, id)
        local label, command, text

	for i = 1, 12 do
		label = getglobal(bar.."Button"..i.."HotKey");
		if ( SetMultiBars_HideKey ~= 1 ) then
			command = "MULTIACTIONBAR"..id.."BUTTON"..i;
			text = GetBindingText(GetBindingKey(command),"KEY_", 1);
			if ( SetMultiBars_HideKeyMod == 1 ) then
				while ( ( text ) and ( strlen(text) > 1 ) and (string.find(text, "-")) and (text ~= "KP -") and (text ~= "Num Pad -")) do
					firsti, lasti, text = string.find (text, "-(.+)");
				end
			end
			label:SetText(text);
			label:Show();
		else
			label:SetText("");
		end
	end
end

function SetMultiBars_SetupBars(bar, horizon, scale)
	if ( ChangeBar_Settings and ChangeBar_Settings[GetCVar("realmName").."|"..UnitName("player")].Style == 2 ) then
		return;
	end
	if ( bar and type(bar) == "string" ) then
		if ( bar == "Shapeshift" ) then
			if ( scale ) then
				for i=1, 10 do
					getglobal(bar.."Button"..i):SetWidth(scale);
					getglobal(bar.."Button"..i):SetHeight(scale);
					getglobal(bar.."Button"..i.."NormalTexture"):SetWidth(math.ceil(scale*1.83));
					getglobal(bar.."Button"..i.."NormalTexture"):SetHeight(math.ceil(scale*1.83));
					getglobal(bar.."Button"..i.."Border"):SetWidth(math.ceil(scale*1.83));
					getglobal(bar.."Button"..i.."Border"):SetHeight(math.ceil(scale*1.83));
				end
			end
			if ( horizon and horizon == 1 ) then
				for i=1,10 do
					if i > 1 then
						getglobal(bar.."Button"..i):ClearAllPoints();
						getglobal(bar.."Button"..i):SetPoint("LEFT", bar.."Button"..i-1, "RIGHT", 6, 0);
					end
				end
			elseif ( horizon == 0 ) then
				for i=1,10 do
					if i > 1 then
						getglobal(bar.."Button"..i):ClearAllPoints();
						getglobal(bar.."Button"..i):SetPoint("TOP", bar.."Button"..i-1, "BOTTOM", 0, -6);
					end
				end
			end
		else
			if ( scale ) then
				for i=1, 12 do
					getglobal(bar.."Button"..i):SetWidth(scale);
					getglobal(bar.."Button"..i):SetHeight(scale);
					getglobal(bar.."Button"..i.."NormalTexture"):SetWidth(math.ceil(scale*1.83));
					getglobal(bar.."Button"..i.."NormalTexture"):SetHeight(math.ceil(scale*1.83));
					getglobal(bar.."Button"..i.."Border"):SetWidth(math.ceil(scale*1.83));
					getglobal(bar.."Button"..i.."Border"):SetHeight(math.ceil(scale*1.83));
				end
			end
			if ( horizon and horizon == 1 ) then
				for i=1,12 do
					if i > 1 then
						getglobal(bar.."Button"..i):ClearAllPoints();
						getglobal(bar.."Button"..i):SetPoint("LEFT", bar.."Button"..i-1, "RIGHT", 6, 0);
					end
				end
			elseif ( horizon == 0 ) then
				for i=1,12 do
					if i > 1 then
						getglobal(bar.."Button"..i):ClearAllPoints();
						getglobal(bar.."Button"..i):SetPoint("TOP", bar.."Button"..i-1, "BOTTOM", 0, -6);
					end
				end
			end
		end
	end
end

function SetMultiBars_Update()
	SetMultiBars_UpdateKeybind("MultiBarBottomLeft", 1);
	SetMultiBars_UpdateKeybind("MultiBarBottomRight", 2);
	SetMultiBars_UpdateKeybind("MultiBarRight", 3);
	SetMultiBars_UpdateKeybind("MultiBarLeft", 4);
end

local SBActionButtonPushedIDs = {};
function SB_ActionButtonDown(id)
	local button;
	if ( BonusActionBarFrame:IsShown() ) then
		button = getglobal("BonusActionButton"..id);
	else
		button = getglobal("ActionButton"..id);
	end
	if ( button:GetButtonState() == "PUSHED" ) then
		SBActionButtonPushedIDs[id] = true;
	end
end

function SB_ActionButtonUp(id)
	local button;
	if ( BonusActionBarFrame:IsVisible() ) then
		button = getglobal("BonusActionButton"..id);
	else
		local button = getglobal("ActionButton"..id);
	end
	if ( SBActionButtonPushedIDs[id] and Instant_ActionBar_Return ~= -1 ) then
		for k,_ in pairs(SBActionButtonPushedIDs) do
			SBActionButtonPushedIDs[k] = nil;
		end
--	Instant Action Bar START
		ChangeActionBarPage(Instant_ActionBar_Return);
		Instant_ActionBar_Return = -1;
--	Instant Action Bar end
	end
end