export function registerOcclusionConfig_old() {
	// Criar uma nova camada personalizada no canvas
	class OcclusionLayer extends PIXI.Container {
		 constructor() {
			  super();
			  this.sortableChildren = true;
		 }
	}

	// Hooks para gerenciar a camada de oclusão
	Hooks.on('canvasReady', () => {
		 canvas.occlusionLayer = canvas.stage.addChildAt(
			  new OcclusionLayer(), 
			  canvas.stage.getChildIndex(canvas.tokens)
		 );
	});

	// Hooks originais
	Hooks.on('createToken', handleTokenDocument);
	Hooks.on('updateToken', handleTokenDocument);
	Hooks.on('refreshToken', processTokenOcclusion);
	Hooks.on('controlToken', processTokenOcclusion);
	Hooks.on('deleteToken', function(tokenDoc) {
		 PIXI_FILTERS.delete(tokenDoc.id);
		 removeOcclusionSprite(tokenDoc.id);
	});

	Hooks.on('updateTile', processAllTokens);
	Hooks.on('refreshTile', processAllTokens);
	Hooks.on('canvasReady', processAllTokens);
	Hooks.on('canvasPan', processAllTokens);
	Hooks.on('canvasTokensRefresh', processAllTokens);
}

// Mapa global para armazenar os filtros PIXI e sprites de oclusão
const PIXI_FILTERS = new Map();
const OCCLUSION_SPRITES = new Map();

function processAllTokens() {
	canvas.tokens.placeables.forEach(function(token) {
		 processTokenOcclusion(token);
	});
}

function handleTokenDocument(tokenDoc) {
	processTokenOcclusion(tokenDoc.object);
}

function processTokenOcclusion(token) {
	if (!token?.mesh) return;

	const occludingTiles = canvas.tiles.placeables.filter(tile => 
		 tile.document.occlusion?.mode !== CONST.TILE_OCCLUSION_MODES.NONE
	);

	let occluded = false;
	for (const tile of occludingTiles) {
		 const intersection = getTokenTileIntersection(token, tile);
		 if (intersection) {
			  applyOcclusionEffects(token, tile, intersection);
			  occluded = true;
			  break; // Aplica oclusão no primeiro tile que intersecta
		 }
	}

	if (!occluded) {
		 removeOcclusionEffects(token);
	}
}

function getTokenTileIntersection(token, tile) {
	const tokenBounds = token.mesh.getBounds();
	const tileBounds = tile.mesh.getBounds();

	const intersection = {
		 left:   Math.max(tokenBounds.left, tileBounds.left),
		 right:  Math.min(tokenBounds.right, tileBounds.right),
		 top:    Math.max(tokenBounds.top, tileBounds.top),
		 bottom: Math.min(tokenBounds.bottom, tileBounds.bottom),
	};

	if (intersection.left >= intersection.right || intersection.top >= intersection.bottom) {
		 return null; // Sem interseção
	}

	return new PIXI.Rectangle(
		 intersection.left,
		 intersection.top,
		 intersection.right - intersection.left,
		 intersection.bottom - intersection.top
	);
}

function createOcclusionSprite(token, tile, intersection) {
	// Remover sprite de oclusão anterior, se existir
	removeOcclusionSprite(token.id);

	const renderTexture = PIXI.RenderTexture.create({
		 width: intersection.width,
		 height: intersection.height
	});

	const tokenSprite = new PIXI.Sprite(token.mesh.texture);
	tokenSprite.position.set(-intersection.x, -intersection.y);

	const tileSprite = new PIXI.Sprite(tile.mesh.texture);
	tileSprite.position.set(-intersection.x, -intersection.y);

	const maskContainer = new PIXI.Container();
	maskContainer.addChild(tileSprite);
	maskContainer.addChild(tokenSprite);

	canvas.app.renderer.render(maskContainer, {
		 renderTexture: renderTexture,
		 clear: true
	});

	const occlusionSprite = new PIXI.Sprite(renderTexture);
	
	// Posicionar o sprite na mesma posição do token original
	occlusionSprite.position.set(
		 token.mesh.position.x, 
		 token.mesh.position.y
	);

	// Adicionar à camada de oclusão
	canvas.occlusionLayer.addChild(occlusionSprite);

	// Armazenar referência do sprite
	OCCLUSION_SPRITES.set(token.id, occlusionSprite);

	return occlusionSprite;
}

function removeOcclusionSprite(tokenId) {
	const existingSprite = OCCLUSION_SPRITES.get(tokenId);
	if (existingSprite) {
		 canvas.occlusionLayer.removeChild(existingSprite);
		 OCCLUSION_SPRITES.delete(tokenId);
	}
}

