/*
RSK World – Tower Defense 3D (Village)
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';

class BuildingBase {
	constructor(position) {
		this.position = position.clone();
		this.group = new THREE.Group();
		this.group.position.copy(this.position);
	}
	addTo(scene) { scene.add(this.group); }
	removeFrom(scene) { scene.remove(this.group); }
}

export class TownHall extends BuildingBase {
	constructor(position) {
		super(position);
		this._create();
	}
	_create() {
		const base = new THREE.Mesh(
			new THREE.BoxGeometry(6, 2.2, 6),
			new THREE.MeshStandardMaterial({ color: 0x475569, roughness: 0.9 })
		);
		base.position.y = 1.1;
		base.castShadow = base.receiveShadow = true;
		this.group.add(base);

		const roof = new THREE.Mesh(
			new THREE.ConeGeometry(4.8, 2.2, 5),
			new THREE.MeshStandardMaterial({ color: 0x9a3412, roughness: 0.8 })
		);
		roof.position.y = 2.2 + 1.1;
		roof.rotation.y = Math.PI / 5;
		roof.castShadow = true;
		this.group.add(roof);
	}
}

export class Hut extends BuildingBase {
	constructor(position, scale = 1) {
		super(position);
		this.scale = scale;
		this._create();
	}
	_create() {
		const s = this.scale;
		const base = new THREE.Mesh(
			new THREE.CylinderGeometry(1.2 * s, 1.4 * s, 1.2 * s, 6),
			new THREE.MeshStandardMaterial({ color: 0x64748b, roughness: 0.95 })
		);
		base.position.y = 0.6 * s;
		base.castShadow = base.receiveShadow = true;
		this.group.add(base);

		const roof = new THREE.Mesh(
			new THREE.ConeGeometry(1.8 * s, 1.2 * s, 6),
			new THREE.MeshStandardMaterial({ color: 0x7c2d12, roughness: 0.85 })
		);
		roof.position.y = 1.2 * s + 0.6 * s;
		roof.castShadow = true;
		this.group.add(roof);
	}
}

export class VillageManager {
	constructor(scene, center = new THREE.Vector3(-36, 0, 22)) {
		this.scene = scene;
		this.center = center.clone();
		this.buildings = [];
	}

	spawnDefault() {
		const hall = new TownHall(this.center);
		hall.addTo(this.scene);
		this.buildings.push(hall);
		const offsets = [
			new THREE.Vector3(-8, 0, 5),
			new THREE.Vector3(-10, 0, -4),
			new THREE.Vector3(6, 0, 4),
			new THREE.Vector3(6, 0, -6)
		];
		for (const o of offsets) {
			const hut = new Hut(this.center.clone().add(o), THREE.MathUtils.randFloat(0.9, 1.2));
			hut.addTo(this.scene);
			this.buildings.push(hut);
		}
	}
}
