/*
RSK World – Tower Defense 3D (Environment Evolution)
For support, licensing, or custom development inquiries: help@rskworld.in • Phone/WhatsApp: +91 9330539277
Website: RSK World • Business Inquiries: help@rskworld.in
🤝 Contributing: Suggestions and improvements are welcome!
*/

import * as THREE from 'three';

export class Environment {
	constructor(scene, pathPoints) {
		this.scene = scene;
		this.path = pathPoints;
		this._createBackground();
		this._createTrees();
		this._createStars();
	}

	_createBackground() {
		// Big surrounding cylinder for depth
		const bgGeo = new THREE.CylinderGeometry(150, 150, 40, 16, 1, true);
		const bgMat = new THREE.MeshBasicMaterial({ 
			color: 0x070b14, 
			side: THREE.BackSide, 
			fog: false 
		});
		const bg = new THREE.Mesh(bgGeo, bgMat);
		bg.position.y = 10;
		this.scene.add(bg);
	}

	_createStars() {
		const count = 2000;
		const pos = new Float32Array(count * 3);
		for (let i = 0; i < count; i++) {
			const theta = Math.random() * Math.PI * 2;
			const phi = Math.acos(2 * Math.random() - 1);
			const r = 180 + Math.random() * 20;
			pos[i * 3] = r * Math.sin(phi) * Math.cos(theta);
			pos[i * 3 + 1] = r * Math.sin(phi) * Math.sin(theta);
			pos[i * 3 + 2] = r * Math.cos(phi);
		}
		const geo = new THREE.BufferGeometry();
		geo.setAttribute('position', new THREE.BufferAttribute(pos, 3));
		const mat = new THREE.PointsMaterial({ color: 0xffffff, size: 0.5, sizeAttenuation: true, transparent: true, opacity: 0.8 });
		const stars = new THREE.Points(geo, mat);
		this.scene.add(stars);
	}

	_createTrees() {
		for (let i = 0; i < 60; i++) {
			const x = (Math.random() - 0.5) * 160;
			const z = (Math.random() - 0.5) * 160;
			
			// Don't place on path or too close to center village
			const pos = new THREE.Vector3(x, 0, z);
			if (this._distToPath(pos) < 5 || pos.length() < 12) continue;

			this._spawnTree(pos);
		}
	}

	_spawnTree(pos) {
		const g = new THREE.Group();
		g.position.copy(pos);
		
		const trunk = new THREE.Mesh(
			new THREE.CylinderGeometry(0.2, 0.3, 1), 
			new THREE.MeshStandardMaterial({ color: 0x452711, roughness: 1.0 })
		);
		trunk.position.y = 0.5;
		trunk.castShadow = true;
		g.add(trunk);

		const leaves = new THREE.Mesh(
			new THREE.ConeGeometry(1.2, 2.5, 6),
			new THREE.MeshStandardMaterial({ color: 0x14532d, roughness: 0.8 })
		);
		leaves.position.y = 2.2;
		leaves.castShadow = true;
		g.add(leaves);

		const leaves2 = leaves.clone();
		leaves2.scale.set(0.8, 0.8, 0.8);
		leaves2.position.y = 3.2;
		g.add(leaves2);

		this.scene.add(g);
	}

	_distToPath(point) {
		let min = Infinity;
		for (let i = 0; i < this.path.length - 1; i++) {
			const a = this.path[i];
			const b = this.path[i+1];
			const ab = new THREE.Vector3().subVectors(b, a);
			const ap = new THREE.Vector3().subVectors(point, a);
			const t = THREE.MathUtils.clamp(ap.dot(ab) / ab.lengthSq(), 0, 1);
			const proj = new THREE.Vector3().copy(a).add(ab.multiplyScalar(t));
			const d = proj.distanceTo(point);
			if (d < min) min = d;
		}
		return min;
	}
}
