local E, L, V, P, G = unpack(ElvUI);
local mPlugin = "mMediaTag"
local mMT = E:GetModule(mPlugin);
local DT = E:GetModule("DataTexts");
local addon, ns = ...
local mText = format("Dock %s", CHARACTER_BUTTON)
local mTextName = "mCharacter"

local DURABILITY = DURABILITY
local REPAIR_COST = REPAIR_COST
local tooltipString = '%d%%'
local totalDurability = 0
local invDurability = {}
local totalRepairCost
local r, g, b = nil

local slots = {
	[1] = _G.INVTYPE_HEAD,
	[3] = _G.INVTYPE_SHOULDER,
	[5] = _G.INVTYPE_CHEST,
	[6] = _G.INVTYPE_WAIST,
	[7] = _G.INVTYPE_LEGS,
	[8] = _G.INVTYPE_FEET,
	[9] = _G.INVTYPE_WRIST,
	[10] = _G.INVTYPE_HAND,
	[16] = _G.INVTYPE_WEAPONMAINHAND,
	[17] = _G.INVTYPE_WEAPONOFFHAND,
}

local function mCheckDurability()
	if totalDurability <= E.global.datatexts.settings.Durability.percThreshold then
		r, g, b = E:ColorGradient(totalDurability * .01, 1, .1, .1, 1, 1, .1, .1, 1, .1)
		return r, g, b, 1
	end
end

local function mDockCheckFrame()
	return ( CharacterFrame and CharacterFrame:IsShown() )
end

function mMT:CheckFrameCharacter(self)
	if totalDurability <= E.global.datatexts.settings.Durability.percThreshold then
		r, g, b = E:ColorGradient(totalDurability * .01, 1, .1, .1, 1, 1, .1, .1, 1, .1)
		mMT:DockTimerCheck(self, mDockCheckFrame(), true, r, g, b, 1)
	else
		mMT:DockTimerCheck(self, mDockCheckFrame())
	end
end

local function OnEnter(self)
	mMT:mOnEnter(self, CHARACTER_BUTTON)

	if E.db[mPlugin].mDock.tip.mode == "extend" then
		DT.tooltip:ClearLines()

		if mCheckDurability() then
			self.mIcon:SetVertexColor(mCheckDurability())
		end

		for slot, durability in pairs(invDurability) do
			DT.tooltip:AddDoubleLine(format('|T%s:14:14:0:0:64:64:4:60:4:60|t  %s', GetInventoryItemTexture('player', slot), GetInventoryItemLink('player', slot)), format(tooltipString, durability), 1, 1, 1, E:ColorGradient(durability * 0.01, 1, .1, .1, 1, 1, .1, .1, 1, .1))
		end

		if totalRepairCost > 0 then
			DT.tooltip:AddLine(' ')
			DT.tooltip:AddDoubleLine(REPAIR_COST, GetMoneyString(totalRepairCost), .6, .8, 1, 1, 1, 1)
		end

		DT.tooltip:Show()
	end
end

local function OnEvent(self, event, ...)
	mMT:mOnEvent(self, nil, true, nil, mDockCheckFrame(), E.db[mPlugin].mDock.textures.character.path, "CheckFrameCharacter")

	totalDurability = 100
	totalRepairCost = 0

	wipe(invDurability)

	for index in pairs(slots) do
		local currentDura, maxDura = GetInventoryItemDurability(index)
		if currentDura and maxDura > 0 then
			local perc = (currentDura/maxDura)*100
			invDurability[index] = perc

			if perc < totalDurability then
				totalDurability = perc
			end

			totalRepairCost = totalRepairCost + select(3, E.ScanTooltip:SetInventoryItem('player', index))
		end
	end
	
	if mCheckDurability() then
		E:Flash(self, 0.5, true)
		self.mIcon:SetVertexColor(mCheckDurability())
	else
		E:StopFlash(self)
		self.mIcon:SetVertexColor(mMT:mSetColor("normal"))
	end

end

local function OnLeave(self)
	mMT:mOnLeave(self, mDockCheckFrame())

	if mCheckDurability() then
		self.mIcon:SetVertexColor(mCheckDurability())
	end
end

local function OnClick(self)
	mMT:mClick(self, "CheckFrameCharacter")
	if mCheckDurability() then
		self.mIcon:SetVertexColor(mCheckDurability())
	end
	if not mMT:mCheckCombat() then
		ToggleCharacter("PaperDollFrame")
	end
end

DT:RegisterDatatext(mTextName, nil, {'UPDATE_INVENTORY_DURABILITY', 'MERCHANT_SHOW'}, OnEvent, nil, OnClick, OnEnter, OnLeave, mText, nil)