local function GetItemID(itemName)
	return select(5, string.find(select(2, GetItemInfo(itemName)), "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?"))
end

local function GetAbilityItems()
	local itemList = {}
	local count = 1
	local bagSize
	for bag = 0, NUM_BAG_SLOTS do
		bagSize = GetContainerNumSlots(bag);
		if bagSize > 0 then
			for i = 1, bagSize do
				local itemId = GetContainerItemID(bag, i)
				if itemId then
					local itemName = GetItemInfo(itemId) 
					local itemSpell, itemSpellId = GetItemSpell(itemId)
					if itemSpellId then
						itemList[count] = itemName
						count = count+1
					end
				end
			end
		end
	end
	for i = 1,20 do 
		local itemId = GetInventoryItemID("player", i)
		if itemId then
			local itemName = GetItemInfo(itemId) 
			local itemSpell, itemSpellId = GetItemSpell(itemId)
			if itemSpellId then
				itemList[count] = itemName
				count = count+1
			end
		end
	end
	return itemList;
end

local function GetEquippableItems()
	local itemList = {}
	local count = 1
	local bagSize
	for bag = 0, NUM_BAG_SLOTS do
		bagSize = GetContainerNumSlots(bag);
		if bagSize > 0 then
			for i = 1, bagSize do
				local itemId = GetContainerItemID(bag, i)
				if itemId then
					local itemName = GetItemInfo(itemId) 
					if IsEquippableItem(itemId) then
						itemList[count] = itemName
						count = count+1
					end
				end
			end
		end
	end
	for i = 1,20 do 
		local itemId = GetInventoryItemID("player", i)
		if itemId then
			local itemName = GetItemInfo(itemId) 
			itemList[count] = itemName
			count = count+1
		end
	end
	return itemList;
end

WhatsNext:AddConditionalStatement("Item Equipped",{
	category = "Items",
	text = "player is wearing {itemName}.",
	test = function(checkTable) 
		return IsEquippedItem(checkTable.itemName) == 1
	end,
	arguments = {
		itemName = {label = "Item", type = "item", default = "",suggestionList=GetEquippableItems},
	}
})

WhatsNext:AddConditionalStatement("Item On Cooldown",{
	category = "Items",
	text = "{itemName} will be on cooldown for more than {time} seconds.",
	test = function(checkTable) 
		local start, duration, enable = GetItemCooldown(GetItemID(checkTable.itemName))
		return start + duration - GetTime() > tonumber(checkTable.time)
	end,
	arguments = {
		itemName = {label = "Item", type = "item", default = "",suggestionList=GetAbilityItems},
		time = {label = "Seconds", type = "integer", default = 3, min = 1, max = 300},
	}
})

WhatsNext:AddConditionalStatement("Item Disabled",{
	category = "Items",
	text = "{itemName}'s cooldown timer won't start until out of combat.",
	test = function(checkTable) 
		return select(3,GetItemCooldown(checkTable.itemName)) == 0
	end,
	arguments = {
		itemName = {label = "Item", type = "item", default = "",suggestionList=GetAbilityItems},
	}
})