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 displayString, lastPanel = ""

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

	if E.db[mPlugin].mDock.settings.lfd.greatvault then
		local nhc, hc, myth, mythp, other, titel = E.db[mPlugin].mDataText.colornhc.hex, E.db[mPlugin].mDataText.colorhc.hex, E.db[mPlugin].mDataText.colormyth.hex, E.db[mPlugin].mDataText.colormythplus.hex, E.db[mPlugin].mDataText.colorother.hex, E.db[mPlugin].mDataText.colortitel.hex
		local vaultinforaid, vaultinfomplus, vaultinfopvp, vaultinfohighest, ok = mMT:mGetVaultInfo()
		if ok then
			DT.tooltip:AddLine(" ")
			DT.tooltip:AddLine(format("%s%s|r", titel, GREAT_VAULT_REWARDS))

			if vaultinfohighest then
				DT.tooltip:AddDoubleLine(format("%sAktuelle Belohnung:|r", other), vaultinfohighest or "-")
			end

			if vaultinforaid[1] then
				DT.tooltip:AddDoubleLine(format("%sRaid|r", myth), format("%s, %s, %s", vaultinforaid[1] or "-", vaultinforaid[2] or "-", vaultinforaid[3] or "-"))
			end

			if vaultinfomplus[1] then
				DT.tooltip:AddDoubleLine(format("%sMyth+|r", mythp), format("%s, %s, %s", vaultinfomplus[1] or "-", vaultinfomplus[2] or "-", vaultinfomplus[3] or "-"))
			end

			if vaultinfopvp[1] then
				DT.tooltip:AddDoubleLine(format("%sPvP|r", hc), format("%s, %s, %s", vaultinfopvp[1] or "-", vaultinfopvp[2] or "-", vaultinfopvp[3] or "-"))
			end 
		end
	if C_WeeklyRewards.HasAvailableRewards() then
			DT.tooltip:AddLine(" ")
			DT.tooltip:AddLine(format("%s%s|r", titel, GREAT_VAULT_REWARDS_WAITING))
		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)
	if E.db[mPlugin].mDock.settings.lfd.difficulty then
		local text = mMT:DungeonDifficultyShort()
		local inInstance, _ = IsInInstance()
		if inInstance and text ~= nil then
			mMT:mOnEvent(self, nil, text, displayString, mDockCheckFrame(), E.db[mPlugin].mDock.textures.lfd.path, "CheckFrameLFDTool")
		else
			mMT:mOnEvent(self, nil, " ", nil, mDockCheckFrame(), E.db[mPlugin].mDock.textures.lfd.path, "CheckFrameLFDTool")
		end
		lastPanel = self
	else
		mMT:mOnEvent(self, nil, " ", nil, mDockCheckFrame(), E.db[mPlugin].mDock.textures.lfd.path, "CheckFrameLFDTool")
	end
end

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

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

local function ValueColorUpdate(hex)
	displayString = strjoin("", hex, "%s|r")

	if lastPanel ~= nil then
		OnEvent(lastPanel, "ELVUI_COLOR_UPDATE")
	end
end
E.valueColorUpdateFuncs[ValueColorUpdate] = true

DT:RegisterDatatext(mTextName, nil, {"CHALLENGE_MODE_START", "CHALLENGE_MODE_COMPLETED", "PLAYER_ENTERING_WORLD", "UPDATE_INSTANCE_INFO"}, OnEvent, nil, OnClick, OnEnter, OnLeave, mText, ValueColorUpdate)