/*
RSK World – Tower Defense 3D (Resources)
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 ResourceState {
	constructor() {
		this.gold = 250;
		this.elixir = 0;
		this.builders = 1;
		this.storageCap = { gold: 1000, elixir: 1000 };
	}

	canAffordGold(amount) { return this.gold >= amount; }
	canAffordElixir(amount) { return this.elixir >= amount; }

	spendGold(amount) { if (this.gold < amount) return false; this.gold -= amount; return true; }
	spendElixir(amount) { if (this.elixir < amount) return false; this.elixir -= amount; return true; }

	earnGold(amount) { this.gold = Math.min(this.gold + amount, this.storageCap.gold); }
	earnElixir(amount) { this.elixir = Math.min(this.elixir + amount, this.storageCap.elixir); }
}

class ProdBuilding {
	constructor(position) {
		this.position = position.clone();
		this.group = new THREE.Group();
		this.group.position.copy(this.position);
		this.level = 1;
		this.timer = 0;
		this.rate = 5; // per tick
		this.tick = 2.5; // seconds
	}
	addTo(scene) { scene.add(this.group); }
	removeFrom(scene) { scene.remove(this.group); }
	update(delta, res) { this.timer += delta; if (this.timer >= this.tick) { this.timer = 0; this.produce(res); } }
	produce(res) {}
}

export class GoldMine extends ProdBuilding {
	constructor(position) {
		super(position);
		this.rate = 10; this.tick = 2.0; this._create();
	}
	_create() {
		const base = new THREE.Mesh(new THREE.CylinderGeometry(1.2, 1.4, 1.2, 6), new THREE.MeshStandardMaterial({ color: 0x4b5563 }));
		base.position.y = 0.6; base.castShadow = base.receiveShadow = true; this.group.add(base);
		const drum = new THREE.Mesh(new THREE.TorusGeometry(0.9, 0.2, 8, 16), new THREE.MeshStandardMaterial({ color: 0xf59e0b }));
		drum.rotation.x = Math.PI / 2; drum.position.y = 1.0; this.group.add(drum);
	}
	produce(res) { res.earnGold(this.rate); }
}

export class ElixirCollector extends ProdBuilding {
	constructor(position) {
		super(position);
		this.rate = 10; this.tick = 2.0; this._create();
	}
	_create() {
		const base = new THREE.Mesh(new THREE.CylinderGeometry(1.2, 1.4, 1.2, 6), new THREE.MeshStandardMaterial({ color: 0x4c1d95 }));
		base.position.y = 0.6; base.castShadow = base.receiveShadow = true; this.group.add(base);
		const dome = new THREE.Mesh(new THREE.SphereGeometry(0.8, 16, 12), new THREE.MeshStandardMaterial({ color: 0x9333ea, emissive: 0x7c3aed, emissiveIntensity: 0.4 }));
		dome.position.y = 1.2; this.group.add(dome);
	}
	produce(res) { res.earnElixir(this.rate); }
}

export class Storage extends ProdBuilding {
	constructor(position) {
		super(position);
		this.tick = 9999; // no production
		this._create();
	}
	_create() {
		const crate = new THREE.Mesh(new THREE.BoxGeometry(2.2, 1.6, 2.2), new THREE.MeshStandardMaterial({ color: 0x78350f, roughness: 0.9 }));
		crate.position.y = 0.8; crate.castShadow = crate.receiveShadow = true; this.group.add(crate);
	}
	produce(res) {}
}
