local C = Broker_Everything.color
local TT
local atMerchant = nil

local slots = {
	"Head",
	"Shoulder", 
	"Chest", 
	"Waist", 
	"Legs", 
	"Feet", 
	"Wrist", 
	"Hands", 
	"MainHand", 
	"SecondaryHand", 
	"Ranged", 
}



local function getRepairCosts()
	local total = 0

	for _, slot in ipairs(slots) do
		local id = GetInventorySlotInfo(slot .. "Slot")
	
		local hasItem, _ ,cost = TT:SetInventoryItem("player", id)
		total = total + cost
	end
	return total
end

-- Function to remove the reputation discounts if at a vendor.
local function removeRepDiscounts(total)
	if not atMerchant then return end

	local reputation = UnitReaction("npc", "player")
	if reputation == 5 then
		--total = total * 100 / 95
		total = total  / 0.95
	elseif reputation == 6 then
		total = total / 0.9
	elseif reputation == 7 then
		total = total / 0.85
	elseif reputation == 8 then
		total = total / 0.8
	end
	return total
end

local durability_init = function(self)
	TT = CreateFrame("GameTooltip", "BasicBrokerScanTip", nil, "GameTooltipTemplate")
	TT:SetOwner(UIParent, "ANCHOR_NONE")
end

-- event function to calculate the durability of equiptment
local durability_onevent = function(self, event)
	local d = 1
	local dataobj = self.obj or LibStub:GetLibrary("LibDataBroker-1.1"):GetDataObjectByName(L["DURABILITY"])

	if event == "MERCHANT_SHOW" then atMerchant = "yes" end

	if event == "MERCHANT_CLOSED" then atMerchant = nil end

	for slotId = 1, 18 do
		if GetInventoryItemLink("player", slotId) then
			local x, y = GetInventoryItemDurability(slotId)
			if x and y > 0 then
				if x / y < d then
					d = x / y
				end
			end
		end
	end

	local t = floor(d * 100) .. "%"

	if d < 0.2 then
		dataobj.text = C.red(t)
	elseif d < 0.4 then
		dataobj.text = C.orange(t)
	else
		dataobj.text = t
	end
end


local durability_events = {
	"PLAYER_LOGIN",
	"PLAYER_DEAD",
	"MERCHANT_CLOSED",
	"PLAYER_REGEN_ENABLED",
	"PLAYER_ENTERING_WORLD",
	"MERCHANT_SHOW"
}


local durability_onclick = function()
	ToggleCharacter("PaperDollFrame")
end


local durability_tooltip = function(t)
	local repairCost = getRepairCosts()
	if atMerchant then 
		repairCost = removeRepDiscounts(repairCost)
	end

	t:AddLine(L["DURABILITY"])
	t:AddLine(" ")
	t:AddDoubleLine(C.dkyellow(L["Repair Cost"]), GetCoinTextureString(repairCost))
	t:AddLine(" ")
	t:AddLine(C.ltblue(L["Reputation discounts"]))
	t:AddDoubleLine(C.white(L["NEUTRAL"]), GetCoinTextureString(repairCost))
	t:AddDoubleLine(C.white(L["FRIENDLY"]), GetCoinTextureString(repairCost * 0.95))
	t:AddDoubleLine(C.white(L["HONOURED"]), GetCoinTextureString(repairCost * 0.90))
	t:AddDoubleLine(C.white(L["FRIENDLY"]), GetCoinTextureString(repairCost * 0.85))
	t:AddDoubleLine(C.white(L["EXALTED"]), GetCoinTextureString(repairCost * 0.80))
	t:AddLine(" ")
	t:AddLine(C.dkgreen(L["DURHINT"]))
end


Broker_Everything:Register(L["DURABILITY"],
	-- GetItemIcon (5956),
        "Interface\\Minimap\\TRACKING\\Repair",
	durability_init,
	durability_onevent,
	durability_events,
	durability_onclick,
	durability_tooltip,
	nil,
	nil)
