Quantcast
local A, L = ...

local thinBackdrop = L.C.thin

local function HideDBMSpark(self)
	local spark = _G[self.frame:GetName().."BarSpark"]
	spark:SetAlpha(0)
	spark:SetTexture(nil)
end

local function ResetBackdrop(self)
	if self.backdrop then
		self.backdrop:SetBackdrop(nil)
	end
end

local function ApplyDBMStyle(self)
	local frame = self.frame
	local frame_name = frame:GetName()
	local tbar = _G[frame_name.."Bar"]
	local texture = _G[frame_name.."BarTexture"]
	local icon1 = _G[frame_name.."BarIcon1"]
	local icon2 = _G[frame_name.."BarIcon2"]
	local name = _G[frame_name.."BarName"]
	local timer = _G[frame_name.."BarTimer"]

	-- tex
	if texture then texture:SetTexture(L.C.bar.tex) end

	-- reset backdrop
	ResetBackdrop(tbar)
	ResetBackdrop(icon1)
	ResetBackdrop(icon2)
	name:ClearAllPoints()
    timer:ClearAllPoints()
	icon1:ClearAllPoints()
	icon2:ClearAllPoints()
	icon1:Hide()
	icon2:Hide()

	-- bar
	local barHeight = self.owner.Options.Height
	local barWidth = self.owner.Options.Width
	if self.enlarged then
		barHeight = self.owner.Options.HugeHeight/2
		barWidth = self.owner.Options.HugeWidth
	end
	tbar:SetSize(barWidth, barHeight)
	tbar:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, 0)

	-- frame
	frame:SetScale(L.C.scale)

	-- backdrop
	local iconOffsetX = 0
	if self.enlarged then
		iconOffsetX = 5
		tbar.backdrop = L.CreateBackdrop(tbar)
		if DBT.Options.IconLeft then
			icon1:Show()
			icon1.backdrop = L.CreateBackdrop(icon1)
		elseif DBT.Options.IconRight then
			icon2:Show()
			icon2.backdrop = L.CreateBackdrop(icon2)
		end
	else
		tbar.backdrop = L.CreateBackdrop(tbar, nil, thinBackdrop)
	end

	-- icon
	icon1:SetPoint('BOTTOMRIGHT', frame, 'BOTTOMLEFT', -iconOffsetX, 0)
	icon2:SetPoint('BOTTOMLEFT', frame, 'BOTTOMRIGHT', iconOffsetX, 0)

	-- text
    if self.enlarged then
        name:SetPoint("BOTTOMLEFT", tbar, "TOPLEFT", 3, 3)
        timer:SetPoint("BOTTOMRIGHT", tbar, "TOPRIGHT", -3, 3)
    else
        name:SetPoint("LEFT", tbar, "LEFT", 3, 0)
        timer:SetPoint("RIGHT", tbar, "RIGHT", -3, 0)
    end
end

local function SkinDBM()
    -- Default notice message
	local RaidNotice_AddMessage_ = RaidNotice_AddMessage
	RaidNotice_AddMessage = function(noticeFrame, textString, colorInfo)
		if strfind(textString, "|T") then
			if strmatch(textString, ":(%d+):(%d+)") then
				local size1, size2 = strmatch(textString, ":(%d+):(%d+)")
				size1, size2 = size1 + 3, size2 + 3
				textString = gsub(textString,":(%d+):(%d+)",":"..size1..":"..size2..":0:0:64:64:5:59:5:59")
			elseif strmatch(textString, ":(%d+)|t") then
				local size = strmatch(textString, ":(%d+)|t")
				size = size + 3
				textString = gsub(textString,":(%d+)|t",":"..size..":"..size..":0:0:64:64:5:59:5:59|t")
			end
		end
		return RaidNotice_AddMessage_(noticeFrame, textString, colorInfo)
	end

    if not IsAddOnLoaded("DBM-Core") then return end

    hooksecurefunc(DBT, "CreateBar", function(self)
		for bar in self:GetBarIterator() do
			if not bar.injected then
				hooksecurefunc(bar, "Update", HideDBMSpark)
				hooksecurefunc(bar, "ApplyStyle", ApplyDBMStyle)
				bar:ApplyStyle()

				bar.injected = true
			end
		end
	end)

	if rSkinDB.resets.dbm then
		-- Force Settings
		if not DBM_AllSavedOptions["Default"] then DBM_AllSavedOptions["Default"] = {} end
		-- DBM_AllSavedOptions["Default"]["BlockVersionUpdateNotice"] = true
		DBM_AllSavedOptions["Default"]["EventSoundVictory"] = "None"
		if IsAddOnLoaded("DBM-VPYike") then
			DBM_AllSavedOptions["Default"]["CountdownVoice"] = "VP:Yike"
			DBM_AllSavedOptions["Default"]["ChosenVoicePack"] = "Yike"
		end

		local hugeBarWidth, hugeBarHeight = unpack(L.C.bar.big)
		local barWidth, barheight = unpack(L.C.bar.small)
		if not DBT_AllPersistentOptions["Default"] then DBT_AllPersistentOptions["Default"] = {} end

		-- place on top of details
		local Details = _G.Details
		local timerX, timerY = (-(barWidth/2)-10+1)*L.C.scale, Details and 185 or 69

		-- normal bar
		DBT_AllPersistentOptions["Default"]["DBM"].BarYOffset = L.C.bar.space.small
		DBT_AllPersistentOptions["Default"]["DBM"].Height = barheight
		DBT_AllPersistentOptions["Default"]["DBM"].Width = barWidth
		DBT_AllPersistentOptions["Default"]["DBM"].ExpandUpwards = true
		DBT_AllPersistentOptions["Default"]["DBM"].TimerPoint = "BOTTOMRIGHT"
		DBT_AllPersistentOptions["Default"]["DBM"].TimerX = timerX
		DBT_AllPersistentOptions["Default"]["DBM"].TimerY = timerY

		-- huge bar
		DBT_AllPersistentOptions["Default"]["DBM"].HugeBarYOffset = L.C.bar.space.big
		DBT_AllPersistentOptions["Default"]["DBM"].ExpandUpwardsLarge = true
		DBT_AllPersistentOptions["Default"]["DBM"].HugeHeight = hugeBarHeight
		DBT_AllPersistentOptions["Default"]["DBM"].HugeWidth = hugeBarWidth
		if _G.oUF_SimpleTarget then
			local point, _, _, xOfs, yOfs = _G.oUF_SimpleTarget:GetPoint(1)
			DBT_AllPersistentOptions["Default"]["DBM"].HugeTimerPoint = point
			DBT_AllPersistentOptions["Default"]["DBM"].HugeTimerX = xOfs*L.C.scale
			DBT_AllPersistentOptions["Default"]["DBM"].HugeTimerY = yOfs+hugeBarHeight*3+18
		end

		-- rearrange
		DBT:Rearrange()
		rSkinDB.resets.dbm = false
	end
end

rSkin:RegisterSkin('DBM', SkinDBM)