DurabilityFu = AceLibrary("AceAddon-2.0"):new("FuBarPlugin-2.0", "AceDB-2.0", "AceEvent-2.0", "AceHook-2.1", "AceConsole-2.0")

DurabilityFu:RegisterDB("DurabilityFuDB")
DurabilityFu:RegisterDefaults('profile', {
	showPopup = true,
	showMan = false,
	showAverage = false,
	showHealthy = true,
	popupX = 0,
	popupY = -200,
	autoRepairEquipment = false,
	autoRepairInventory = false,
})

DurabilityFu.version = "2.0." .. string.sub("$Revision: 50188 $", 12, -3)
DurabilityFu.date = string.sub("$Date: 2007-09-28 18:38:21 -0400 (Fri, 28 Sep 2007) $", 8, 17)
DurabilityFu.hasIcon = true
DurabilityFu.canHideText = true

local L = AceLibrary("AceLocale-2.2"):new("FuBar_DurabilityFu")

local Gratuity = AceLibrary("Gratuity-2.0")
local Tablet = AceLibrary("Tablet-2.0")
local Abacus = AceLibrary("Abacus-2.0")
local Crayon = AceLibrary("Crayon-2.0")

local TEXT_HONORED = FACTION_STANDING_LABEL6

function DurabilityFu:IsShowingPopup()
	return self.db.profile.showPopup
end

function DurabilityFu:ToggleShowingPopup()
	self.db.profile.showPopup = not self.db.profile.showPopup
end

function DurabilityFu:IsAutoRepairingEquipment()
	return self.db.profile.autoRepairEquipment
end

function DurabilityFu:ToggleAutoRepairingEquipment()
	self.db.profile.autoRepairEquipment = not self.db.profile.autoRepairEquipment
end

function DurabilityFu:IsAutoRepairingInventory()
	return self.db.profile.autoRepairInventory
end

function DurabilityFu:ToggleAutoRepairingInventory()
	self.db.profile.autoRepairInventory = not self.db.profile.autoRepairInventory
end

function DurabilityFu:IsShowingMan()
	return self.db.profile.showMan
end

function DurabilityFu:ToggleShowingMan()
	self.db.profile.showMan = not self.db.profile.showMan
--[[	if self.db.profile.showMan and self.manX and self.manY then
		DurabilityFrame:ClearAllPoints()
		DurabilityFrame:SetPoint("TOPRIGHT", UIParent, "TOPRIGHT", self.manX, self.manY)
	elseif DurabilityFrame:GetRight() and DurabilityFrame:GetTop() then
		self.manX = DurabilityFrame:GetRight() - GetScreenWidth()
		self.manY = DurabilityFrame:GetTop() - GetScreenHeight()
		DurabilityFrame:ClearAllPoints()
		DurabilityFrame:SetPoint("TOPRIGHT", UIParent, "TOPRIGHT", self.manX + 3000, self.manY + 3000)
	end]]
	self:DurabilityFrame_SetAlerts()
end

function DurabilityFu:IsShowingAverage()
	return self.db.profile.showAverage
end

function DurabilityFu:ToggleShowingAverage()
	self.db.profile.showAverage = not self.db.profile.showAverage
	self:Update()
end

function DurabilityFu:IsShowingHealthyItems()
	return self.db.profile.showHealthy
end

function DurabilityFu:ToggleShowingHealthyItems()
	self.db.profile.showHealthy = not self.db.profile.showHealthy
	self:UpdateTooltip()
end

local repairIndex = 0
local itemStatus = {
	-- value, max, cost, name, slot
	{ 0, 0, 0, INVTYPE_HEAD, "Head" },
	{ 0, 0, 0, INVTYPE_SHOULDER, "Shoulder" },
	{ 0, 0, 0, INVTYPE_CHEST, "Chest" },
	{ 0, 0, 0, INVTYPE_WAIST, "Waist" },
	{ 0, 0, 0, INVTYPE_LEGS, "Legs" },
	{ 0, 0, 0, INVTYPE_FEET, "Feet" },
	{ 0, 0, 0, INVTYPE_WRIST, "Wrist" },
	{ 0, 0, 0, INVTYPE_HAND, "Hands" },
	{ 0, 0, 0, INVTYPE_WEAPONMAINHAND, "MainHand" },
	{ 0, 0, 0, INVTYPE_WEAPONOFFHAND, "SecondaryHand" },
	{ 0, 0, 0, INVTYPE_RANGED, "Ranged" },
	{ 0, 0, 0, INVENTORY_TOOLTIP },
}
local INVENTORY_SLOT = 12
local VALUE = 1
local MAX = 2
local COST = 3
local NAME = 4
local SLOT = 5
function DurabilityFu:OnEnable()
	self:RegisterEvent("PLAYER_UNGHOST", "OnBagUpdate")
	self:RegisterEvent("PLAYER_DEAD", "OnBagUpdate")
	self:RegisterEvent("PLAYER_REGEN_ENABLED", "OnBagUpdate")
	self:RegisterEvent("PLAYER_ENTERING_WORLD")
	self:RegisterEvent("UPDATE_INVENTORY_ALERTS", 1)
	self:RegisterEvent("MERCHANT_SHOW")
	self:RegisterEvent("MERCHANT_CLOSED")
	self:RegisterEvent("FACTION_UPDATE")

	self:SecureHook("DurabilityFrame_SetAlerts")
	self:DurabilityFrame_SetAlerts()
