local E = unpack(ElvUI)
local _G = _G
local CreateFrame = _G.CreateFrame
local UIParent = _G.UIParent
local BackdropTemplateMixin = _G.BackdropTemplateMixin
local IsInInstance = _G.IsInInstance
local GetItemClassInfo = _G.C_Item and _G.C_Item.GetItemClassInfo or _G.GetItemClassInfo
local InCombatLockdown = _G.InCombatLockdown
local GetBindingKey = _G.GetBindingKey
local SetBindingClick = _G.SetBindingClick
local HandleModifiedItemClick = _G.HandleModifiedItemClick
local GameTooltip = _G.GameTooltip
local GetItemInfo = _G.C_Item and _G.C_Item.GetItemInfo or _G.GetItemInfo
local ipairs = _G.ipairs
local C_Container = _G.C_Container
local GetContainerNumSlots = C_Container.GetContainerNumSlots
local GetContainerItemLink = C_Container.GetContainerItemLink
local GetContainerItemInfo = C_Container.GetContainerItemInfo
local GetContainerItemCooldown = C_Container.GetContainerItemCooldown
local tonumber = _G.tonumber
local GetInventorySlotInfo = _G.GetInventorySlotInfo
local GetInventoryItemLink = _G.GetInventoryItemLink
local GetInventoryItemCooldown = _G.GetInventoryItemCooldown
local CooldownFrame_Set = _G.CooldownFrame_Set
local _, instanceType
local GetItemSpell = _G.C_Item and _G.C_Item.GetItemSpell or _G.GetItemSpell
local AB = E:GetModule('ActionBars')


--A merge of QBAr by Aezay with a few edits by Eltreum
--This module is GNU GPL v3
local EltruismQuestItemFrame = CreateFrame("Frame", "EltruismQuestItem", UIParent, BackdropTemplateMixin and "BackdropTemplate")	-- 9.0.1: Using BackdropTemplate
EltruismQuestItemFrame:SetPoint("BOTTOM", E.UIParent, "BOTTOM", 0, 34)
E:CreateMover(EltruismQuestItemFrame, "MoverEltruismQuestItem", "EltruismQuestItemBar", nil, nil, nil, "ALL,SOLO,ELTREUMUI", nil, 'ElvUI_EltreumUI,quests,item')
EltruismQuestItemFrame.tip = CreateFrame("GameTooltip","EltruismQuestItemTip",nil,"GameTooltipTemplate")
EltruismQuestItemFrame.tip:SetOwner(UIParent,"ANCHOR_NONE")
EltruismQuestItemFrame.items = {}
EltruismQuestItemFrame.debug = false

-- Constants
local UPDATE_DELAY = 1.0 --was 0.5 but i think that might be too low
local ITEMID_PATTERN = "item:(%d+)"
local QUEST_TOKEN = (GetItemClassInfo and GetItemClassInfo(LE_ITEM_CLASS_QUESTITEM or 12) or LOOT_JOURNAL_LEGENDARIES_SOURCE_QUEST or "Quest") -- Obtain the localization of the "Quest" type for items -- [7.0.3/Legion] API Removed: GetAuctionItemClasses()

local slots = {
	"HeadSlot", "NeckSlot", "ShoulderSlot", "BackSlot", "ChestSlot", "ShirtSlot", "TabardSlot", "WristSlot",
	"HandsSlot", "WaistSlot", "LegsSlot", "FeetSlot", "Finger0Slot", "Finger1Slot", "Trinket0Slot", "Trinket1Slot",
	"MainHandSlot", "SecondaryHandSlot",
}

