/*
RSK World – Tower Defense 3D (UpgradeSystem)
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 SelectionManager {
	constructor(scene) {
		this.scene = scene;
		this.current = null;
		this.outline = null;
	}

	select(object3D) {
		this.clear();
		if (!object3D) return;
		this.current = object3D;
		const box = new THREE.Box3().setFromObject(object3D);
		const size = new THREE.Vector3(); box.getSize(size);
		const radius = Math.max(size.x, size.z) * 0.6;
		const geom = new THREE.RingGeometry(radius * 0.9, radius, 24);
		const mat = new THREE.MeshBasicMaterial({ color: 0x22d3ee, transparent: true, opacity: 0.65, side: THREE.DoubleSide });
		const ring = new THREE.Mesh(geom, mat);
		ring.rotation.x = -Math.PI / 2;
		ring.position.copy(object3D.position).setY(0.02);
		this.scene.add(ring);
		this.outline = ring;
	}

	clear() {
		if (this.outline) { this.scene.remove(this.outline); this.outline.geometry.dispose(); this.outline.material.dispose(); }
		this.outline = null; this.current = null;
	}
}

export class UpgradeSystem {
	constructor() {
		this.maxLevel = 3;
		this.baseCosts = { tower: 100, wall: 50 };
		this.multipliers = { damage: 1.5, range: 1.2, cooldown: 0.85, wallHp: 1.8 };
	}

	getUpgradeCost(type, level) {
		const base = this.baseCosts[type] ?? 100;
		return Math.round(base * Math.pow(1.8, level));
	}

	applyTowerUpgrade(tower, level) {
		tower.level = level;
		tower.range = 12 * Math.pow(this.multipliers.range, level - 1);
		tower.fireCooldown = 0.6 * Math.pow(this.multipliers.cooldown, level - 1);
		tower.damageMultiplier = Math.pow(this.multipliers.damage, level - 1);
		if (tower._ring) tower._ring.scale.setScalar(1 + (level - 1) * 0.05);
	}

	applyWallUpgrade(wall, level) {
		wall.level = level;
		wall.maxHp = Math.floor(100 * Math.pow(this.multipliers.wallHp, level - 1));
		wall.hp = wall.maxHp;
	}
}