end

function DurabilityFu:OnDisable()
	StaticPopup_Hide("BOSSPANEL_DURABILITY_POPUP")
	if not self.db.profile.showMan then
		self:ToggleShowingMan()
		self.db.profile.showMan = false
	end
end

local options = {
	type = 'group',
	args = {
		popup = {
			type = 'toggle',
			name = L["Show repair popup at vendor"],
			desc = L["Toggle whether to show the popup at the merchant window"],
			get = "IsShowingPopup",
			set = "ToggleShowingPopup",
		},
		showMan = {
			type = 'toggle',
			name = L["Show the armored man"],
			desc = L["Toggle whether to show Blizzard's armored man"],
			get = "IsShowingMan",
			set = "ToggleShowingMan",
		},
		average = {
			type = 'toggle',
			name = L["Show average value"],
			desc = L["Toggle whether to show your average or minimum durability"],
			get = "IsShowingAverage",
			set = "ToggleShowingAverage",
		},
		healthy = {
			type = 'toggle',
			name = L["Show healthy items"],
			desc = L["Toggle whether to show items that are healthy (100% repaired)"],
			get = "IsShowingHealthyItems",
			set = "ToggleShowingHealthyItems",
		},
		autoRepair = {
			type = 'group',
			name = L["Auto repair"],
			desc = L["Auto repair"],
			args = {
				equipment = {
					type = 'toggle',
					name = L["Equipment"],
					desc = L["Equipment"],
					get = "IsAutoRepairingEquipment",
					set = "ToggleAutoRepairingEquipment",
				},
				inventory = {
					type = 'toggle',
					name = L["Inventory"],
					desc = L["Inventory"],
					get = "IsAutoRepairingInventory",
					set = "ToggleAutoRepairingInventory",
				},
			}
		},
	},
	handler = DurabilityFu,
}

DurabilityFu:RegisterChatCommand(L["AceConsole-Commands"], options)
DurabilityFu.OnMenuRequest = options

function DurabilityFu:PLAYER_ENTERING_WORLD()
	if not self.isHonored then
		for i = 1, GetNumFactions() do
			local _, _, standing = GetFactionInfo(i)
			if standing >= 6 then
				self.isHonored = true
				break
			end
		end
	end
end

function DurabilityFu:FACTION_UPDATE()
	if GetNumFactions() > 0 and not self.isHonored then
		for i = 1, GetNumFactions() do
			local _, _, standing = GetFactionInfo(i)
			if standing >= 6 then
				self.isHonored = true
				break
			end
		end
		self:UnregisterEvent("FACTION_UPDATE")
	elseif self.isHonored then
		self:UnregisterEvent("FACTION_UPDATE")
	end
end

local lastUpdate
function DurabilityFu:UpdateBagData()
	if lastUpdate and GetTime() - lastUpdate < 1 then
		return
	end
	lastUpdate = GetTime()
	local minStatus = 1.0
	local minValue = 0
	local minMax = 0

	itemStatus[INVENTORY_SLOT][VALUE] = 0
	itemStatus[INVENTORY_SLOT][MAX] = 0
	itemStatus[INVENTORY_SLOT][COST] = 0

	for bag = 0, 4 do
		for slot = 1, GetContainerNumSlots(bag) do

			local status, value, max, cost = self:GetStatus(slot, bag)
			if not self:IsShowingAverage() then
				if status and status < minStatus then
					minStatus = status
					minValue = value
					minMax = max

					if (repairIndex == 0 or status < self:GetPercent(itemStatus[repairIndex][VALUE], itemStatus[repairIndex][MAX])) and max ~= 0 then
						repairIndex = INVENTORY_SLOT
					end

					itemStatus[INVENTORY_SLOT][VALUE] = value
					itemStatus[INVENTORY_SLOT][MAX] = max
				end
			elseif cost then
				itemStatus[INVENTORY_SLOT][VALUE] = itemStatus[INVENTORY_SLOT][VALUE] + value
				itemStatus[INVENTORY_SLOT][MAX] = itemStatus[INVENTORY_SLOT][MAX] + max
			end
			if cost then
				itemStatus[INVENTORY_SLOT][COST] = itemStatus[INVENTORY_SLOT][COST] + cost
			end
		end
	end
	if self:IsShowingAverage() then
		local status = self:GetPercent(itemStatus[INVENTORY_SLOT][VALUE], itemStatus[INVENTORY_SLOT][MAX])
		if (repairIndex == 0 or status < self:GetPercent(itemStatus[repairIndex][VALUE], itemStatus[repairIndex][MAX])) and itemStatus[INVENTORY_SLOT][MAX] ~= 0 then
			repairIndex = INVENTORY_SLOT
		end
	end