-- These items are not marked as being quest items, but we want to include them anyway
local qItems = {
	--6948, --hearthstone as a test item
	--127770, --another one as test
	972,
	3985,
	4854,
	4945,
	5411,
	5456,	-- Divining Scroll (item has no Use: text, even though you can use it)
	5996,
	6372,
	6636,
	7843,
	8432,
	8474,
	8529,
	10569,
	10687,
	10688,
	10689,
	10690,
	10695,
	11116,
	11568,
	11582,	-- Fel Salve
	11914, -- Cursed Ooze Jar
	11948,	-- Tainted Ooze Jar
	11955,
	12565,
	12886,
	12922,	-- Empty Canteen
	16302,
	16321,
	16790,
	20483,
	21713,	-- Elune's Candle (Lunar Festival)
	23361,	-- Cleansing Vial
	23417,
	23645,
	23792,	-- Tree Disguise Kit
	23818,	-- Stillpine Furbolg Language Primer
	24084,	-- Draenei Banner
	24278,	-- Flare Gun
	24330,
	24335,
	24355,
	24355,	-- Ironvine Seeds
	24421,
	24467, -- living fire
	24474,
	24501,	-- Gordawg's Boulder
	24504,
	24558,
	24559,
	25458, -- mag'har battle standard
	25465,	-- Stormcrow Amulet
	25539,
	25552,	-- Warmaul Ogre Banner
	25555,	-- Kil'sorrow Banner
	25658,	-- Damp Woolen Blanket
	25853,	-- Pack of Incendiary Bombs (Old Hillsbrad)
	28038,	-- Seaforium PU-36 Explosive Nether Modulator
	28132,	-- Area 52 Special
	28607, -- Sunfury Disguise
	29324,
	29443,
	29473,
	29588,
	29590,
	29778,
	30105,
	30540,
	30576,
	30712,
	30719,
	30811,
	31121,
	31122,
	31495,
	31518,
	31664,
	31702,
	31955,
	32385,
	32386,
	32405,
	32406,
	32726,
	32971,	-- Water Bucket (Hallow's End)
	33096,	-- Complimentary Brewfest Sampler (Brew Fest)
	33349, --plague vials
	33614, --empty apothecary's flask
	33615, --flask of vrykul blood
	33621, --plague spray
	33634,	-- Orehammer's Precision Bombs, quest from Howling Fjord
	34023, --empty apothecary's flask
	34024, --flask of vrykul blood
	34076,
	34475, -- arcane charges
	34483, --orb of murloc control
	34871,
	35233, --multiphase spectographic goggles
	35704,
	35792,
	36770,
	36771,
	37173,
	37265,
	37445,
	37661, --gossamer potion
	37708,
	37877,
	38351,
	38657,
	38659,
	38676,
	38684,
	38689,
	38697,
	38699,
	38701,
	38709,
	38731,
	39041,
	39154,
	39157,
	39158,
	39164,
	39165,
	39187,
	39206,
	39238,
	39253,
	39268,
	39566,
	39574,
	39576,
	39615,
	39645,
	39664,
	39700,
	39737,
	40390,
	40397,
	40551,
	40587,
	40676,
	40730,
	40731,
	40732,
	40946,
	41131,
	41179,
	41340,
	41366,
	41372,
	41390,
	41430,
	41431,
	41615,
	41776,
	41988,
	42164,
	42419,
	42424,
	42441,
	42442,
	42479,
	42480,
	42481,
	42499,
	42624,
	42679,
	42769,
	42774,
	42781,
	42797,
	42837,
	42840,
	42918,
	42928,
	43101,
	43139,
	43142,
	43147,
	43153,
	43166,
	43206,
	43243,
	43289,
	43315,
	43524,
	43564,
	43608,
	43968,
	44048,
	44064,
	44065,
	44127,
	44186,
	44212,
	44222,
	44251,
	44304,
	44307,
	44433,
	44450,
	44653,
	44704,
	44890,
	44950,
	45067,	-- Egg Basket -- Az: offhand item, but I wanted it on my bar for a hotkey
	46861,	-- Bouquet of Orange Marigolds (Day of the Dead)
	49132, -- fireliminator x-21
	49278,	-- Goblin Rocket Pack (ICC - Lootship)
	49368, -- ambassador disquise
	56909,	-- Earthen Ring Unbinding Totem (Cata event)
	60501, 	-- Stormstone, Deepholm Quest
	185956,
	180008, --resonating anima core
	45072, --noblegarden egg that has to be opened
}
local blocklist = {
	[176809] = true, -- junk item that for some reason showed up
	[8529] = true, --noggenfogger
	[180536] = true, --broken kyrian flute, can't be used
	[180817] = true, -- cypher of relocation
	[45067] = true, --noblegarden dress transmog
	--[140212] = true, --test item

	--[24468] = true, --burstcap mushroom
	--[24449] = true, --fertile spores
	--[24291] = true, --bog lord tendril
	--[24497] = true, --feralfen protection totem
	--[25448] = true, --blacksting's stinger
	--[25491] = true, --salvaged spore sacs
	--[24238] = true, --mushroom sample
	--[24472] = true, --boss grog'ak's head
}

function ElvUI_EltreumUI:QuestItem()
	if E.db.ElvUI_EltreumUI.quests.questitems then
		_, instanceType = IsInInstance()
		if instanceType ~= "none" then
			EltruismQuestItemFrame:Hide()
			EltruismQuestItemFrame:UnregisterAllEvents()
		else
			--Events
			--these events will fire correctly
			--ACTIONBAR_UPDATE_COOLDOWN,UNIT_INVENTORY_CHANGED,MAIL_SUCCESS
			EltruismQuestItemFrame:RegisterUnitEvent("UNIT_INVENTORY_CHANGED", "player")
			EltruismQuestItemFrame:RegisterEvent("MAIL_SUCCESS") -- when mailing quest items UNIT_INVENTORY_CHANGED does not fire

			--these events will simply request an update
			EltruismQuestItemFrame:RegisterEvent("BAG_UPDATE")
			EltruismQuestItemFrame:RegisterEvent("BAG_UPDATE_DELAYED")
			--EltruismQuestItemFrame:RegisterEvent("BAG_UPDATE_COOLDOWN")
			EltruismQuestItemFrame:RegisterEvent("QUEST_WATCH_UPDATE")
			EltruismQuestItemFrame:RegisterEvent("BAG_NEW_ITEMS_UPDATED")
			EltruismQuestItemFrame:RegisterEvent("QUEST_ACCEPTED") -- Needed for items that starts a quest, when we accept it, update to remove the icon
			--EltruismQuestItemFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
			EltruismQuestItemFrame:RegisterEvent("UPDATE_BINDINGS")
			EltruismQuestItemFrame:RegisterEvent("ACTIONBAR_UPDATE_COOLDOWN")
			--these were causing memory issues, exploding whenever a waypoint was set
			--EltruismQuestItemFrame:RegisterEvent("QUEST_LOG_UPDATE") -- For when items get added/removed during quest
			EltruismQuestItemFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA")	-- Should work better than PLAYER_ENTERING_WORLD

			if not InCombatLockdown() then
				EltruismQuestItemFrame:Show()
			end

			if E.db.ElvUI_EltreumUI.quests.questitemsbar1 and E.private.actionbar.enable then
				if not InCombatLockdown() then
					EltruismQuestItemFrame:SetParent(_G["ElvUI_Bar1Button1"])
				end
			end
			if not InCombatLockdown() then
				EltruismQuestItemFrame:SetSize(E.db.ElvUI_EltreumUI.quests.questitemsize,E.db.ElvUI_EltreumUI.quests.questitemsize)
				EltruismQuestItemFrame:SetClampedToScreen(true)
				EltruismQuestItemFrame:SetFrameStrata("MEDIUM")
			end

			-- update mover position
			function EltruismQuestItemFrame:FixPosition()
				if EltruismQuestItemFrame.debug then
					ElvUI_EltreumUI:Print("Firing FixPosition")
				end
				E:Delay(0, function()
					if not InCombatLockdown() and _G["EltruismQuestItem1"] then
						local point, relativeTo, relativePoint, xOfs, yOfs = EltruismQuestItemFrame:GetPoint()
						_G["EltruismQuestItem1"]:ClearAllPoints()
						if E.db.ElvUI_EltreumUI.quests.questorientation == "HORIZONTAL" then
							if EltruismQuestItemFrame.shownItems ~= 1 then
								if (EltruismQuestItemFrame.shownItems % 2) == 0 then
									if xOfs >= 0 then
										--_G["EltruismQuestItem1"]:SetPoint(point, relativeTo, relativePoint, xOfs-(((EltruismQuestItemFrame.shownItems-1)*E.db.ElvUI_EltreumUI.quests.questitemsize)/2), yOfs)
										_G["EltruismQuestItem1"]:SetPoint(point, relativeTo, relativePoint, xOfs-(((EltruismQuestItemFrame.shownItems-1)*E.db.ElvUI_EltreumUI.quests.questitemsize)/2)-(E.db.ElvUI_EltreumUI.quests.questitemspacing *(EltruismQuestItemFrame.shownItems-1)/2), yOfs)
									elseif xOfs < 0 then
										--_G["EltruismQuestItem1"]:SetPoint(point, relativeTo, relativePoint, xOfs+(((EltruismQuestItemFrame.shownItems-1)*E.db.ElvUI_EltreumUI.quests.questitemsize)/2), yOfs)
										_G["EltruismQuestItem1"]:SetPoint(point, relativeTo, relativePoint, xOfs-(((EltruismQuestItemFrame.shownItems-1)*E.db.ElvUI_EltreumUI.quests.questitemsize)/2)+(E.db.ElvUI_EltreumUI.quests.questitemspacing *(EltruismQuestItemFrame.shownItems-1)/2), yOfs)
									end
								else
									if xOfs >= 0 then
										--_G["EltruismQuestItem1"]:SetPoint(point, relativeTo, relativePoint, xOfs-(((EltruismQuestItemFrame.shownItems-(EltruismQuestItemFrame.shownItems % 2))*(E.db.ElvUI_EltreumUI.quests.questitemsize+1))/2), yOfs)
										_G["EltruismQuestItem1"]:SetPoint(point, relativeTo, relativePoint, xOfs-(E.db.ElvUI_EltreumUI.quests.questitemspacing *(EltruismQuestItemFrame.shownItems-1)/2)-(((EltruismQuestItemFrame.shownItems-(EltruismQuestItemFrame.shownItems % 2))*(E.db.ElvUI_EltreumUI.quests.questitemsize+1))/2), yOfs)
									elseif xOfs < 0 then
										--_G["EltruismQuestItem1"]:SetPoint(point, relativeTo, relativePoint, xOfs+(((EltruismQuestItemFrame.shownItems-(EltruismQuestItemFrame.shownItems % 2))*(E.db.ElvUI_EltreumUI.quests.questitemsize-1))/2), yOfs)
										_G["EltruismQuestItem1"]:SetPoint(point, relativeTo, relativePoint, xOfs+(E.db.ElvUI_EltreumUI.quests.questitemspacing *(EltruismQuestItemFrame.shownItems-1)/2)+(((EltruismQuestItemFrame.shownItems-(EltruismQuestItemFrame.shownItems % 2))*(E.db.ElvUI_EltreumUI.quests.questitemsize-1))/2), yOfs)
									end
								end
							else
								_G["EltruismQuestItem1"]:SetPoint(point, relativeTo, relativePoint, xOfs, yOfs)
							end
						else
							_G["EltruismQuestItem1"]:SetPoint(point, relativeTo, relativePoint, xOfs, yOfs)
						end
					end
				end)
			end

			--------------------------------------------------------------------------------------------------------
			--                                                Items                                               --
			--------------------------------------------------------------------------------------------------------

			-- Make Button
			local function CreateItemButton()
				if EltruismQuestItemFrame.debug then
					ElvUI_EltreumUI:Print("Firing CreateItemButton")
				end
				local b = CreateFrame("Button","EltruismQuestItem"..(#EltruismQuestItemFrame.items + 1),EltruismQuestItemFrame,"ActionButtonTemplate, SecureActionButtonTemplate")
				b:CreateBackdrop('Transparent')
				b:SetSize(E.db.ElvUI_EltreumUI.quests.questitemsize,E.db.ElvUI_EltreumUI.quests.questitemsizey)
				if E.db.ElvUI_EltreumUI.skins.shadow.enable then
					if not b.shadow then
						b:CreateShadow(E.db.ElvUI_EltreumUI.skins.shadow.length)
						ElvUI_EltreumUI:ShadowColor(b.shadow)
					end
				end
				b:SetHighlightTexture("Interface\\Buttons\\ButtonHilight-Square")
				b:RegisterForClicks("AnyDown","AnyUp")
				b:SetScript("OnEnter", function (button)
					GameTooltip:SetOwner(UIParent, "ANCHOR_CURSOR")
					local bag, slot = button:GetAttribute("bag"), button:GetAttribute("slot")
					if (bag) then
						GameTooltip:SetBagItem(bag,slot)
					else
						GameTooltip:SetInventoryItem("player",slot)
					end
					if E.db.ElvUI_EltreumUI.quests.questitemsfade then
						b:SetAlpha(1)
					end
				end)
				b:SetScript("OnLeave", function()
					if E.db.ElvUI_EltreumUI.quests.questitemsfade then
						b:SetAlpha(0)
					end
					GameTooltip:Hide()
				end)
				--b:HookScript("OnClick",Button_OnClick)

				b:HookScript("OnEnter", function()
					AB:BindUpdate(b)
					AB:FixKeybindText(b)
				end)

				b:SetAttribute("type*","item")

				b.icon = b:CreateTexture(nil,"ARTWORK")
				b.icon:SetAllPoints()

				b.name = "EltruismQuestItem"..(#EltruismQuestItemFrame.items + 1)

				b.count = b:CreateFontString(nil,"ARTWORK")
				b.count:SetFont(E.LSM:Fetch("font", E.db.general.font), E.db.actionbar.fontSize, ElvUI_EltreumUI:FontFlag(E.db.general.fontStyle))
				b.count:SetTextColor(1,1,1)
				b.count:SetPoint("BOTTOMRIGHT",b.icon, 0, 0)

				b.cooldown = CreateFrame("Cooldown",nil,b,"CooldownFrameTemplate")
				b.cooldown:SetAllPoints()
				E:RegisterCooldown(b.cooldown)

				if E.db.ElvUI_EltreumUI.quests.showkeybind then
					b.HotKey = b:CreateFontString(nil,"ARTWORK","NumberFontNormalSmallGray")
					b.HotKey:SetPoint(E.db.actionbar.bar1.hotkeyTextPosition or "CENTER",b.icon,E.db.actionbar.bar1.hotkeyTextXOffset or 0,b.icon,E.db.actionbar.bar1.hotkeyTextYOffset or 0)
					b.HotKey:SetFont(E.LSM:Fetch("font", E.db.general.font), E.db.actionbar.bar1.hotkeyFontSize, ElvUI_EltreumUI:FontFlag(E.db.actionbar.bar1.hotkeyFontOutline))
					--b.HotKey:SetPoint("TOPRIGHT",b.icon,0,0)
					--b.HotKey:SetJustifyH("LEFT")
				end

				b:Enable()
				b:Show()
				--if (#EltruismQuestItemFrame.items == 0) then
				if (EltruismQuestItemFrame.shownItems == 0) then
					if E.db.ElvUI_EltreumUI.quests.questorientation == "HORIZONTAL" then
						b:SetPoint("TOPLEFT",EltruismQuestItemFrame,0,0)
					else
						b:SetPoint("BOTTOM",EltruismQuestItemFrame,0,0)
					end
				end
				EltruismQuestItemFrame.items[#EltruismQuestItemFrame.items + 1] = b
				return b
			end

			-- Add Button
			local function AddButton(index,bag,slot,link,itemID,count)
				if EltruismQuestItemFrame.debug then
					ElvUI_EltreumUI:Print("Firing AddButton")
				end
				local btn = EltruismQuestItemFrame.items[index] or CreateItemButton()
				local _, _, _, _, _, _, _, _, _, itemTexture, _, _ = GetItemInfo(link)
				btn.icon:SetTexture(itemTexture)
				--btn.icon:SetTexCoord(0.08,0.92,0.08,0.92)
				--btn.icon:SetTexCoord(unpack(E.TexCoords))

				--from elvui trim action bar button
				local left, right, top, bottom = 0.08,0.92,0.08,0.92
				local width, height = btn:GetSize()
				local ratio = width / height
				if ratio > 1 then
					local trimAmount = (1 - (1 / ratio)) * 0.5
					top = top + trimAmount
					bottom = bottom - trimAmount
				else
					local trimAmount = (1 - ratio) * 0.5
					left = left + trimAmount
					right = right - trimAmount
				end
				btn.icon:SetTexCoord(left, right, top, bottom)
				btn.count:SetText(count and count > 1 and count or "")

				btn.link = link
				btn.itemID = itemID

				btn:SetAttribute("type*","item")
				btn:SetAttribute("bag",bag)
				btn:SetAttribute("slot",slot)
				btn:RegisterForClicks("AnyDown", "AnyUp")
				btn:Enable()

				if (index > 1) then
					btn:ClearAllPoints()
					if E.db.ElvUI_EltreumUI.quests.questorientation == "HORIZONTAL" then
						btn:SetPoint("LEFT", EltruismQuestItemFrame.items[index - 1], "RIGHT", E.db.ElvUI_EltreumUI.quests.questitemspacing, 0) --CONTROLS SPACING
					else
						btn:SetPoint("TOP", EltruismQuestItemFrame.items[index - 1], "BOTTOM", 0, -E.db.ElvUI_EltreumUI.quests.questitemspacing) --CONTROLS SPACING
					end
				end
				btn:Show()

				-- update mover position
				EltruismQuestItemFrame:FixPosition()
			end

			-- Check Item -- Az: Some items which starts a quest, are not marked as "Quest" in itemType or itemSubType. Ex: item:17008
			local function CheckItemTooltip(link,itemID)
				if EltruismQuestItemFrame.debug then
					ElvUI_EltreumUI:Print("Firing AddButton")
				end
				local _, _, _, _, _, itemType, itemSubType, _, itemEquipLoc, _, _, classID = GetItemInfo(link)

				-- Include predefinded items
				for _, id in ipairs(qItems) do
					if (itemID == id) and not blocklist[itemID] then
						return 1
					end
				end

				--old, was causing issues
				-- Scan Tip -- Az: any reason we cant just check for more or equal to 4 lines, or would some quest items fail that check?
				--[[EltruismQuestItemFrame.tip:ClearLines()
				EltruismQuestItemFrame.tip:SetHyperlink(link)
				local numLines = EltruismQuestItemFrame.tip:NumLines()
				local line2 = (_G["EltruismQuestItemTipTextLeft2"]:GetText() or "")
				if (numLines >= 3) and (itemType == QUEST_TOKEN or itemSubType == QUEST_TOKEN or classID == 12 or line2 == ITEM_BIND_QUEST or line2 == GetZoneText()) and itemEquipLoc == "" then
					for i = 3, numLines do
						if _G["EltruismQuestItemTipTextLeft"..i] then
							local text = _G["EltruismQuestItemTipTextLeft"..i]:GetText() or ""
							if text and (text:find("^"..ITEM_SPELL_TRIGGER_ONUSE)) then
								return 1
							end
						end
					end
				end]]

				--new
				if (itemType == QUEST_TOKEN or itemSubType == QUEST_TOKEN or classID == 12) and itemEquipLoc == "" and GetItemSpell(itemID) ~= nil then
					if not blocklist[itemID] then
						return 1
					end
				end
			end
			--------------------------------------------------------------------------------------------------------
			--                                               Update                                               --
			--------------------------------------------------------------------------------------------------------
			-- Update Cooldowns
			function EltruismQuestItemFrame:UpdateCooldowns()
				if EltruismQuestItemFrame.debug then
					ElvUI_EltreumUI:Print("Firing UpdateCooldowns")
				end
				for i = 1, EltruismQuestItemFrame.shownItems do
					local bag, slot = self.items[i]:GetAttribute("bag"), self.items[i]:GetAttribute("slot")
					if (bag) then
						CooldownFrame_Set(self.items[i].cooldown,GetContainerItemCooldown(bag,slot))
					else
						CooldownFrame_Set(self.items[i].cooldown,GetInventoryItemCooldown("player",slot))
					end
				end
			end

			--check for other buttons that are the same
			local function CheckButtonExistence(itemID)
				for i =1, #EltruismQuestItemFrame.items do
					if EltruismQuestItemFrame.items[i].itemID and EltruismQuestItemFrame.items[i].itemID == itemID then
						return false
					end
				end

				return true
			end

			-- Update Buttons
			function EltruismQuestItemFrame:UpdateButtons()
				if EltruismQuestItemFrame.debug then
					ElvUI_EltreumUI:Print("Firing UpdateButtons")
				end

				 --use dummy item id to avoid taint
				if #EltruismQuestItemFrame.items > 0 then
					for i =1, #EltruismQuestItemFrame.items do
						EltruismQuestItemFrame.items[i].itemID = 6948
					end
				end

				-- Check if we are locked by combat
				if InCombatLockdown() then
					return
				end

				--reset ids
				if #EltruismQuestItemFrame.items > 0 then
					for i =1, #EltruismQuestItemFrame.items do
						EltruismQuestItemFrame.items[i].itemID = 6948
						if not InCombatLockdown() then
							EltruismQuestItemFrame.items[i]:SetAttribute("disabled",nil)
							EltruismQuestItemFrame.items[i]:Disable()
						end
					end
				end

				-- locals
				local index = 1

				-- Inventory
				for bag = 0, NUM_BAG_SLOTS do
					for slot = 1, GetContainerNumSlots(bag) do
						local link = GetContainerItemLink(bag,slot)
						local itemID = link and tonumber(link:match(ITEMID_PATTERN))
						if (link) and (itemID) then
							if not blocklist[itemID] then
								local _, _, _, _, _, itemType, itemSubType, _, _, _, _, classID = GetItemInfo(link)
								if CheckButtonExistence(itemID) then
									if E.Retail or E.Mists then
										local questInfo = C_Container.GetContainerItemQuestInfo(bag,slot)
										if ((questInfo.isQuestItem or (itemType == QUEST_TOKEN or itemSubType == QUEST_TOKEN or classID == 12)) and GetItemSpell(itemID) ~= nil) or (CheckItemTooltip(link,itemID)) then
											local _, count = GetContainerItemInfo(bag,slot)
											AddButton(index,bag,slot,link,itemID,count)
											index = (index + 1)
										end
									elseif E.Classic then
										if ((itemType == QUEST_TOKEN or itemSubType == QUEST_TOKEN or classID == 12) and GetItemSpell(itemID) ~= nil) or (CheckItemTooltip(link,itemID)) then
											local _, count = GetContainerItemInfo(bag,slot)
											AddButton(index,bag,slot,link,itemID,count)
											index = (index + 1)
										end
									end
								end
							end
						end
					end
				end

				-- Equipped Items
				for _, slotName in ipairs(slots) do
					local slotId = GetInventorySlotInfo(slotName)
					local link = GetInventoryItemLink("player",slotId)
					local itemID = link and tonumber(link:match(ITEMID_PATTERN))
					if (link) and (itemID) and (CheckItemTooltip(link,itemID)) and GetItemSpell(itemID) ~= nil then
						if CheckButtonExistence(itemID) then
							if not blocklist[itemID] then
								AddButton(index,nil,slotId,link,itemID)
								index = (index + 1)
							end
						end
					end
				end

				-- Set Shown Items
				EltruismQuestItemFrame.shownItems = (index - 1)
				for i = index, #self.items do
					self.items[i]:Hide()
					self.items[i]:SetAttribute("disabled",nil)
					self.items[i]:Disable()
				end

				--update bind text
				if E.db.ElvUI_EltreumUI.quests.showkeybind and not InCombatLockdown() then
					for i = 1, EltruismQuestItemFrame.shownItems do
						self.items[i].HotKey:SetText(GetBindingKey("CLICK ".."EltruismQuestItem"..i..":LeftButton"))

						--register keybind
						self.items[i].bindstring = "CLICK ".."EltruismQuestItem"..i..":LeftButton"

						self.items[i].commandName = "CLICK ".."EltruismQuestItem"..i..":LeftButton"
						SetBindingClick(self.items[i].bindstring, "EltruismQuestItem1")

						--try to get elvui binding mode to work
						AB:StyleButton(self.items[i])
						AB:FixKeybindText(self.items[i])
					end
				end

				-- Update Misc
				EltruismQuestItemFrame:UpdateCooldowns()
			end
			EltruismQuestItemFrame:UpdateButtons()

			--------------------------------------------------------------------------------------------------------
			--                                               Events                                               --
			--------------------------------------------------------------------------------------------------------
			EltruismQuestItemFrame:SetScript("OnEvent",function(frame,event,...)
				if EltruismQuestItemFrame.debug then
					ElvUI_EltreumUI:Print("Firing OnEvent")
				end
				if (frame[event]) then
					--print("registered",event)
					frame[event](frame,event,...)
				else
					--print("unregisteredevent",event)
					EltruismQuestItemFrame:UpdateButtons()
				end
			end)

			-- Update Cooldowns
			function EltruismQuestItemFrame:ACTIONBAR_UPDATE_COOLDOWN()
				if EltruismQuestItemFrame.debug then
					ElvUI_EltreumUI:Print("Firing ACTIONBAR_UPDATE_COOLDOWN")
				end
				if not EltruismQuestItemFrame.shownItems then --added this check
					EltruismQuestItemFrame.shownItems = 0
				end
				if (EltruismQuestItemFrame.shownItems > 0) then
					EltruismQuestItemFrame:UpdateCooldowns()
				end
			end

			-- Inventory Changed
			function EltruismQuestItemFrame:UNIT_INVENTORY_CHANGED(_,unit)
				if EltruismQuestItemFrame.debug then
					ElvUI_EltreumUI:Print("Firing UNIT_INVENTORY_CHANGED")
				end
				if (unit == "player") then
					EltruismQuestItemFrame:UpdateButtons()
					-- update mover position
					EltruismQuestItemFrame:FixPosition()
				end
			end

			-- Inventory might've changed because of mail
			function EltruismQuestItemFrame:MAIL_SUCCESS()
				if EltruismQuestItemFrame.debug then
					ElvUI_EltreumUI:Print("Firing MAIL_SUCCESS")
				end
				EltruismQuestItemFrame:UpdateButtons()
				-- update mover position
				EltruismQuestItemFrame:FixPosition()
			end

		end
	else
		EltruismQuestItemFrame:UnregisterAllEvents()
	end
end
