Quantcast

-- Grab a copy of the local data
local addonName, addonData = ...
local Translate = addonData.Translate
local expansionLevel = GetExpansionLevel()


addonData.UpdateAlpha = function(self,button)

	if ( not button ) then return end
	if ( not button.spellID  ) then return end

	local spellName, spellRank, spellIcon = GetSpellInfo(button.spellID);

	local spellKnown = ( GetSpellInfo(spellName) ~= nil )
	local isSolo = true
	local isGrouped = ( GetNumGroupMembers() > 0 )

	if ( isGrouped ) then
		isSolo = false
	end
	if ( not spellKnown ) then
		button:SetAlpha(XMagePortsDB.NotAvailableAlpha)
	else
		if ( ( isSolo and button.spellType == "PORTAL" ) or ( isGrouped and button.spellType == "TELEPORT" ) ) then
			button:SetAlpha(XMagePortsDB.NotIdealAlpha)
		else
			button:SetAlpha(XMagePortsDB.AvailableAlpha)
		end
	end
end


addonData.CreateSecureSpellButton = function(self,spellID,spellType)

	-- Protected Function call to catch errors
	local retOK, spellName, spellRank, spellIcon = pcall (GetSpellInfo, spellID);

	-- This spell isn't available yet so don't worry about it
	if ( not spellName ) then return nil end

	local spellKnown = IsSpellKnown(spellID)
	local buttonName = "XMP_" .. spellID

	local Button = CreateFrame("Button",buttonName,UIParent,"ActionButtonTemplate,SecureActionButtonTemplate")
	Button:SetWidth(36)
	Button:SetHeight(36)

	Button.spellID = spellID
	Button.spellType = spellType

	Button:SetAttribute("type","spell")
	Button:SetAttribute("spell",spellName)

	local fontStructure = {}
	fontStructure.Size = 16
	fontStructure.Color = { r = 255, g = 255, b = 255, a = 255 }
	fontStructure.Template = "NumberFontNormalLargeYellow"

	self:CreateCooldown(Button,spellIcon,fontStructure)

	Button:RegisterEvent("PLAYER_LOGIN")
	Button:RegisterEvent("PARTY_CONVERTED_TO_RAID")
	Button:RegisterEvent("PARTY_INVITE_CANCEL")
	Button:RegisterEvent("PARTY_INVITE_REQUEST")
	Button:RegisterEvent("LEARNED_SPELL_IN_TAB");
	Button:RegisterEvent("PLAYER_ENTERING_WORLD");
	Button:RegisterEvent("UNIT_SPELLCAST_START");
	Button:RegisterEvent("UNIT_SPELLCAST_STOP");

	Button:SetScript("OnEvent",function(self,event,...)
		if self:GetNormalTexture() ~= ""
		then self:SetNormalTexture( "" );
		end
		addonData:UpdateAlpha(self)
	end)

	Button:Show()
	return Button

end