do
	BrandnewManaStone = {}
	BrandnewManaStone.destroyTime = -1
end

local CheckCasting_Name = function (spellName)
	BrandnewManaStone:CheckCasting(spellName)
end

local CheckCasting_Action = function (buttonID)
	local type, spellID, spellType = GetActionInfo(buttonID)

	if type == "spell"
	then
		BrandnewManaStone:CheckCasting(GetSpellName(spellID, spellType))
	end
end

local CheckCasting = function (spellID, spellType)
	BrandnewManaStone:CheckCasting(GetSpellName(spellID, spellType))
end

function BrandnewManaStone:CheckCasting(spellName)
	if not InCombatLockdown() and spellName == BRANDNEWMANASTONE_SPELLNAME
	then
		if self:DestroyItem(BRANDNEWMANASTONE_ITEMNAME) and GetTime() > self.destroyTime + 1
		then
			DEFAULT_CHAT_FRAME:AddMessage(BRANDNEWMANASTONE_MESSAGE)
			self.destroyTime = GetTime()
		end
	end
end

function BrandnewManaStone:FindItemPosition(itemName)
	local bagID, slotNum, slotID, itemLink

	for bagID = 0, 4, 1
	do
		slotNum = GetContainerNumSlots(bagID)

		for slotID = 1, slotNum, 1
		do
			itemLink = GetContainerItemLink(bagID, slotID)
			if itemLink ~= nil and string.find(GetItemInfo(itemLink), itemName)
			then
				return bagID, slotID
			end
		end
	end

	return nil, nil
end

function BrandnewManaStone:DestroyItem(bagID, slotID)
	if slotID == nil and type(bagID) == "string"
	then
		bagID, slotID = self:FindItemPosition(bagID)
	end

	if bagID == nil
	then
		return false
	end

	-- for safety!
	if CursorHasItem()
	then
		ClearCursor()
	end

	PickupContainerItem(bagID, slotID)
	DeleteCursorItem()

	return true
end

-- do once

do
	hooksecurefunc("CastSpellByName", CheckCasting_Name)
	hooksecurefunc("CastSpell", CheckCasting)
	hooksecurefunc("UseAction", CheckCasting_Action)
end