end

function DurabilityFu:OnBagUpdate()
	self:UpdateInventoryData()
	self:UpdateBagData()
	self:UpdateText()
	self:UpdateTooltip()
end

local tmp = {}
for i = 1, 11 do
	tmp[i] = true
end
function DurabilityFu:UpdateInventoryData(toCheck)
	if not toCheck then
		toCheck = tmp
	end

	local minStatus = 1.0
	local minValue = 0
	local minMax = 0
	local minIndex = 0

	for index,_ in pairs(toCheck) do
		local status, value, max, cost = self:GetStatus(index)
		if status and status < minStatus then
			minStatus = status
			minValue = value
			minMax = max
			minIndex = index
		end

		itemStatus[index][VALUE] = value or 0
		itemStatus[index][MAX] = max or 0
		itemStatus[index][COST] = cost or 0
	end

	repairIndex = minIndex

	if not self:IsShowingMan() then
		self.db.profile.showMan = true
		self:ToggleShowingMan()
	end
end

function DurabilityFu:UPDATE_INVENTORY_ALERTS()
	self:UpdateInventoryData(INVENTORY_ALERT_STATUS_SLOTS)
	self:UpdateText()
	self:UpdateTooltip()
end

function DurabilityFu:MERCHANT_SHOW()
	local dirty = false
	local stop = false
	if CanMerchantRepair() then
		if self:IsAutoRepairingEquipment() then
			local repairCost = GetRepairAllCost()
			local money = GetMoney()
			if money < repairCost then
				self:Print(L["Cannot auto-repair equipment. Your money: %s. Needed: %s"], Abacus:FormatMoneyFull(money, true), Abacus:FormatMoneyFull(repairCost, true))
				stop = true
			elseif repairCost > 0 then
				RepairAllItems()
				self:Print(L["Auto-repaired equipment: %s"], Abacus:FormatMoneyFull(repairCost))
				dirty = true
			else
				self:Print(L["No equipment to auto-repair"])
			end
		end
		if not stop and self:IsAutoRepairingInventory() then
			local repairCost = self:CalculateInventoryCost()
			local money = GetMoney()
			if money < repairCost then
				self:Print(L["Cannot auto-repair inventory. Your money: %s. Needed: %s"], Abacus:FormatMoneyFull(money, true), Abacus:FormatMoneyFull(repairCost, true))
			elseif repairCost > 0 then
				ShowRepairCursor()
				for bag = 0, 4 do
					for slot = 1, GetContainerNumSlots(bag) do
						local _,repairCost = Gratuity:SetBagItem(bag, slot)
						if repairCost and repairCost > 0 then
							PickupContainerItem(bag, slot)
						end
					end
				end
				HideRepairCursor()
				self:Print(L["Auto-repaired inventory: %s"], Abacus:FormatMoneyFull(repairCost))
				dirty = true
			else
				self:Print(L["No items in inventory to auto-repair"])
			end
		end
	end
	self.merchantShown = true
	if self:IsShowingPopup() then
		local canRepair, repairCost = CanMerchantRepair(), GetRepairAllCost()
		if canRepair then
			repairCost = repairCost + self:CalculateInventoryCost()
			if repairCost > 0 then
				if dirty then
					self:ScheduleEvent(self.OpenPopup, 1, self)
				else
					self:OpenPopup()
				end
			end
		end
	end
	self:ScheduleRepeatingEvent(self.name, self.OnUpdate, 1, self)
	self.updateNum = nil
	self:Update()
end

function DurabilityFu:MERCHANT_CLOSED()
	self.merchantShown = false
	self:ClosePopup()
	self:ScheduleRepeatingEvent(self.name, self.OnUpdate, 1, self)
	self.updateNum = nil
	self:Update()
end

function DurabilityFu:CalculateInventoryCost()
	local result = 0
	for bag = 0, 4 do
		for slot = 1, GetContainerNumSlots(bag) do
			local _,repairCost = Gratuity:SetBagItem(bag, slot)
			if repairCost and repairCost > 0 then
				result = result + repairCost
			end
		end
	end

	return result
end

function DurabilityFu:GetPercent(quotient, denominator)
	if denominator ~= 0 then
		return quotient / denominator
	else
		return 1
	end
end

function DurabilityFu:GetStatus(index, bag)
	local value = 0
	local max = 0
	local cost = 0

	local hasItem, repairCost, _
	if bag then
		_,repairCost = Gratuity:SetBagItem(bag, index)
		hasItem = GetContainerItemInfo(bag, index)
	else
		local slotName = itemStatus[index][SLOT] .. "Slot"

		local id = GetInventorySlotInfo(slotName)
		hasItem,_,repairCost = Gratuity:SetInventoryItem("player", id)
	end

	if hasItem then
		if repairCost then
			cost = repairCost
		end

		local value, max = Gratuity:FindDeformat(DURABILITY_TEMPLATE, nil, nil, nil, true)
		if value then
			value = tonumber(value)
			max = tonumber(max)
			return self:GetPercent(value, max), value, max, cost
		end
	end
