local E, L, V, P, G = unpack(ElvUI);
local mPlugin = "mMediaTag"
local mMT = E:GetModule(mPlugin);
local DT = E:GetModule("DataTexts");
local addon, ns = ...

--Lua functions
local format = format
local strjoin = strjoin

--WoW API / Variables
local IsInInstance = IsInInstance

--Variables
local mText = format("Dock %s", DUNGEONS_BUTTON)
local mTextName = "mLFDTool"
local displayString, lastPanel = ""
local mInctanceInfoText, keyText, mAffixesText, vaultinforaidText, vaultinfomplusText, vaultinfopvpText = {}, {}, {}, {}, {}, {}

local function mLFDTooltip()
	local nhc, hc, myth, mythp, other, titel, tip = mMT:mColorDatatext()
	
	mInctanceInfoText = mMT:InctanceInfo()
	if mInctanceInfoText then
		DT.tooltip:AddLine(" ")
		DT.tooltip:AddLine(mInctanceInfoText[1])
		DT.tooltip:AddLine(mInctanceInfoText[2])
		DT.tooltip:AddLine(mInctanceInfoText[3])
	end
	
	if E.db[mPlugin].mDock.lfd.keystone then
		keyText = mMT:OwenKeystone()
		if keyText then
			DT.tooltip:AddLine(" ")
			DT.tooltip:AddLine(keyText[1])
			DT.tooltip:AddLine(keyText[2])
		end 
		
	end
	
	if E.db[mPlugin].mDock.lfd.affix then
		mAffixesText = mMT:WeeklyAffixes()
		if mAffixesText then
			DT.tooltip:AddLine(" ")
			if mAffixesText[3] then
				DT.tooltip:AddLine(mAffixesText[3])
			else
				DT.tooltip:AddLine(mAffixesText[1])
				DT.tooltip:AddLine(mAffixesText[2])
			end
		end
	end
	
	if E.db[mPlugin].mDock.lfd.greatvault then
		local vaultinfohighest, ok = 0, false
		vaultinforaidText, vaultinfomplusText, vaultinfopvpText, 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 vaultinforaidText[1] then
				DT.tooltip:AddDoubleLine(format("%sRaid|r", myth), format("%s, %s, %s", vaultinforaidText[1] or "-", vaultinforaidText[2] or "-", vaultinforaidText[3] or "-"))
			end
			
			if vaultinfomplusText[1] then
				DT.tooltip:AddDoubleLine(format("%sMyth+|r", mythp), format("%s, %s, %s", vaultinfomplusText[1] or "-", vaultinfomplusText[2] or "-", vaultinfomplusText[3] or "-"))
			end
			
			if vaultinfopvpText[1] then
				DT.tooltip:AddDoubleLine(format("%sPvP|r", hc), format("%s, %s, %s", vaultinfopvpText[1] or "-", vaultinfopvpText[2] or "-", vaultinfopvpText[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, tip, L["left click to open LFD Window"]))
	if E.db[mPlugin].mDock.lfd.greatvault then
		DT.tooltip:AddLine(format("%s %s%s|r", ns.RightButtonIcon, tip, L["right click to open Great Vault Window"]))
	end
end

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

function mMT:CheckFrameLFDTool(self)
	self.mIcon.isClicked = mDockCheckFrame()
	mMT:DockTimer(self)
end

local function OnEnter(self)
	self.mIcon.isClicked = mDockCheckFrame()
	mMT:mOnEnter(self)

	if E.db[mPlugin].mDock.tip.enable then
		mLFDTooltip()
		DT.tooltip:Show()
	end
end

local function OnEvent(self, event)
	self.mSettings = {
		Name = mTextName,
		IconTexture = E.db[mPlugin].mDock.lfd.path,
		Notifications = true,
		Text = true,
		Spezial = false,
	}

	mMT:DockInitialisation(self)

	if E.db[mPlugin].mDock.lfd.greatvault and C_WeeklyRewards.HasAvailableRewards() then
		self.HasAvailableRewardsTimer = mMT:ScheduleRepeatingTimer("GreatVaultTimer", 2, self)
		self.mNotifications:Show()
		if E.db[mPlugin].mDock.nottification.flash then
			E:Flash(self.mNotifications, 0.5, true)
		end
	end

	if E.db[mPlugin].mDock.lfd.greatvault and not C_WeeklyRewards.HasAvailableRewards() then
		self.mNotifications:Hide()
		E:StopFlash(self.mNotifications, 0.5, true)
	end

	local inInstance, _ = IsInInstance()
	
	if E.db[mPlugin].mDock.lfd.difficulty and inInstance then
		self.mIcon.TextA:SetText(mMT:DungeonDifficultyShort())
	else 
		self.mIcon.TextA:SetText("")
	end

	lastPanel = self
end

local function OnLeave(self)
	if E.db[mPlugin].mDock.tip.enable then
		DT.tooltip:Hide()
	end

	self.mIcon.isClicked = mDockCheckFrame()
	mMT:mOnLeave(self)
end

local function OnClick(self, button)
	if self.mNotifications:IsShown() then
		self.mNotifications:Hide()
		if E.db[mPlugin].mDock.nottification.flash then
			E:StopFlash(self.mNotifications)
		end
	end
	
	if mMT:CheckCombatLockdown() then
		mMT:mOnClick(self, "CheckFrameLFDTool")
		if E.db[mPlugin].mDock.lfd.greatvault then
			if button == "LeftButton" then
				ToggleLFDParentFrame()
			else
				if not WeeklyRewardsFrame then
					LoadAddOn("Blizzard_WeeklyRewards")
				end
				if WeeklyRewardsFrame:IsVisible() then
					WeeklyRewardsFrame:Hide()
				else
					WeeklyRewardsFrame:Show()
				end
			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, "mDock", {"CHALLENGE_MODE_START", "CHALLENGE_MODE_COMPLETED", "PLAYER_ENTERING_WORLD", "UPDATE_INSTANCE_INFO"}, OnEvent, nil, OnClick, OnEnter, OnLeave, mText, nil, ValueColorUpdate)