local MER, F, E, I, V, P, G, L = unpack(ElvUI_MerathilisUI)
local module = MER:GetModule("MER_UnitFrames")
local UF = E:GetModule("UnitFrames")
local AB = E:GetModule("ActionBars")

local CreateVector3D = CreateVector3D
local hooksecurefunc = hooksecurefunc

function module:ADDON_LOADED(event, addon)
	if addon ~= "ElvUI_Config" then
		return
	end

	module:UnregisterEvent(event)
end

function module:CreateHighlight(frame)
	if not frame then
		return
	end
	if not E.db.mui.unitframes.highlight then
		return
	end

	local hl = frame:CreateTexture(nil, "BACKGROUND")
	hl:SetAllPoints()
	hl:SetTexture("Interface\\PETBATTLES\\PetBattle-SelectedPetGlow")
	hl:SetTexCoord(0, 1, 0.5, 1)
	hl:SetVertexColor(1, 1, 0.6, 1)
	hl:SetBlendMode("ADD")
	hl:Hide()
	frame.Highlight = hl

	frame:HookScript("OnEnter", function()
		frame.Highlight:Show()
	end)
	frame:HookScript("OnLeave", function()
		frame.Highlight:Hide()
	end)
end

function module:Initialize()
	if not E.private.unitframe.enable then
		return
	end

	local db = E.db.mui.unitframes

	-- Player
	hooksecurefunc(UF, "Update_PlayerFrame", module.Update_PlayerFrame)
	-- Target
	hooksecurefunc(UF, "Update_TargetFrame", module.Update_TargetFrame)
	-- TargetTarget
	hooksecurefunc(UF, "Update_TargetTargetFrame", module.Update_TargetTargetFrame)
	-- Pet
	hooksecurefunc(UF, "Update_PetFrame", module.Update_PetFrame)
	-- Focus
	hooksecurefunc(UF, "Update_FocusFrame", module.Update_FocusFrame)
	-- FocusTarget
	hooksecurefunc(UF, "Update_FocusTargetFrame", module.Update_FocusTargetFrame)
	-- Party
	hooksecurefunc(UF, "Update_PartyFrames", module.Update_PartyFrames)
	-- Raid
	hooksecurefunc(UF, "Update_RaidFrames", module.Update_RaidFrames)
	-- Boss
	hooksecurefunc(UF, "Update_BossFrames", module.Update_BossFrames)
	-- Castbar
	hooksecurefunc(UF, "LoadUnits", module.CastBarHooks)
	hooksecurefunc(UF, "LoadUnits", module.UpdateAllCastbars)
	-- Power Textures
	module:ChangePowerBarTexture()
	hooksecurefunc(UF, "Update_StatusBars", module.ChangePowerBarTexture)
	hooksecurefunc(UF, "Update_AllFrames", module.ChangeUnitPowerBarTexture)
	hooksecurefunc(AB, "StyleShapeShift", module.ChangeUnitPowerBarTexture)
	-- RaidIcons
	hooksecurefunc(UF, "Configure_RaidIcon", module.Configure_RaidIcon)
	-- RoleIcons
	module:Configure_RoleIcons()

	self:RegisterEvent("ADDON_LOADED")
end

MER:RegisterModule(module:GetName())