end

function DurabilityFu:OnDataUpdate()
	self:UpdateBagData()
	self:UpdateInventoryData()
end

function DurabilityFu:OnTextUpdate()
	local percent
	if not self:IsShowingAverage() then
		if repairIndex == 0 then
			percent = 1
		else
			percent = self:GetPercent(itemStatus[repairIndex][VALUE], itemStatus[repairIndex][MAX])
		end
	else
		local value = 0
		local max = 0
		for i,item in pairs(itemStatus) do
			if i ~= INVENTORY_SLOT then
				value = value + item[VALUE]
				max = max + item[MAX]
			end
		end
		percent = self:GetPercent(value, max)
	end
	self:SetText(string.format("|cff%s%d%%|r", Crayon:GetThresholdHexColor(percent), percent * 100))
end

function DurabilityFu:OnTooltipUpdate()
	local cost = 0
	local cat = Tablet:AddCategory(
		'columns', 3,
		'child_textR', 1,
		'child_textG', 1,
		'child_textB', 0
	)
	for index, item in pairs(itemStatus) do
		if item[MAX] > 0 then
			cost = cost + item[COST]
			local percent = self:GetPercent(item[VALUE], item[MAX])
			if self:IsShowingHealthyItems() or percent < 1 then
				local r, g, b = Crayon:GetThresholdColor(percent)
				cat:AddLine(
					'text', item[NAME],
					'text2', string.format("%.0f%%", percent * 100),
					'text2R', r,
					'text2G', g,
					'text2B', b,
					'text3', Abacus:FormatMoneyShort(item[COST], true)
				)
			end
		end
	end
	local value = 0
	local max = 0
	for i,item in pairs(itemStatus) do
		if i ~= INVENTORY_SLOT then
			value = value + item[VALUE]
			max = max + item[MAX]
		end
	end
	cat = Tablet:AddCategory(
		'columns', 2,
		'text', L["Total"],
		'child_textR', 1,
		'child_textG', 1,
		'child_textB', 0,
		'child_text2R', 1,
		'child_text2G', 1,
		'child_text2B', 1
	)
	local r, g, b = Crayon:GetThresholdColor(value / max)
	cat:AddLine(
		'text', L["Percent"],
		'text2', string.format("%.0f%%", value / max * 100),
		'text2R', r,
		'text2G', g,
		'text2B', b
	)

	cat:AddLine(
		'text', L["Repair cost"],
		'text2', Abacus:FormatMoneyFull(cost, true)
	)
	if not self.merchantShown then
		if self.isHonored then
			cat:AddLine(
				'text', TEXT_HONORED,
				'text2', Abacus:FormatMoneyFull(cost * 0.9, true)
			)
		end
	end
end

function DurabilityFu:PromptEquipment_OnClick()
	RepairAllItems()
	self:ClosePopup()
	self:Update()
end

function DurabilityFu:PromptInventory_OnClick()
	ShowRepairCursor()
	for bag = 0, 4 do
		for slot = 1, GetContainerNumSlots(bag) do
			local _,repairCost = Gratuity:SetBagItem(bag, slot)
			if repairCost and repairCost > 0 then
				PickupContainerItem(bag, slot)
			end
		end
	end
	HideRepairCursor()
	self:ClosePopup()
	self:Update()
	self:ScheduleRepeatingEvent(self.name, self.OnUpdate, 1, self)
end

local updateNum = 0
function DurabilityFu:OnUpdate()
	self:Update()
	updateNum = updateNum + 1
	if updateNum >= 5 then
		self:CancelScheduledEvent(self.name)
		updateNum = 0
	end
end

function DurabilityFu:PromptBoth_OnClick()
	RepairAllItems()
	self:PromptInventory_OnClick()
end

local popup
function DurabilityFu:ClosePopup()
	if popup then
		popup:Hide()
	end
end

local buttonSize = 32

