local mMT, DB, M, E, P, L, MEDIA = unpack(ElvUI_mMediaTag)

local module = mMT:GetModule("Portraits")

function module:InitializePlayerPortrait()
	if module.db.player.enable then
		local portraits = module.portraits
		local parent_frame = _G.ElvUF_Player

		if parent_frame then
			local unit = "player"
			local type = "player"
			local name = "Player"

			portraits[unit] = portraits[unit] or module:CreatePortrait(name, parent_frame, E.db.mMediaTag.portraits.player)

			if portraits[unit] then
				portraits[unit].__owner = parent_frame
				portraits[unit].unit = unit --parent_frame.unit
				portraits[unit].type = type
				portraits[unit].db = E.db.mMediaTag.portraits.player
				portraits[unit].size = E.db.mMediaTag.portraits.player.size
				portraits[unit].point = E.db.mMediaTag.portraits.player.point
				portraits[unit].isPlayer = nil
				portraits[unit].unitClass = nil
				portraits[unit].lastGUID = nil
				portraits[unit].realUnit = "player"
				portraits[unit].name = name
				portraits[unit].forceExtra = E.db.mMediaTag.portraits.player.extra and "player" or nil

				portraits[unit].media = module:UpdateTexturesFiles(E.db.mMediaTag.portraits.player.texture, E.db.mMediaTag.portraits.player.mirror)

				module:UpdateSize(portraits[unit], portraits[unit].size, portraits[unit].point)
				module:InitPortrait(portraits[unit], E.db.mMediaTag.portraits.player.size, E.db.mMediaTag.portraits.player.point)

				if not portraits[unit].isEnabled then
					portraits[unit]:RegisterEvent("UNIT_ENTERED_VEHICLE")
					portraits[unit]:RegisterEvent("UNIT_EXITING_VEHICLE")
					portraits[unit]:RegisterEvent("UNIT_EXITED_VEHICLE")
					portraits[unit]:RegisterEvent("VEHICLE_UPDATE")
					portraits[unit].isEnabled = true
				end
			end
		end
	elseif module.portraits.player then
		module.portraits.player:UnregisterAllEvents()
		module.portraits.player:Hide()
		module.portraits.player = nil
	end
end
