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", DUNGEONS_BUTTON)
local mTextName = "mLFDTool"

local function mDockTooltip()
	DT.tooltip:ClearLines()
	local mInctanceInfo = mMT:InctanceInfo()
	if mInctanceInfo then
		DT.tooltip:AddLine(" ")
		DT.tooltip:AddLine(mInctanceInfo[1])
		DT.tooltip:AddLine(mInctanceInfo[2])
		DT.tooltip:AddLine(mInctanceInfo[3])
	end

	if E.db[mPlugin].mDock.settings.lfd.keystone then
		local key = mMT:OwenKeystone()
		if key then
			DT.tooltip:AddLine(" ")
			DT.tooltip:AddLine(key[1])
			DT.tooltip:AddLine(key[2])
		end 
		
	end

	if E.db[mPlugin].mDock.settings.lfd.affix then
		local mAffixes = mMT:WeeklyAffixes()
		if mAffixes then
			DT.tooltip:AddLine(" ")
			if mAffixes[3] then
				DT.tooltip:AddLine(mAffixes[3])
			else
				DT.tooltip:AddLine(mAffixes[1])
				DT.tooltip:AddLine(mAffixes[2])
			end
		end
	end

	DT.tooltip:AddLine(" ")
	DT.tooltip:AddLine(format("%s %s%s|r", ns.LeftButtonIcon, E.db[mPlugin].mDataText.colortip.hex, L["left click to open LFD Window"]))
	if E.db[mPlugin].mDock.settings.lfd.greatvault then
		DT.tooltip:AddLine(format("%s %s%s|r", ns.RightButtonIcon, E.db[mPlugin].mDataText.colortip.hex, L["right click to open Great Vault Window"]))
	end
end

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

function mMT:CheckFrameLFDTool(self)
	mMT:DockTimerCheck(self, mDockCheckFrame())
end

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

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

local function OnEvent(self, event)
	mMT:mOnEvent(self, nil, nil, nil, mDockCheckFrame(), E.db[mPlugin].mDock.textures.lfd.path, "CheckFrameLFDTool")
end

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

local function OnClick(self, button)
	mMT:mClick(self, "CheckFrameLFDTool")
	if not mMT:mCheckCombat() then
		if E.db[mPlugin].mDock.settings.lfd.greatvault then
			if button == "LeftButton" then
				ToggleLFDParentFrame()
			else
				if WeeklyRewardsFrame:IsVisible() then
					WeeklyRewardsFrame:Hide()
				else
					WeeklyRewardsFrame:Show()
				end
			end
		else
			ToggleLFDParentFrame()
		end
    end
end

DT:RegisterDatatext(mTextName, nil, nil, OnEvent, nil, OnClick, OnEnter, OnLeave, mText, nil)