function DurabilityFu:OpenPopup()
	local canRepair, repairCost = CanMerchantRepair(), GetRepairAllCost()
	if not canRepair then
		return
	end
	repairCost = repairCost + self:CalculateInventoryCost()
	if repairCost == 0 then
		return
	end
	if not popup then
		popup = CreateFrame("Frame", "DurabilityFuPrompt", UIParent)
		popup:SetFrameStrata("DIALOG")
		popup:EnableMouse(true)
		popup:SetMovable(true)
		popup:Hide()
		popup:SetWidth(256)
		popup:SetHeight(160)
		popup:SetPoint("TOP", UIParent, "TOP", self.db.profile.popupX, self.db.profile.popupY)

		local header = CreateFrame("Frame", popup:GetName() .. "Header", popup)
		header:SetWidth(192)
		header:SetHeight(32)
		header:SetPoint("TOP", popup, "TOP", 0, 6)
		local texture = header:CreateTexture(popup:GetName() .. "HeaderBar", "ARTWORK")
		texture:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Header")
		texture:SetWidth(384)
		texture:SetHeight(64)
		texture:SetPoint("TOP", header, "TOP", 0, 5)
		texture:Show()
		local text = header:CreateFontString(popup:GetName() .. "HeaderTitle", "ARTWORK")
		text:SetFontObject(GameFontNormal)
		text:SetPoint("CENTER", header, "CENTER", 0, 0)
		text:SetText(self:GetTitle() .. " " .. L["Repair"])
		header:EnableMouse(true)
		header:SetMovable(true)
		header:SetScript("OnMouseDown", function()
			popup:StartMoving()
		end)
		header:SetScript("OnMouseUp", function()
			popup:StopMovingOrSizing()
			self.db.profile.popupY = popup:GetTop() - GetScreenHeight()
			self.db.profile.popupX = popup:GetCenter() - GetScreenWidth() / 2
		end)

		local eqButton = CreateFrame("Button", popup:GetName() .. "EquipmentButton", popup)
		popup.eqButton = eqButton
		eqButton:SetWidth(buttonSize)
		eqButton:SetHeight(buttonSize)
		local texture = eqButton:CreateTexture()
		texture:SetWidth(buttonSize * 1.6458333)
		texture:SetHeight(buttonSize * 1.6458333)
		texture:SetPoint("CENTER", eqButton, "CENTER")
		texture:SetTexture("Interface\\Buttons\\UI-Quickslot2")
		eqButton:SetNormalTexture(texture)
		local texture = eqButton:CreateTexture(nil, "BACKGROUND")
		texture:SetTexture("Interface\\Icons\\INV_Chest_Leather_08")
		texture:SetAllPoints(eqButton)
		eqButton:SetPushedTexture("Interface\\Buttons\\UI-Quickslot-Depress")
		local texture = eqButton:CreateTexture()
		texture:SetTexture("Interface\\Buttons\\ButtonHilight-Square")
		texture:SetAllPoints(eqButton)
		eqButton:SetHighlightTexture(texture)
		local texture = eqButton:CreateTexture()
		texture:SetTexture("Interface\\Icons\\INV_Chest_Leather_08")
		texture:SetDesaturated(true)
		texture:SetAllPoints(eqButton)
		eqButton:SetDisabledTexture(texture)
		eqButton:SetPoint("TOPRIGHT", popup, "TOPRIGHT", -18, -36)
		eqButton:SetScript("OnClick", function()
			self:PromptEquipment_OnClick()
		end)
		local texture = eqButton:CreateTexture(nil, "ARTWORK")
		texture:SetTexture(0, 0, 0, 0.5)
		texture:SetAllPoints(eqButton)
		local button_Enable = eqButton.Enable
		function eqButton:Enable()
			button_Enable(self)
			texture:Hide()
		end
		local button_Disable = eqButton.Disable
		function eqButton:Disable()
			button_Disable(self)
			texture:Show()
		end

		local invButton = CreateFrame("Button", popup:GetName() .. "InventoryButton", popup)
		popup.invButton = invButton
		invButton:SetWidth(buttonSize)
		invButton:SetHeight(buttonSize)
		local texture = invButton:CreateTexture()
		texture:SetWidth(buttonSize * 1.6458333)
		texture:SetHeight(buttonSize * 1.6458333)
		texture:SetPoint("CENTER", invButton, "CENTER")
		texture:SetTexture("Interface\\Buttons\\UI-Quickslot2")
		invButton:SetNormalTexture(texture)
		local texture = invButton:CreateTexture(nil, "BACKGROUND")
		texture:SetTexture("Interface\\Icons\\INV_Misc_Bag_10")
		texture:SetAllPoints(invButton)
		invButton:SetPushedTexture("Interface\\Buttons\\UI-Quickslot-Depress")
		local texture = invButton:CreateTexture()
		texture:SetTexture("Interface\\Buttons\\ButtonHilight-Square")
		texture:SetAllPoints(invButton)
		invButton:SetHighlightTexture(texture)
		local texture = invButton:CreateTexture()
		texture:SetTexture("Interface\\Icons\\INV_Misc_Bag_10")
		texture:SetDesaturated(true)
		texture:SetAllPoints(invButton)
		invButton:SetDisabledTexture(texture)
		invButton:SetPoint("TOP", eqButton, "BOTTOM", 0, -8)
		invButton:SetScript("OnClick", function()
			self:PromptInventory_OnClick()
		end)
		local texture = invButton:CreateTexture(nil, "ARTWORK")
		texture:SetTexture(0, 0, 0, 0.5)
		texture:SetAllPoints(invButton)
		local button_Enable = invButton.Enable
		function invButton:Enable()
			button_Enable(self)
			texture:Hide()
		end
		local button_Disable = invButton.Disable
		function invButton:Disable()
			button_Disable(self)
			texture:Show()
		end

		local bothButton = CreateFrame("Button", popup:GetName() .. "BothButton", popup)
		popup.bothButton = bothButton
		bothButton:SetWidth(buttonSize)
		bothButton:SetHeight(buttonSize)
		local texture = bothButton:CreateTexture()
		texture:SetWidth(buttonSize * 1.6458333)
		texture:SetHeight(buttonSize * 1.6458333)
		texture:SetPoint("CENTER", bothButton, "CENTER")
		texture:SetTexture("Interface\\Buttons\\UI-Quickslot2")
		bothButton:SetNormalTexture(texture)
		local texture = bothButton:CreateTexture(nil, "BACKGROUND")
		texture:SetTexture("Interface\\Icons\\Trade_Blacksmithing")
		texture:SetAllPoints(bothButton)
		bothButton:SetPushedTexture("Interface\\Buttons\\UI-Quickslot-Depress")
		local texture = bothButton:CreateTexture()
		texture:SetTexture("Interface\\Buttons\\ButtonHilight-Square")
		texture:SetAllPoints(bothButton)
		bothButton:SetHighlightTexture(texture)
		local texture = bothButton:CreateTexture()
		texture:SetTexture("Interface\\Icons\\Trade_Blacksmithing")
		texture:SetDesaturated(true)
		texture:SetAllPoints(bothButton)
		bothButton:SetDisabledTexture(texture)
		bothButton:SetPoint("TOP", invButton, "BOTTOM", 0, -8)
		bothButton:SetScript("OnClick", function()
			self:PromptBoth_OnClick()
		end)
		local texture = bothButton:CreateTexture(nil, "ARTWORK")
		texture:SetTexture(0, 0, 0, 0.5)
		texture:SetAllPoints(bothButton)
		local button_Enable = bothButton.Enable
		function bothButton:Enable()
			button_Enable(self)
			texture:Hide()
		end
		local button_Disable = bothButton.Disable
		function bothButton:Disable()
			button_Disable(self)
			texture:Show()
		end

		local closeButton = CreateFrame("Button", popup:GetName() .. "CloseButton", popup)
		popup.closeButton = closeButton
		closeButton:SetWidth(32)
		closeButton:SetHeight(32)
		closeButton:SetPoint("TOPRIGHT", popup, "TOPRIGHT", 0, 0)
		closeButton:SetNormalTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Up")
		closeButton:SetPushedTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Down")
		local texture = closeButton:CreateTexture()
		texture:SetTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight")
		texture:SetBlendMode("ADD")
		closeButton:SetHighlightTexture(texture)
		closeButton:SetScript("OnClick", function()
			self:ClosePopup()
		end)
		closeButton:Show()

		local eqCost = CreateFrame("Frame", popup:GetName() .. "EquipmentCost", popup)
		popup.eqCost = eqCost
		eqCost:SetWidth(96)
		eqCost:SetHeight(13)
		eqCost:SetPoint("RIGHT", eqButton, "LEFT", -8, 0)
		local copper = CreateFrame("Frame", eqCost:GetName() .. "Copper", eqCost)
		copper:SetWidth(32)
		copper:SetHeight(13)
		copper:SetPoint("RIGHT", eqCost, "RIGHT")
		local texture = copper:CreateTexture(copper:GetName() .. "Texture", "ARTWORK")
		texture:SetPoint("RIGHT", copper, "RIGHT")
		texture:SetWidth(13)
		texture:SetHeight(13)
		texture:SetTexture("Interface\\MoneyFrame\\UI-MoneyIcons")
		texture:SetTexCoord(0.5, 0.75, 0, 1)
		local copperText = copper:CreateFontString(copper:GetName() .. "Text", "ARTWORK")
		copperText:SetPoint("RIGHT", texture, "LEFT")
		copperText:SetHeight(13)
		copperText:SetFontObject(NumberFontNormal)
		copperText:SetText(0)
		copperText:SetJustifyH("RIGHT")

		local silver = CreateFrame("Frame", eqCost:GetName() .. "Silver", eqCost)
		silver:SetWidth(32)
		silver:SetHeight(13)
		silver:SetPoint("RIGHT", copper, "LEFT")
		local texture = silver:CreateTexture(silver:GetName() .. "Texture", "ARTWORK")
		texture:SetPoint("RIGHT", silver, "RIGHT")
		texture:SetWidth(13)
		texture:SetHeight(13)
		texture:SetTexture("Interface\\MoneyFrame\\UI-MoneyIcons")
		texture:SetTexCoord(0.25, 0.5, 0, 1)
		local silverText = silver:CreateFontString(silver:GetName() .. "Text", "ARTWORK")
		silverText:SetPoint("RIGHT", texture, "LEFT")
		silverText:SetHeight(13)
		silverText:SetFontObject(NumberFontNormal)
		silverText:SetText(0)
		silverText:SetJustifyH("RIGHT")

		local gold = CreateFrame("Frame", eqCost:GetName() .. "Gold", eqCost)
		gold:SetWidth(32)
		gold:SetHeight(13)
		gold:SetPoint("RIGHT", silver, "LEFT")
		local texture = gold:CreateTexture(gold:GetName() .. "Texture", "ARTWORK")
		texture:SetPoint("RIGHT", gold, "RIGHT")
		texture:SetWidth(13)
		texture:SetHeight(13)
		texture:SetTexture("Interface\\MoneyFrame\\UI-MoneyIcons")
		texture:SetTexCoord(0, 0.25, 0, 1)
		local goldText = gold:CreateFontString(gold:GetName() .. "Text", "ARTWORK")
		goldText:SetPoint("RIGHT", texture, "LEFT")
		goldText:SetHeight(13)
		goldText:SetFontObject(NumberFontNormal)
		goldText:SetText(0)
		goldText:SetJustifyH("RIGHT")
		function eqCost:SetValue(value)
			local g = floor(value / 10000)
			local s = mod(floor(value / 100), 100)
			local c = mod(value, 100)
			goldText:SetText(g)
			silverText:SetText(s)
			copperText:SetText(c)
			if g == 0 then
				gold:Hide()
				if s == 0 then
					silver:Hide()
				else
					silver:Show()
				end
			else
				gold:Show()
				silver:Show()
			end
		end

		local invCost = CreateFrame("Frame", popup:GetName() .. "InventoryCost", popup)
		popup.invCost = invCost
		invCost:SetWidth(96)
		invCost:SetHeight(13)
		invCost:SetPoint("RIGHT", invButton, "LEFT", -8, 0)
		local copper = CreateFrame("Frame", invCost:GetName() .. "Copper", invCost)
		copper:SetWidth(32)
		copper:SetHeight(13)
		copper:SetPoint("RIGHT", invCost, "RIGHT")
		local texture = copper:CreateTexture(copper:GetName() .. "Texture", "ARTWORK")
		texture:SetPoint("RIGHT", copper, "RIGHT")
		texture:SetWidth(13)
		texture:SetHeight(13)
		texture:SetTexture("Interface\\MoneyFrame\\UI-MoneyIcons")
		texture:SetTexCoord(0.5, 0.75, 0, 1)
		local copperText = copper:CreateFontString(copper:GetName() .. "Text", "ARTWORK")
		copperText:SetPoint("RIGHT", texture, "LEFT")
		copperText:SetHeight(13)
		copperText:SetFontObject(NumberFontNormal)
		copperText:SetText(0)
		copperText:SetJustifyH("RIGHT")

		local silver = CreateFrame("Frame", invCost:GetName() .. "Silver", invCost)
		silver:SetWidth(32)
		silver:SetHeight(13)
		silver:SetPoint("RIGHT", copper, "LEFT")
		local texture = silver:CreateTexture(silver:GetName() .. "Texture", "ARTWORK")
		texture:SetPoint("RIGHT", silver, "RIGHT")
		texture:SetWidth(13)
		texture:SetHeight(13)
		texture:SetTexture("Interface\\MoneyFrame\\UI-MoneyIcons")
		texture:SetTexCoord(0.25, 0.5, 0, 1)
		local silverText = silver:CreateFontString(silver:GetName() .. "Text", "ARTWORK")
		silverText:SetPoint("RIGHT", texture, "LEFT")
		silverText:SetHeight(13)
		silverText:SetFontObject(NumberFontNormal)
		silverText:SetText(0)
		silverText:SetJustifyH("RIGHT")

		local gold = CreateFrame("Frame", invCost:GetName() .. "Gold", invCost)
		gold:SetWidth(32)
		gold:SetHeight(13)
		gold:SetPoint("RIGHT", silver, "LEFT")
		local texture = gold:CreateTexture(gold:GetName() .. "Texture", "ARTWORK")
		texture:SetPoint("RIGHT", gold, "RIGHT")
		texture:SetWidth(13)
		texture:SetHeight(13)
		texture:SetTexture("Interface\\MoneyFrame\\UI-MoneyIcons")
		texture:SetTexCoord(0, 0.25, 0, 1)
		local goldText = gold:CreateFontString(gold:GetName() .. "Text", "ARTWORK")
		goldText:SetPoint("RIGHT", texture, "LEFT")
		goldText:SetHeight(13)
		goldText:SetFontObject(NumberFontNormal)
		goldText:SetText(0)
		goldText:SetJustifyH("RIGHT")
		function invCost:SetValue(value)
			local g = floor(value / 10000)
			local s = mod(floor(value / 100), 100)
			local c = mod(value, 100)
			goldText:SetText(g)
			silverText:SetText(s)
			copperText:SetText(c)
			if g == 0 then
				gold:Hide()
				if s == 0 then
					silver:Hide()
				else
					silver:Show()
				end
			else
				gold:Show()
				silver:Show()
			end
		end

		local bothCost = CreateFrame("Frame", popup:GetName() .. "BothCost", popup)
		popup.bothCost = bothCost
		bothCost:SetWidth(96)
		bothCost:SetHeight(13)
		bothCost:SetPoint("RIGHT", bothButton, "LEFT", -8, 0)
		local copper = CreateFrame("Frame", bothCost:GetName() .. "Copper", bothCost)
		copper:SetWidth(32)
		copper:SetHeight(13)
		copper:SetPoint("RIGHT", bothCost, "RIGHT")
		local texture = copper:CreateTexture(copper:GetName() .. "Texture", "ARTWORK")
		texture:SetPoint("RIGHT", copper, "RIGHT")
		texture:SetWidth(13)
		texture:SetHeight(13)
		texture:SetTexture("Interface\\MoneyFrame\\UI-MoneyIcons")
		texture:SetTexCoord(0.5, 0.75, 0, 1)
		local copperText = copper:CreateFontString(copper:GetName() .. "Text", "ARTWORK")
		copperText:SetPoint("RIGHT", texture, "LEFT")
		copperText:SetHeight(13)
		copperText:SetFontObject(NumberFontNormal)
		copperText:SetText(0)
		copperText:SetJustifyH("RIGHT")

		local silver = CreateFrame("Frame", bothCost:GetName() .. "Silver", bothCost)
		silver:SetWidth(32)
		silver:SetHeight(13)
		silver:SetPoint("RIGHT", copper, "LEFT")
		local texture = silver:CreateTexture(silver:GetName() .. "Texture", "ARTWORK")
		texture:SetPoint("RIGHT", silver, "RIGHT")
		texture:SetWidth(13)
		texture:SetHeight(13)
		texture:SetTexture("Interface\\MoneyFrame\\UI-MoneyIcons")
		texture:SetTexCoord(0.25, 0.5, 0, 1)
		local silverText = silver:CreateFontString(silver:GetName() .. "Text", "ARTWORK")
		silverText:SetPoint("RIGHT", texture, "LEFT")
		silverText:SetHeight(13)
		silverText:SetFontObject(NumberFontNormal)
		silverText:SetText(0)
		silverText:SetJustifyH("RIGHT")

		local gold = CreateFrame("Frame", bothCost:GetName() .. "Gold", bothCost)
		gold:SetWidth(32)
		gold:SetHeight(13)
		gold:SetPoint("RIGHT", silver, "LEFT")
		local texture = gold:CreateTexture(gold:GetName() .. "Texture", "ARTWORK")
		texture:SetPoint("RIGHT", gold, "RIGHT")
		texture:SetWidth(13)
		texture:SetHeight(13)
		texture:SetTexture("Interface\\MoneyFrame\\UI-MoneyIcons")
		texture:SetTexCoord(0, 0.25, 0, 1)
		local goldText = gold:CreateFontString(gold:GetName() .. "Text", "ARTWORK")
		goldText:SetPoint("RIGHT", texture, "LEFT")
		goldText:SetHeight(13)
		goldText:SetFontObject(NumberFontNormal)
		goldText:SetText(0)
		goldText:SetJustifyH("RIGHT")
		function bothCost:SetValue(value)
			local g = floor(value / 10000)
			local s = mod(floor(value / 100), 100)
			local c = mod(value, 100)
			goldText:SetText(g)
			silverText:SetText(s)
			copperText:SetText(c)
			if g == 0 then
				gold:Hide()
				if s == 0 then
					silver:Hide()
				else
					silver:Show()
				end
			else
				gold:Show()
				silver:Show()
			end
		end

		local eqLabel = popup:CreateFontString(popup:GetName() .. "EquipmentLabel", "ARTWORK")
		eqLabel:SetPoint("RIGHT", eqCost, "LEFT", -8, 0)
		eqLabel:SetFontObject(GameFontNormal)
		eqLabel:SetText(L["Equipment"])

		local invLabel = popup:CreateFontString(popup:GetName() .. "InventoryLabel", "ARTWORK")
		invLabel:SetPoint("RIGHT", invCost, "LEFT", -8, 0)
		invLabel:SetFontObject(GameFontNormal)
		invLabel:SetText(L["Inventory"])

		local bothLabel = popup:CreateFontString(popup:GetName() .. "BothLabel", "ARTWORK")
		bothLabel:SetPoint("RIGHT", bothCost, "LEFT", -8, 0)
		bothLabel:SetFontObject(GameFontNormal)
		bothLabel:SetText(L["Total"])

		popup:SetBackdrop({
			bgFile = "Interface\\TutorialFrame\\TutorialFrameBackground",
			edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
			tile = true,
			edgeSize = 16,
			tileSize = 32,
			insets = {
				left = 5,
				right = 5,
				top = 5,
				bottom = 5
			}
		})
	end

	popup:Show()

	local equipment = GetRepairAllCost()
	local inventory = self:CalculateInventoryCost()
	local both = equipment + inventory
	popup.bothButton:Disable()
	if equipment == 0 then
		popup.eqButton:Disable()
	else
		popup.eqButton:Enable()
		popup.bothButton:Enable()
	end
	if inventory == 0 then
		popup.invButton:Disable()
	else
		popup.invButton:Enable()
		popup.bothButton:Enable()
	end
	popup.eqCost:SetValue(equipment)
	popup.invCost:SetValue(inventory)
	popup.bothCost:SetValue(both)
end

function DurabilityFu:DurabilityFrame_SetAlerts()
	if not self:IsShowingMan() then
		DurabilityFrame:Hide()
	end
end
