import { Vec3 } from '../../../core/math/vec3.js';
import { CULLFACE_NONE } from '../../../platform/graphics/constants.js';
import { PlaneGeometry } from '../../../scene/geometry/plane-geometry.js';
import { Mesh } from '../../../scene/mesh.js';
import { TriData } from '../tri-data.js';
import { Shape } from './shape.js';

const UPDATE_EPSILON = 1e-6;
class PlaneShape extends Shape {
		constructor(device, args = {}){
				super(device, 'plane', args), this._cull = CULLFACE_NONE, this._size = 0.2, this._gap = 0.1, this._flipped = new Vec3();
				this._size = args.size ?? this._size;
				this._gap = args.gap ?? this._gap;
				this.triData = [
						new TriData(new PlaneGeometry())
				];
				this._createRenderComponent(this.entity, [
						Mesh.fromGeometry(this.device, new PlaneGeometry())
				]);
				this._update();
		}
		set size(value) {
				this._size = value ?? this._size;
				this._update();
		}
		get size() {
				return this._size;
		}
		set gap(value) {
				this._gap = value ?? this._gap;
				this._update();
		}
		get gap() {
				return this._gap;
		}
		set flipped(value) {
				if (this._flipped.distance(value) < UPDATE_EPSILON) {
						return;
				}
				this._flipped.copy(value);
				this._update();
		}
		get flipped() {
				return this._flipped;
		}
		_update() {
				const offset = this._size / 2 + this._gap;
				this._position.set(this._flipped.x ? -offset : offset, this._flipped.y ? -offset : offset, this._flipped.z ? -offset : offset);
				this._position[this.axis] = 0;
				this.entity.setLocalPosition(this._position);
				this.entity.setLocalEulerAngles(this._rotation);
				this.entity.setLocalScale(this._size, this._size, this._size);
		}
}

export { PlaneShape };
