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

--WoW API / Variables
local _G = _G
local InCombatLockdown = InCombatLockdown

--Variables
local LSM = E.Libs.LSM
local _, unitClass = UnitClass('player')
local class = ElvUF.colors.class[unitClass]

-- Dock Combat check
function mMT:mCheckCombat()
	if InCombatLockdown() then
		_G.UIErrorsFrame:AddMessage(format("%s%s|r", ns.mColor5, _G.ERR_NOT_IN_COMBAT))
	else
		return true
	end
end

function mMT:mSetColor(color)
	if color == "normal" then
		if E.db[mPlugin].mDock.normal.style == "class" then
			return class[1], class[2], class[3], E.db[mPlugin].mDock.nottification.a
		else
			return E.db[mPlugin].mDock.normal.r, E.db[mPlugin].mDock.normal.g, E.db[mPlugin].mDock.normal.b, E.db[mPlugin].mDock.normal.a
		end
	elseif color == "click" then
		if E.db[mPlugin].mDock.click.style == "class" then
			return class[1], class[2], class[3], E.db[mPlugin].mDock.nottification.a
		else
			return E.db[mPlugin].mDock.click.r, E.db[mPlugin].mDock.click.g, E.db[mPlugin].mDock.click.b, E.db[mPlugin].mDock.click.a
		end
	elseif color == "hover" then
		if E.db[mPlugin].mDock.hover.style == "class" then
			return class[1], class[2], class[3], E.db[mPlugin].mDock.nottification.a
		else
			return E.db[mPlugin].mDock.hover.r, E.db[mPlugin].mDock.hover.g, E.db[mPlugin].mDock.hover.b, E.db[mPlugin].mDock.hover.a
		end
	elseif color == "notifi" then
		if E.db[mPlugin].mDock.nottification.style == "class" then
			return class[1], class[2], class[3], E.db[mPlugin].mDock.nottification.a
		else
			return E.db[mPlugin].mDock.nottification.r, E.db[mPlugin].mDock.nottification.g, E.db[mPlugin].mDock.nottification.b, E.db[mPlugin].mDock.nottification.a
		end
	end
end

function mMT:mFade(self, FadeIn)
	if FadeIn then
		E:UIFrameFadeIn(self.mIcon, E.db[mPlugin].mDock.fadetime, E.db[mPlugin].mDock.normal.a, E.db[mPlugin].mDock.fadealpha)
	else
		E:UIFrameFadeOut(self.mIcon, E.db[mPlugin].mDock.fadetime, E.db[mPlugin].mDock.fadealpha, E.db[mPlugin].mDock.normal.a)
	end
end

local function SetUpIcon(self, notifi, text)
	if not self.loaded then
		self.loaded = true
		self.mIcon 	= self:CreateTexture(nil, "ARTWORK")
		self.mIcon:Point("CENTER")
		
		if notifi then
			self.mNotifications	= self:CreateTexture(nil, "ARTWORK")
			self.mNotifications:Point("TOPLEFT", 2, 2)
		end
		
		if text then
			self.mText = self:CreateFontString(self, "OVERLAY", "GameTooltipText")
			self.mText:SetFont(LSM:Fetch('font', E.db[mPlugin].mDock.font), E.db[mPlugin].mDock.fontSize or E.db["datatexts"]["fontSize"], E.db[mPlugin].mDock.fontflag or E.db["actionbar"]["fontOutline"])
			self.mText:SetPoint("BOTTOMRIGHT", 2, 2)
		end
	end
end

local function UpdateIcon(self, notifi, texture)
	local xy = E.db[mPlugin].mDock.sizexy
	self:Size(xy, xy)
	self.mIcon:SetTexture(texture)
	self.mIcon:Size(xy, xy)
	
	if notifi then
		local nxy = E.db[mPlugin].mDock.nottification.size or 8
		self.mNotifications:SetTexture(E.db[mPlugin].mDock.textures.nottification.path)
		self.mNotifications:Size(nxy, nxy)
		self.mNotifications:SetVertexColor(mMT:mSetColor("notifi"))
		self.mNotifications:Hide()
	end
	
	self.hover 	= false
end

local function CheckWindow(self, option, leave, timer)
	if option then
		self.down 	= true
		self.mIcon:SetVertexColor(mMT:mSetColor("click"))
		if timer then
			self.CheckTimer = mMT:ScheduleRepeatingTimer(timer, 2, self)
		end
	else
		self.down 	= false
		self.mIcon:SetVertexColor(mMT:mSetColor("normal"))
	end
	
	if leave then
		mMT:mFade(self, false)
	end
end

local function mDownCheck(self)
	if self.down then
		self.mIcon:SetVertexColor(mMT:mSetColor("click"))
	else
		self.mIcon:SetVertexColor(mMT:mSetColor("hover"))
		mMT:mFade(self, true)
	end
end

local function mSetHover(self)
	local xy = E.db[mPlugin].mDock.sizexy
	self.hover 	= true
	self.mIcon:Size(xy+E.db[mPlugin].mDock.growsize, xy+E.db[mPlugin].mDock.growsize)
end

function mMT:mOnEvent(self, notifi, text, displayString, option, texture, timer)--xxxx
	SetUpIcon(self, notifi or false, text or false)
	UpdateIcon(self, notifi, texture)
	if text and displayString then
		self.mText:SetFormattedText(displayString, text)
	elseif text then
		self.mText:SetText(text)
	end
	
	if not self.CheckTimer then
		CheckWindow(self, option, false, timer)
	end
end

function mMT:mOnLeave(self, option)--xxxx
	local xy = E.db[mPlugin].mDock.sizexy
	CheckWindow(self, option, true)
	self.hover	= false
	self.mIcon:Size(xy, xy)
	if E.db[mPlugin].mDock.tip.enable then
		DT.tooltip:Hide()
	end
end

function mMT:mClick(self, timer)--xxxx
	if self.down then
		if self.hover then
			self.mIcon:SetVertexColor(mMT:mSetColor("hover"))
		else
			self.mIcon:SetVertexColor(mMT:mSetColor("normal"))
		end
		self.down	= false
		mMT:CancelTimer(self.CheckTimer)
	else
		self.mIcon:SetVertexColor(mMT:mSetColor("click"))
		self.down 	= true
		self.CheckTimer = mMT:ScheduleRepeatingTimer(timer, 2, self)
	end
end

function mMT:mOnEnter(self, text)--xxxx
	mDownCheck(self)
	mSetHover(self)
	
	if E.db[mPlugin].mDock.tip.enable and text then
		DT.tooltip:AddLine(text)
		DT.tooltip:Show()
	end
end


function mMT:DockTimerCheck(self, option, color, r, g, b, a)--xxxx
	if option then
		self.down 	= true
		if color then
			self.mIcon:SetVertexColor(r, g, b, a)
		else
			self.mIcon:SetVertexColor(mMT:mSetColor("click"))
		end
	else
		self.down 	= false
		if color then
			self.mIcon:SetVertexColor(r, g, b, a)
		else
			self.mIcon:SetVertexColor(mMT:mSetColor("normal"))
		end
		mMT:CancelTimer(self.CheckTimer)
	end
end