local DGV = DugisGuideViewer
if not DGV then return end

local Item = DGV:RegisterModule("ItemButton")

DugisItemButtonMixin = {
	OnLoad = QuestObjectiveItemButtonMixin.OnLoad,
	OnEvent = QuestObjectiveItemButtonMixin.OnEvent,
	OnShow = QuestObjectiveItemButtonMixin.OnShow,
	OnHide = QuestObjectiveItemButtonMixin.OnHide,

	OnUpdate = function(self, elapsed)
		if not self.questLogIndex then return end
		-- Handle range indicator
		local rangeTimer = self.rangeTimer;
		if rangeTimer then
			rangeTimer = rangeTimer - elapsed;
			if rangeTimer <= 0 then
				local count = self.HotKey;
				local valid = IsQuestLogSpecialItemInRange(self.questLogIndex) 
					or C_Item.IsItemInRange(self.item, "target")
				if valid == 0 then
					count:Show();
					count:SetVertexColor(1.0, 0.1, 0.1);
				elseif valid == 1 then
					count:Show();
					count:SetVertexColor(0.6, 0.6, 0.6);
				else
					count:Hide();
				end
				rangeTimer = TOOLTIP_UPDATE_TIME;
			end

			self.rangeTimer = rangeTimer;
		end
	end,

	OnEnter = function(self)
		if self.item then
			GameTooltip:SetOwner(self, "ANCHOR_RIGHT");
			GameTooltip:SetItemByID(self.item);
		end
	end,

	UpdateCooldown = function(self)
		if self.item then
			local start, duration, enable = C_Item.GetItemCooldown(self.item)
			if start then
				CooldownFrame_Set(self.Cooldown, start, duration, enable);
				if duration > 0 and enable == 0 then
					SetItemButtonTextureVertexColor(self, 0.4, 0.4, 0.4);
				else
					SetItemButtonTextureVertexColor(self, 1, 1, 1);
				end
			end
		end
	end,

	SetUseItem = function(self, link, charges, questLogIndex)
		local item = link and C_Item.GetItemInfoInstant(link)
		local texture = item and C_Item.GetItemIconByID(item)
		self:SetAttribute("type1", "item")
		self:SetAttribute("item1", link)
		self.item = item
		self.questLogIndex = questLogIndex
		self.rangeTimer = -1;
		SetItemButtonTexture(self, texture);
		SetItemButtonCount(self, charges);
		self:UpdateCooldown(self);
	end,

	SetUp = function(self, questLogIndex)
		local link, _, charges = questLogIndex and GetQuestLogSpecialItemInfo(questLogIndex);
		DGV.DoOutOfCombat(SetUseItem, self, link, charges, questLogIndex)
	end
}

function Item:Initialize()

	function Item:Load()
		local frame = DugisItemButton

		local function SetUseItem(link, charges, questLogIndex)
			if not (link and DGV:UserSetting(DGV_ITEMBUTTONON)) then 
				frame:Hide()
				return
			end
			frame:SetUseItem(link, charges, questLogIndex)
			frame:Show()
		end

		function Item:SetUseItem(index)
			--DGV:DebugFormat("SetUseItem", "stack", debugstack())
			if DGV:IsModuleLoaded("Guides") then
				local useitem = DGV.useitem[index]
				local questId = DGV.qid[index]
				local logIndex = DGV:GetQuestLogIndexByQID(questId)
				local _, link, _, _, _, _, _, charges = useitem and C_Item.GetItemInfo(useitem)
				if logIndex then
					link, _, charges = GetQuestLogSpecialItemInfo(logIndex);
				end
				DGV.DoOutOfCombat(SetUseItem, link, charges, logIndex)
			end
		end
		
		function Item:SetUseItemByQID(qid)
			if qid then
				local logIndex = DGV:GetQuestLogIndexByQID(qid)
				local link, _, charges = logIndex and GetQuestLogSpecialItemInfo(logIndex);
				DGV.DoOutOfCombat(SetUseItem, link, charges, logIndex)
			end
		end

		function Item:Update()
			if DGV:UserSetting(DGV_ITEMBUTTONON) then frame:Show() else frame:Hide() end
		end
	end

	function Item:Unload()
		DugisItemButton:Hide()
	end
end
