local _, Obituary = ...

-- LibQTip! Create a simple CellProvider to top justify text
local LibQTip = LibStub("LibQTip-1.0")

local LabelVTopProvider
do
	local cellPrototype, baseCellPrototype
	LabelVTopProvider, cellPrototype, baseCellPrototype = LibQTip:CreateCellProvider(LibQTip.LabelProvider)
	function cellPrototype:SetupCell(tooltip, value, justification, font, ...)
		local width, height = baseCellPrototype.SetupCell(self, tooltip, value, justification, font, ...)
		self.fontString:SetJustifyV("TOP")
		return width, height
	end
end

local TooltipText = CreateFont("ObituaryTextFont")
TooltipText:CopyFontObject(GameTooltipText)
TooltipText:SetTextColor(NORMAL_FONT_COLOR.r, NORMAL_FONT_COLOR.g, NORMAL_FONT_COLOR.b)

--our tooltip container
local frame = {}
Obituary.Tooltip = frame

-- close button to mimic ItemRefTip
local closeButton = CreateFrame("Button", "ObituaryTooltipCloseButton")
closeButton:SetSize(32, 32)
closeButton:SetNormalTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Up")
closeButton:SetPushedTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Down")
closeButton:SetHighlightTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight", "ADD")
closeButton:SetScript("OnClick", function() frame:Hide() end)
frame.close = closeButton

local function OnLinkClick(self, msg)
	if msg and IsModifiedClick("CHATLINK") then --IsShiftKeyDown()
		-- strip strip strip, don't understand why blizzard can't just remove formatting automagically
		msg = msg:gsub("|Hicon:%d+:source|h|TInterface.TargetingFrame.UI%-RaidTargetingIcon_(%d).blp:0|t|h", "{rt%1}")
		msg = msg:gsub("|c%x%x%x%x%x%x%x%x([^%|].-)|r", "%1") --remove color other than for spell links

		local active = ChatEdit_GetActiveWindow()
		if active then
			active:Insert(" "..msg)
		else
			ChatFrame_OpenChat(msg)
		end
	end
end

local function OnLinkEnter(self, link)
	GameTooltip:SetOwner(self, "ANCHOR_RIGHT", 0)
	GameTooltip:SetFrameLevel(GameTooltip:GetFrameLevel() + 10)
	GameTooltip:SetHyperlink(link)
	GameTooltip:Show()
end

local function OnLinkLeave(self)
	GameTooltip:Hide()
end

local function OnDragStop(self)
	self:StopMovingOrSizing()

	Obituary.db.profile.tooltipX = self:GetLeft()
	Obituary.db.profile.tooltipY = self:GetTop()
end

frame.Acquire = function(self)
	local tip = LibQTip:Acquire("ObituaryReportingFrame", 3, "RIGHT", "LEFT", "RIGHT")
	self.tip = tip

	tip:SetDefaultProvider(LabelVTopProvider)
	tip:SetFont(TooltipText)
	tip:SetScrollStep(100) -- 400 = full page

	local closeButton = self.close
	closeButton:SetParent(tip)
	closeButton:SetPoint("TOPRIGHT", tip, "TOPRIGHT", 0, 0)
	RaiseFrameLevelByTwo(closeButton)
	closeButton:Show()

	-- use ItemRefTooltip's default colors, otherwise we can get stuck with the color of the last unit we moused over
	tip:SetBackdropColor(ItemRefTooltip:GetBackdropColor())
	tip:SetBackdropBorderColor(ItemRefTooltip:GetBackdropBorderColor())

	if TipTac and TipTac.AddModifiedTip then
		TipTac:AddModifiedTip(tip, true)
	end

	tip:SetClampedToScreen(nil)
	tip:EnableMouse(true)
	tip:SetMovable(true)
	tip:RegisterForDrag("LeftButton")
	tip:SetScript("OnDragStart", tip.StartMoving)
	tip:SetScript("OnDragStop", OnDragStop)

	-- anchoring: use a saved point if set, otherwise use ItemRefTooltip's position
	tip:ClearAllPoints()
	if Obituary.db.profile.tooltipX then
		tip:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", Obituary.db.profile.tooltipX, Obituary.db.profile.tooltipY)
	else
		local pos, _, rel, x, y = ItemRefTooltip:GetPoint()
		tip:SetPoint(pos, UIParent, rel, x, y)
	end

	return tip
end

frame.Release = function(self)
	local tip = self.tip
	tip:SetClampedToScreen(nil)
	tip:EnableMouse(nil)
	tip:SetMovable(nil)
	tip:RegisterForDrag(nil)
	tip:SetScript("OnDragStart", nil)
	tip:SetScript("OnDragStop", nil)

	self.close:Hide()
	self.close:SetParent(nil)

	LibQTip:Release(self.tip)
	self.tip = nil
end

frame.Show = function(self)
	if not self.tip then return end

	self.tip:UpdateScrolling(439) -- ~27 lines
	self.tip:SetClampedToScreen(true)
	self.tip:Show()
end

frame.Hide = function(self)
	if not self.tip then return end
	self.tip:Hide()
	self:Release()
end

frame.SetText = function(self, text)
	local tip = self.tip or self:Acquire()
	tip:Clear()
	local line = tip:AddHeader()
	tip:SetCell(line, 1, text, nil, "LEFT", 3)
end

frame.AddLine = function(self, health, event, ttd, spell)
	local tip = self.tip or self:Acquire()
	local line = tip:AddLine()
	if not event then
		tip:SetCell(line, 1, health, nil, "LEFT", 3)
	else
		-- set the line click action
		if health then
			tip:SetLineScript(line, "OnMouseDown", OnLinkClick, format("%s: %s %s", ttd, health, event))
		else
			tip:SetLineScript(line, "OnMouseDown", OnLinkClick, format("%s: %s", ttd, event))
		end

		if spell then
			-- show spell tooltip on mouseover
			tip:SetLineScript(line, "OnEnter", OnLinkEnter, spell)
			tip:SetLineScript(line, "OnLeave", OnLinkLeave)

			-- show an inline spell icon (that isn't sent to chat)
			local id = spell:match("spell:(%d+)")
			local _, _, icon = GetSpellInfo(id)
			if icon then
				event = event:gsub("(|cff71d5ff|Hspell)", ("|T%s:0:0:1:0:64:64:4:60:4:60|t%%1"):format(icon)) -- zoom the icon texture a bit
			end
		end

		tip:SetCell(line, 1, health or "", nil, nil, nil, nil, nil, 2)
		tip:SetCell(line, 2, event, nil, nil, nil, nil, nil, 2, 400)
		tip:SetCell(line, 3, ttd)
	end
end

frame.AddSeparator = function(self, ...)
	local tip = self.tip or self:Acquire()
	local line = tip:AddSeparator(...)
end

frame.ClearLines = function(self, text)
	local tip = self.tip or self:Acquire()
	self.tip:Clear()
end

