local MER, F, E, I, V, P, G, L = unpack(ElvUI_MerathilisUI)
local module = MER:GetModule("MER_Misc") ---@class Misc
local C = MER.Utilities.Color

local _G = _G
local format = format

local GenericTraitUI_LoadUI = GenericTraitUI_LoadUI
local GetInventoryItemID = GetInventoryItemID
local InCombatLockdown = InCombatLockdown
local ShowUIPanel = ShowUIPanel

local Enum_TooltipDataType_Item = Enum.TooltipDataType.Item
local TooltipDataProcessor_AddTooltipPostCall = TooltipDataProcessor.AddTooltipPostCall

local ScrollButtonIcon = "|TInterface\\TUTORIALFRAME\\UI-TUTORIAL-FRAME:13:11:0:-1:512:512:12:66:127:204|t"

local INVSLOT_BACK = INVSLOT_BACK

-- Credits NDui_Plus
local WARPS_TREE_ID = 1115
local WARPS_ITEM_ID = 235499

function module:ReshiiWrapsUpgrade()
	if not E.private.mui.misc.reshiiWrapsUpgrade then
		return
	end

	_G.CharacterBackSlot:HookScript("OnMouseDown", function(_, button)
		if button == "MiddleButton" then
			local itemID = GetInventoryItemID("player", INVSLOT_BACK)
			if itemID and itemID == WARPS_ITEM_ID then
				if not InCombatLockdown() then
					GenericTraitUI_LoadUI()
					_G.GenericTraitFrame:SetTreeID(WARPS_TREE_ID)
					ShowUIPanel(_G.GenericTraitFrame)
				else
					_G.UIErrorsFrame:AddMessage(E.InfoColor .. _G.ERR_NOT_IN_COMBAT)
				end
			end
		end
	end)
end

TooltipDataProcessor_AddTooltipPostCall(Enum_TooltipDataType_Item, function(tooltip, data)
	if tooltip:GetOwner() ~= _G.CharacterBackSlot or data.id ~= WARPS_ITEM_ID then
		return
	end

	if not E or not E.private or not E.private.mui or not E.private.mui.misc.reshiiWrapsUpgrade then
		return
	end

	tooltip:AddLine(" ")
	tooltip:AddDoubleLine(
		format("%s %s", ScrollButtonIcon, L["Middle Button"]),
		F.GetMERStyleText(L["Open Upgrade Menu"])
	)
end)

module:AddCallback("ReshiiWrapsUpgrade")
