/*
RSK World – Tower Defense 3D (Barracks)
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 Barracks {
	constructor(position) {
		this.position = position.clone();
		this.group = new THREE.Group();
		this.group.position.copy(this.position);
		this.level = 1;
		this.queue = [];
		this.timer = 0;
		this._create();
	}

	_create() {
		const base = new THREE.Mesh(new THREE.BoxGeometry(3, 1.2, 3), new THREE.MeshStandardMaterial({ color: 0x334155, roughness: 0.9 }));
		base.position.y = 0.6; base.castShadow = base.receiveShadow = true; this.group.add(base);
		const arch = new THREE.Mesh(new THREE.TorusGeometry(1.2, 0.18, 10, 24), new THREE.MeshStandardMaterial({ color: 0x9a3412, roughness: 0.8 }));
		arch.rotation.z = Math.PI / 2; arch.position.y = 1.2; this.group.add(arch);
		const flag = new THREE.Mesh(new THREE.PlaneGeometry(0.9, 0.6), new THREE.MeshStandardMaterial({ color: 0xf59e0b, side: THREE.DoubleSide }));
		flag.position.set(1.6, 1.4, 0); flag.rotation.y = Math.PI / 2; this.group.add(flag);
	}

	addTo(scene) { scene.add(this.group); }
	removeFrom(scene) { scene.remove(this.group); }

	// Add a unit to training queue; returns true if enqueued
	train(unitType) {
		const cfg = this._unitConfig(unitType);
		if (!cfg) return false;
		this.queue.push({ type: unitType, time: cfg.time });
		return true;
	}

	_unitConfig(type) {
		if (type === 'barbarian') return { time: 2.5 };
		return null;
	}

	update(delta, spawnCb) {
		if (this.queue.length === 0) return;
		this.timer += delta;
		const head = this.queue[0];
		if (this.timer >= head.time) {
			this.timer = 0;
			this.queue.shift();
			spawnCb(head.type, this.group.position.clone().add(new THREE.Vector3(1.4, 0, 0)));
		}
	}
}
