
assert( oRA, "oRA not found!")

------------------------------
--      Are you local?      --
------------------------------

local L = AceLibrary("AceLocale-2.0"):new("oRAPItem")

-- DO NOT translate these, use the locale tables below
local reagents = {
	["PRIEST"] = "SacredCandle",
	["MAGE"] = "ArcanePowder",
	["DRUID"] = "WildThornroot",
	["WARLOCK"] = "SoulShard",
	["SHAMAN"] = "Ankh",
	["PALADIN"] = "SymbolofDivinity",
	["ROGUE"] = "FlashPowder",
}

----------------------------
--      Localization      --
----------------------------

L:RegisterTranslations("enUS", function() return {
	["itemparticipant"] = true,
	["item"] = true,
	["Options for item checks."] = true,
	["SacredCandle"] = "Sacred Candle",
	["ArcanePowder"] = "Arcane Powder",
	["WildThornroot"] = "Wild Thornroot",
	["Ankh"] = "Ankh",
	["SoulShard"] = "Soul Shard",
	["SymbolofDivinity"] = "Symbol of Divinity",
	["FlashPowder"] = "Flash Powder",
	["Participant/Item"] = true,
} end )

----------------------------------
--      Module Declaration      --
----------------------------------

oRAPItem = oRA:NewModule(L["itemparticipant"])
oRAPItem.defaults = {
}
oRAPItem.participant = true
oRAPItem.name = L["Participant/Item"]
-- oRAPItem.consoleCmd = L"item"
-- oRAPItem.consoleOptions = {
-- 	type = "group",
-- 	desc = L"Options for item checks.",
-- 	args = {
--	}
-- }

------------------------------
--      Initialization      --
------------------------------

function oRAPItem:OnInitialize()
	self.debugFrame = ChatFrame5
end

function oRAPItem:OnEnable()
	self:RegisterCheck("ITMC", "oRA_ItemCheck")
	self:RegisterCheck("REAC", "oRA_ReagentCheck")
end

function oRAPItem:OnDisable()
	self:UnregisterAllEvents()
	self:UnregisterCheck("ITMC")
	self:UnregisterCheck("REAC")
end


-------------------------
--   Event Handlers    --
-------------------------

function oRAPItem:oRA_ItemCheck( msg, author)
	if not self:IsValidRequest(author) then return end
	msg = self:CleanMessage(msg)
	local _, _, itemname = string.find(msg, "^ITMC (.+)$")
	if itemname then
		local numitems = self:GetItems(itemname)
		if numitems and numitems > 0 then
			self:SendMessage("ITM "..numitems.." "..itemname.." "..author)
		end
	end

end

function oRAPItem:oRA_ReagentCheck(msg, author)
	if not self:IsValidRequest(author) then return end
	msg = self:CleanMessage(msg)
	local numitems = self:GetReagents()
	if numitems and numitems > 0 then
		self:SendMessage("REA " .. numitems .. " " .. author )
	end
end


-------------------------
--  Utility Functions  --
-------------------------

function oRAPItem:GetItems( itemname )
	local numitems = 0
	for bag = 4, 0, -1 do
		local size = GetContainerNumSlots(bag)
		if size > 0 then
			for slot=1, size, 1 do
				local ilink = GetContainerItemLink(bag,slot)
				if ilink then
					local _, _, name = string.find(ilink, "%[(.+)%]")
					
					if string.find(name, itemname) then
						local _, itemcount, _, _, _ = GetContainerItemInfo(bag,slot)
						numitems = numitems + itemcount
					end
				end
			end
		end
	end
	return numitems
end

function oRAPItem:GetReagents()
	local numitems = -1
	if UnitClass("player") then
		local _,class = UnitClass("player")
		if reagents[class] then
			numitems = self:GetItems( L(reagents[class]) )
		end
	end
	return numitems
end