function applyOcclusionEffects(token, tile, intersection) {
	if (PIXI_FILTERS.has(token.id)) return;

	const occlusionSprite = createOcclusionSprite(token, tile, intersection);
	token.mesh.mask = occlusionSprite;

	// Filtros de oclusão (mantidos conforme o código original)
	const alphaFilter = new PIXI.filters.AlphaFilter(0.5);
	const outlineFilter = new PIXI.filters.isoOutlineFilter(1, 0xff0000, 0.7);
	const colorMatrixFilter = new PIXI.filters.ColorMatrixFilter();
	
	colorMatrixFilter.alpha = 0.7;
	colorMatrixFilter.matrix = [
		 0.5, 0, 0, 0, 0,
		 0, 0.5, 0, 0, 0,
		 0, 0, 0.5, 0, 0,
		 0, 0, 0, 1.0, 0
	];

	const filters = [alphaFilter, outlineFilter, colorMatrixFilter];
	token.mesh.filters = filters;

	PIXI_FILTERS.set(token.id, { 
		 mask: occlusionSprite, 
		 filters: filters 
	});
}

function removeOcclusionEffects(token) {
	const tokenData = PIXI_FILTERS.get(token.id);
	if (tokenData) {
		 token.mesh.mask = null;
		 token.mesh.filters = [];
		 PIXI_FILTERS.delete(token.id);
		 removeOcclusionSprite(token.id);
	}
}








// Definição do isoOutlineFilter
if (typeof PIXI !== 'undefined' && PIXI.filters) {
	const vertexShader =`
		attribute vec2 aVertexPosition;
		attribute vec2 aTextureCoord;

		uniform mat3 projectionMatrix;
		varying vec2 vTextureCoord;

		void main(void) {
			gl_Position = vec4((projectionMatrix * vec3(aVertexPosition, 1.0)).xy, 0.0, 1.0);
			vTextureCoord = aTextureCoord;
		}
	`;

	const fragmentShader = `
		varying vec2 vTextureCoord;

		uniform sampler2D uSampler;
		uniform float alpha;
		uniform vec2 outlineThickness;
		uniform vec4 outlineColor;
		uniform vec4 filterArea;
		uniform vec4 filterClamp;

		void main(void) {
			vec4 ownColor = texture2D(uSampler, vTextureCoord);
			vec4 curColor;
			float maxAlpha = 0.0;
			vec2 displaced;

			for (float angle = 0.0; angle < 6.28318530718; angle += 0.78539816339) {
				displaced.x = vTextureCoord.x + outlineThickness.x * cos(angle);
				displaced.y = vTextureCoord.y + outlineThickness.y * sin(angle);
				curColor = texture2D(uSampler, displaced);
				maxAlpha = max(maxAlpha, curColor.a);
			}
			float resultAlpha = max(maxAlpha, ownColor.a);
			gl_FragColor = vec4((ownColor.rgb * ownColor.a + outlineColor.rgb * (1.0 - ownColor.a)) * resultAlpha, resultAlpha);
		 }
	`;

	// Defina a classe isoOutlineFilter
	class IsoOutlineFilter extends PIXI.Filter {
		constructor(thickness = 1, color = 0x000000, alpha = 1) {
			super(vertexShader, fragmentShader);

			// Inicialize os uniforms
			this.uniforms.outlineColor = new Float32Array(4); // Para armazenar RGBA
			this.uniforms.outlineThickness = new Float32Array(2); // Para armazenar X e Y
			this.uniforms.filterArea = new Float32Array(2); // Para área de filtro
			this.uniforms.alpha = alpha;

			// Configure as propriedades iniciais
			this.color = color;
			this.thickness = thickness;
		}

		get alpha() {
			return this.uniforms.alpha;
		}
		set alpha(value) {
			this.uniforms.alpha = value;
		}

		get color() {
			return PIXI.utils.rgb2hex(this.uniforms.outlineColor);
		}
		set color(value) {
			PIXI.utils.hex2rgb(value, this.uniforms.outlineColor);
		}

		get thickness() {
			return this.uniforms.outlineThickness[0];
		}
		set thickness(value) {
			// Certifique-se de que filterArea tenha valores válidos
			const filterAreaX = this.uniforms.filterArea[0] || 1; // Evite divisão por 0
			const filterAreaY = this.uniforms.filterArea[1] || 1; // Evite divisão por 0
			
			this.uniforms.outlineThickness[0] = value / filterAreaX;
			this.uniforms.outlineThickness[1] = value / filterAreaY;
		}
	}

	// Adicione o isoOutlineFilter ao namespace PIXI.filters
	PIXI.filters.isoOutlineFilter = IsoOutlineFilter;
} else {
	console.error('PIXI ou PIXI.filters não estão disponíveis.');
}