import { Mat4 } from '../../core/math/mat4.js';
import { Vec4 } from '../../core/math/vec4.js';
import { BoundingBox } from '../../core/shape/bounding-box.js';
import { GSplatIntervalTexture } from './gsplat-interval-texture.js';

class GSplatInfo {
		constructor(device, resource, placement){
				this.activeSplats = 0;
				this.intervals = [];
				this.lineStart = 0;
				this.lineCount = 0;
				this.padding = 0;
				this.viewport = new Vec4();
				this.previousWorldTransform = new Mat4();
				this.aabb = new BoundingBox();
				this.device = device;
				this.resource = resource;
				this.node = placement.node;
				this.lodIndex = placement.lodIndex;
				this.numSplats = resource.centers.length / 3;
				this.aabb.copy(placement.aabb);
				this.intervalTexture = new GSplatIntervalTexture(device);
				this.updateIntervals(placement.intervals);
		}
		destroy() {
				this.intervals.length = 0;
				this.intervalTexture.destroy();
		}
		setLines(start, count, textureSize, activeSplats) {
				this.lineStart = start;
				this.lineCount = count;
				this.padding = textureSize * count - activeSplats;
				this.viewport.set(0, start, textureSize, count);
		}
		updateIntervals(intervals) {
				const resource = this.resource;
				this.intervals.length = 0;
				this.activeSplats = resource.numSplats;
				if (intervals.size > 0) {
						for (const interval of intervals.values()){
								this.intervals.push(interval.x, interval.y + 1);
						}
						this.activeSplats = this.intervalTexture.update(this.intervals);
				}
		}
		update() {
				const worldMatrix = this.node.getWorldTransform();
				const worldMatrixChanged = !this.previousWorldTransform.equals(worldMatrix);
				if (worldMatrixChanged) {
						this.previousWorldTransform.copy(worldMatrix);
				}
				return worldMatrixChanged;
		}
}

export { GSplatInfo };
